Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into add-activerecord-…
Browse files Browse the repository at this point in the history
…fixtures
  • Loading branch information
beauby committed Aug 31, 2015
2 parents c3e3073 + 8d3a89e commit 7af986c
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 10 deletions.
2 changes: 1 addition & 1 deletion lib/active_model/serializer/association.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Serializer
# @param [Hash{Symbol => Object}] options
#
# @example
# Association.new(:comments, CommentSummarySerializer, embed: :ids)
# Association.new(:comments, CommentSummarySerializer)
#
Association = Struct.new(:name, :serializer, :options) do

Expand Down
20 changes: 17 additions & 3 deletions lib/active_model/serializer/lint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,17 @@ def test_serializable_hash
# Typically, it is implemented by including ActiveModel::Serialization.
def test_read_attribute_for_serialization
assert_respond_to resource, :read_attribute_for_serialization, "The resource should respond to read_attribute_for_serialization"
assert_equal resource.method(:read_attribute_for_serialization).arity, 1
actual_arity = resource.method(:read_attribute_for_serialization).arity
if defined?(::Rubinius)
# 1 for def read_attribute_for_serialization(name); end
# -2 for alias :read_attribute_for_serialization :send for rbx because :shrug:
assert_includes [1, -2], actual_arity, "expected #{actual_arity.inspect} to be 1 or -2"
else
# using absolute value since arity is:
# 1 for def read_attribute_for_serialization(name); end
# -1 for alias :read_attribute_for_serialization :send
assert_includes [1, -1], actual_arity, "expected #{actual_arity.inspect} to be 1 or -1"
end
end

# Passes if the object responds to <tt>as_json</tt> and if it takes
Expand Down Expand Up @@ -68,15 +78,19 @@ def test_to_json
end

# Passes if the object responds to <tt>cache_key</tt> and if it takes no
# arguments.
# arguments (Rails 4.0) or a splat (Rails 4.1+).
# Fails otherwise.
#
# <tt>cache_key</tt> returns a (self-expiring) unique key for the object,
# which is used by the adapter.
# It is not required unless caching is enabled.
def test_cache_key
assert_respond_to resource, :cache_key
assert_equal resource.method(:cache_key).arity, 0
actual_arity = resource.method(:cache_key).arity
# using absolute value since arity is:
# 0 for Rails 4.1+, *timestamp_names
# -1 for Rails 4.0, no arguments
assert_includes [-1, 0], actual_arity, "expected #{actual_arity.inspect} to be 0 or -1"
end

# Passes if the object responds to <tt>id</tt> and if it takes no
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/poro.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ def custom_options
cache key:'writer', skip_digest: true
attributes :id, :name

has_many :posts, embed: :ids
has_many :roles, embed: :ids
has_many :posts
has_many :roles
has_one :bio
end

Expand Down
4 changes: 2 additions & 2 deletions test/serializers/association_macros_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class AssociationMacrosTest < Minitest::Test
AuthorSummarySerializer = Class.new
class AssociationsTestSerializer < Serializer
belongs_to :author, serializer: AuthorSummarySerializer
has_many :comments, embed: :ids
has_many :comments
has_one :category
end

Expand All @@ -21,7 +21,7 @@ def test_has_one_defines_reflection
end

def test_has_many_defines_reflection
has_many_reflection = HasManyReflection.new(:comments, embed: :ids)
has_many_reflection = HasManyReflection.new(:comments, {})

assert_includes(@reflections, has_many_reflection)
end
Expand Down
4 changes: 2 additions & 2 deletions test/serializers/associations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ def test_has_many_and_has_one

case key
when :posts
assert_equal({ embed: :ids }, options)
assert_equal({}, options)
assert_kind_of(ActiveModel::Serializer.config.array_serializer, serializer)
when :bio
assert_equal({}, options)
assert_nil serializer
when :roles
assert_equal({ embed: :ids }, options)
assert_equal({}, options)
assert_kind_of(ActiveModel::Serializer.config.array_serializer, serializer)
else
flunk "Unknown association: #{key}"
Expand Down

0 comments on commit 7af986c

Please sign in to comment.