diff --git a/lib/active_model_serializers/model.rb b/lib/active_model_serializers/model.rb index 8de5a27d1..acc7a4a82 100644 --- a/lib/active_model_serializers/model.rb +++ b/lib/active_model_serializers/model.rb @@ -9,22 +9,7 @@ class Model # Configuration to avoid a breaking change with older versions of this class which lacked defined attributes. # Previous behavior was: 1) initialized attributes were the class_attribute :attributes_are_always_the_initialization_data, instance_writer: false, instance_reader: false - self.attributes_are_always_the_initialization_data = false # remove this commit, just for demonstration - module AttributesAreAlwaysTheInitializationData - def initialize(attributes = {}) - @initialized_attributes = attributes && attributes.symbolize_keys - super - end - - # Defaults to the downcased model name. - def id - @initialized_attributes.fetch(:id) { self.class.model_name.name && self.class.model_name.name.downcase } - end - - def attributes - @initialized_attributes - end - end + self.attributes_are_always_the_initialization_data = true def self.inherited(subclass) if subclass.attributes_are_always_the_initialization_data @@ -96,5 +81,21 @@ def self.lookup_ancestors [self] end # :nocov: + + module AttributesAreAlwaysTheInitializationData + def initialize(attributes = {}) + @initialized_attributes = attributes && attributes.symbolize_keys + super + end + + # Defaults to the downcased model name. + def id + @initialized_attributes.fetch(:id) { self.class.model_name.name && self.class.model_name.name.downcase } + end + + def attributes + @initialized_attributes + end + end end end diff --git a/test/action_controller/adapter_selector_test.rb b/test/action_controller/adapter_selector_test.rb index 6f22aae25..7489598a5 100644 --- a/test/action_controller/adapter_selector_test.rb +++ b/test/action_controller/adapter_selector_test.rb @@ -3,6 +3,14 @@ module ActionController module Serialization class AdapterSelectorTest < ActionController::TestCase + Profile = poro_without_legacy_model_support(::Model) do + attributes :name, :description + associations :comments + end + class ProfileSerializer < ActiveModel::Serializer + type 'profiles' + attributes :name, :description + end class AdapterSelectorTestController < ActionController::Base def render_using_default_adapter @profile = Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1') diff --git a/test/active_model_serializers/model_test.rb b/test/active_model_serializers/model_test.rb index c2f316479..4435f8192 100644 --- a/test/active_model_serializers/model_test.rb +++ b/test/active_model_serializers/model_test.rb @@ -20,7 +20,7 @@ def test_initialization_with_string_keys end def test_attributes_can_be_read_for_serialization - klass = Class.new(ActiveModelSerializers::Model) do + klass = poro_without_legacy_model_support do attributes :one, :two, :three end original_attributes = { one: 1, two: 2, three: 3 } @@ -42,7 +42,7 @@ def test_attributes_can_be_read_for_serialization end def test_id_attribute_can_be_read_for_serialization - klass = Class.new(ActiveModelSerializers::Model) do + klass = poro_without_legacy_model_support do attributes :id, :one, :two, :three end self.class.const_set(:SomeTestModel, klass) diff --git a/test/cache_test.rb b/test/cache_test.rb index b2cb27eb4..9c00c4ed9 100644 --- a/test/cache_test.rb +++ b/test/cache_test.rb @@ -49,7 +49,7 @@ class InheritedRoleSerializer < RoleSerializer attribute :special_attribute end - class Comment < ::Model + Comment = poro_without_legacy_model_support(::Model) do attributes :body associations :post, :author diff --git a/test/fixtures/poro.rb b/test/fixtures/poro.rb index 3c804ccca..6abecef74 100644 --- a/test/fixtures/poro.rb +++ b/test/fixtures/poro.rb @@ -1,3 +1,4 @@ +ActiveModelSerializers::Model.attributes_are_always_the_initialization_data = false class Model < ActiveModelSerializers::Model FILE_DIGEST = Digest::MD5.hexdigest(File.open(__FILE__).read) diff --git a/test/support/poro.rb b/test/support/poro.rb new file mode 100644 index 000000000..e7f525b11 --- /dev/null +++ b/test/support/poro.rb @@ -0,0 +1,15 @@ +module ActiveModelSerializersWithoutLegacyModelSupport + module_function + + def poro_without_legacy_model_support(superklass = ActiveModelSerializers::Model, &block) + original_attributes_are_always_the_initialization_data = superklass.attributes_are_always_the_initialization_data + superklass.attributes_are_always_the_initialization_data = false + Class.new(superklass) do + class_exec(&block) if block + end + ensure + superklass.attributes_are_always_the_initialization_data = original_attributes_are_always_the_initialization_data + end +end +Minitest::Test.include ActiveModelSerializersWithoutLegacyModelSupport +Minitest::Test.extend ActiveModelSerializersWithoutLegacyModelSupport diff --git a/test/test_helper.rb b/test/test_helper.rb index e96c4840f..664374488 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -40,6 +40,8 @@ def serialization_options(options) require 'minitest/autorun' Minitest.backtrace_filter = Minitest::BacktraceFilter.new +require 'support/poro' + require 'support/rails_app' # require "rails/test_help"