Skip to content

Commit

Permalink
Restore tests without legacy attributes support
Browse files Browse the repository at this point in the history
  • Loading branch information
bf4 committed Dec 26, 2016
1 parent 4529729 commit faed3b0
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 19 deletions.
33 changes: 17 additions & 16 deletions lib/active_model_serializers/model.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
8 changes: 8 additions & 0 deletions test/action_controller/adapter_selector_test.rb
Expand Up @@ -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')
Expand Down
4 changes: 2 additions & 2 deletions test/active_model_serializers/model_test.rb
Expand Up @@ -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 }
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion test/cache_test.rb
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions 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)

Expand Down
15 changes: 15 additions & 0 deletions 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
2 changes: 2 additions & 0 deletions test/test_helper.rb
Expand Up @@ -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"
Expand Down

0 comments on commit faed3b0

Please sign in to comment.