Skip to content

Commit faed3b0

Browse files
committed
Restore tests without legacy attributes support
1 parent 4529729 commit faed3b0

File tree

7 files changed

+46
-19
lines changed

7 files changed

+46
-19
lines changed

lib/active_model_serializers/model.rb

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,7 @@ class Model
99
# Configuration to avoid a breaking change with older versions of this class which lacked defined attributes.
1010
# Previous behavior was: 1) initialized attributes were the
1111
class_attribute :attributes_are_always_the_initialization_data, instance_writer: false, instance_reader: false
12-
self.attributes_are_always_the_initialization_data = false # remove this commit, just for demonstration
13-
module AttributesAreAlwaysTheInitializationData
14-
def initialize(attributes = {})
15-
@initialized_attributes = attributes && attributes.symbolize_keys
16-
super
17-
end
18-
19-
# Defaults to the downcased model name.
20-
def id
21-
@initialized_attributes.fetch(:id) { self.class.model_name.name && self.class.model_name.name.downcase }
22-
end
23-
24-
def attributes
25-
@initialized_attributes
26-
end
27-
end
12+
self.attributes_are_always_the_initialization_data = true
2813

2914
def self.inherited(subclass)
3015
if subclass.attributes_are_always_the_initialization_data
@@ -96,5 +81,21 @@ def self.lookup_ancestors
9681
[self]
9782
end
9883
# :nocov:
84+
85+
module AttributesAreAlwaysTheInitializationData
86+
def initialize(attributes = {})
87+
@initialized_attributes = attributes && attributes.symbolize_keys
88+
super
89+
end
90+
91+
# Defaults to the downcased model name.
92+
def id
93+
@initialized_attributes.fetch(:id) { self.class.model_name.name && self.class.model_name.name.downcase }
94+
end
95+
96+
def attributes
97+
@initialized_attributes
98+
end
99+
end
99100
end
100101
end

test/action_controller/adapter_selector_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@
33
module ActionController
44
module Serialization
55
class AdapterSelectorTest < ActionController::TestCase
6+
Profile = poro_without_legacy_model_support(::Model) do
7+
attributes :name, :description
8+
associations :comments
9+
end
10+
class ProfileSerializer < ActiveModel::Serializer
11+
type 'profiles'
12+
attributes :name, :description
13+
end
614
class AdapterSelectorTestController < ActionController::Base
715
def render_using_default_adapter
816
@profile = Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1')

test/active_model_serializers/model_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def test_initialization_with_string_keys
2020
end
2121

2222
def test_attributes_can_be_read_for_serialization
23-
klass = Class.new(ActiveModelSerializers::Model) do
23+
klass = poro_without_legacy_model_support do
2424
attributes :one, :two, :three
2525
end
2626
original_attributes = { one: 1, two: 2, three: 3 }
@@ -42,7 +42,7 @@ def test_attributes_can_be_read_for_serialization
4242
end
4343

4444
def test_id_attribute_can_be_read_for_serialization
45-
klass = Class.new(ActiveModelSerializers::Model) do
45+
klass = poro_without_legacy_model_support do
4646
attributes :id, :one, :two, :three
4747
end
4848
self.class.const_set(:SomeTestModel, klass)

test/cache_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class InheritedRoleSerializer < RoleSerializer
4949
attribute :special_attribute
5050
end
5151

52-
class Comment < ::Model
52+
Comment = poro_without_legacy_model_support(::Model) do
5353
attributes :body
5454
associations :post, :author
5555

test/fixtures/poro.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
ActiveModelSerializers::Model.attributes_are_always_the_initialization_data = false
12
class Model < ActiveModelSerializers::Model
23
FILE_DIGEST = Digest::MD5.hexdigest(File.open(__FILE__).read)
34

test/support/poro.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module ActiveModelSerializersWithoutLegacyModelSupport
2+
module_function
3+
4+
def poro_without_legacy_model_support(superklass = ActiveModelSerializers::Model, &block)
5+
original_attributes_are_always_the_initialization_data = superklass.attributes_are_always_the_initialization_data
6+
superklass.attributes_are_always_the_initialization_data = false
7+
Class.new(superklass) do
8+
class_exec(&block) if block
9+
end
10+
ensure
11+
superklass.attributes_are_always_the_initialization_data = original_attributes_are_always_the_initialization_data
12+
end
13+
end
14+
Minitest::Test.include ActiveModelSerializersWithoutLegacyModelSupport
15+
Minitest::Test.extend ActiveModelSerializersWithoutLegacyModelSupport

test/test_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ def serialization_options(options)
4040
require 'minitest/autorun'
4141
Minitest.backtrace_filter = Minitest::BacktraceFilter.new
4242

43+
require 'support/poro'
44+
4345
require 'support/rails_app'
4446

4547
# require "rails/test_help"

0 commit comments

Comments
 (0)