From 9efefb667c7ccbd89cbfcce082dcedbd991c2762 Mon Sep 17 00:00:00 2001 From: Sean Doyle Date: Wed, 22 Oct 2025 12:33:24 -0400 Subject: [PATCH] Fix inconsistent custom format documentation Prior to this commit, the documentation for setting a custom Format was inconsistently mixing module-level and instance-level method definitions. There isn't existing precedent in the documentation for how to declare custom formats that share behavior with first-party formats (like xml and json). Since the [test suite][] utilizes a `class`-based approach (rather than a `module`-based one), change the `Base` documentation to follow that precedent. [test suite]: https://github.com/rails/activeresource/blob/v6.2.0/test/fixtures/address.rb#L4-L15 --- lib/active_resource/base.rb | 8 ++++---- test/fixtures/address.rb | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/active_resource/base.rb b/lib/active_resource/base.rb index a53b6befee..72aed655d0 100644 --- a/lib/active_resource/base.rb +++ b/lib/active_resource/base.rb @@ -105,10 +105,10 @@ module ActiveResource # To customize how attributes are encoded and decoded, declare a format and override # its +encode+ and +decode+ methods: # - # module CamelcaseJsonFormat - # extend ActiveResource::Formats[:json] + # class CamelcaseJsonFormat + # include ActiveResource::Formats[:json] # - # def self.encode(resource, options = nil) + # def encode(resource, options = nil) # hash = resource.as_json(options) # hash = hash.deep_transform_keys! { |key| key.camelcase(:lower) } # super(hash) @@ -120,7 +120,7 @@ module ActiveResource # end # end # - # Person.format = CamelcaseJsonFormat + # Person.format = CamelcaseJsonFormat.new # # person = Person.new(first_name: "First", last_name: "Last") # person.encode diff --git a/test/fixtures/address.rb b/test/fixtures/address.rb index 742a335600..67a5562b13 100644 --- a/test/fixtures/address.rb +++ b/test/fixtures/address.rb @@ -5,7 +5,7 @@ class AddressXMLFormatter include ActiveResource::Formats::XmlFormat def decode(xml) - data = ActiveResource::Formats::XmlFormat.decode(xml) + data = super # process address fields data.each do |address| address["city_state"] = "#{address['city']}, #{address['state']}"