-
Notifications
You must be signed in to change notification settings - Fork 362
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support XML and JSON inflector acronyms
Related to #361 Add special-case treatment of JSON and XML acronyms by declaring aliases for the `JsonFormat` and `XmlFormat` constants. In addition to the aliases, this commit also includes test coverage to cover existing behavior. The original PR included a comment citing that `ActiveSupport::Inflector::Inflections#clear` was not working as expected. It was resolved by [a76344f][], which was merged into `main` prior to the `7.0.0` release. Since the CI matrix currently includes `7-0-stable` as the minimum version, the code can rely on the resolved behavior. [a76344f]: rails/rails@a76344f
- Loading branch information
1 parent
9c8a2ee
commit 9122780
Showing
3 changed files
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,5 +24,7 @@ def decode(json) | |
Formats.remove_root(ActiveSupport::JSON.decode(json)) | ||
end | ||
end | ||
|
||
JSONFormat = JsonFormat | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,5 +23,7 @@ def decode(xml) | |
Formats.remove_root(Hash.from_xml(xml)) | ||
end | ||
end | ||
|
||
XMLFormat = XmlFormat | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# frozen_string_literal: true | ||
|
||
require "abstract_unit" | ||
|
||
class FormatsTest < ActiveSupport::TestCase | ||
def test_json_format_uses_camelcase | ||
assert_equal ActiveResource::Formats::JsonFormat, ActiveResource::Formats[:json] | ||
end | ||
|
||
def test_xml_format_uses_camelcase | ||
assert_equal ActiveResource::Formats::XmlFormat, ActiveResource::Formats[:xml] | ||
end | ||
|
||
def test_custom_format_uses_camelcase | ||
klass = Class.new | ||
ActiveResource::Formats.const_set(:MsgpackFormat, klass) | ||
|
||
assert_equal klass, ActiveResource::Formats[:msgpack] | ||
ensure | ||
ActiveResource::Formats.send(:remove_const, :MsgpackFormat) | ||
end | ||
|
||
def test_unknown_format_raises_not_found_error | ||
assert_raises NameError, match: "uninitialized constant ActiveResource::Formats::MsgpackFormat" do | ||
ActiveResource::Formats[:msgpack] | ||
end | ||
end | ||
|
||
def test_json_format_uses_acronym_inflections | ||
ActiveSupport::Inflector.inflections { |inflect| inflect.acronym "JSON" } | ||
|
||
assert_equal ActiveResource::Formats::JsonFormat, ActiveResource::Formats[:json] | ||
ensure | ||
ActiveSupport::Inflector.inflections.clear | ||
end | ||
|
||
def test_xml_format_uses_acronym_inflections | ||
ActiveSupport::Inflector.inflections { |inflect| inflect.acronym "XML" } | ||
|
||
assert_equal ActiveResource::Formats::XmlFormat, ActiveResource::Formats[:xml] | ||
ensure | ||
ActiveSupport::Inflector.inflections.clear | ||
end | ||
end |