Skip to content

Commit

Permalink
Pass options to associations
Browse files Browse the repository at this point in the history
  • Loading branch information
Tema Bolshakov and Dmitry Myaskovskiy authored and Tema Bolshakov committed Aug 29, 2014
1 parent 258b595 commit 71a43a4
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 16 deletions.
10 changes: 7 additions & 3 deletions lib/active_model/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,15 @@ def attributes(options = {})
end
end

def associations(options = {})
self.class._associations.dup.each_with_object({}) do |(name, value), hash|
def each_association(&block)
self.class._associations.dup.each do |name, options|
association = object.send(name)
serializer_class = ActiveModel::Serializer.serializer_for(association)
hash[name] = serializer_class.new(association)
serializer = serializer_class.new(association)

if block_given?
block.call(name, serializer, options[:options])
end
end
end
end
Expand Down
9 changes: 5 additions & 4 deletions lib/active_model/serializer/adapter/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ class Serializer
class Adapter
class Json < Adapter
def serializable_hash(options = {})
@hash = serializer.attributes
@hash = serializer.attributes(options)

serializer.associations.each do |name, association|
serializer.each_association do |name, association, options|
if association.respond_to?(:each)
@hash[name] = association.map(&:attributes)
array_serializer = association
@hash[name] = array_serializer.map { |item| item.attributes(options) }
else
@hash[name] = association.attributes
@hash[name] = association.attributes(options)
end
end
@hash
Expand Down
16 changes: 9 additions & 7 deletions lib/active_model/serializer/adapter/json_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,33 @@ class JsonApi < Adapter
def serializable_hash(options = {})
@hash = serializer.attributes

serializer.associations.each do |name, association|
serializer.each_association do |name, association, options|
@hash[:links] ||= {}
@hash[:linked] ||= {}

if association.respond_to?(:each)
add_links(name, association)
add_links(name, association, options)
else
add_link(name, association)
add_link(name, association, options)
end
end

@hash
end

def add_links(name, serializers)
def add_links(name, serializers, options)
@hash[:links][name] ||= []
@hash[:linked][name] ||= []
@hash[:links][name] += serializers.map(&:id)
@hash[:linked][name] += serializers.map(&:attributes)
@hash[:linked][name] += serializers.map { |item| item.attributes(options) }
end

def add_link(name, serializer)
def add_link(name, serializer, options)
plural_name = name.to_s.pluralize.to_sym
@hash[:linked][plural_name] ||= []

@hash[:links][name] = serializer.id
@hash[:linked][plural_name].push serializer.attributes
@hash[:linked][plural_name].push serializer.attributes(options)
end
end
end
Expand Down
12 changes: 10 additions & 2 deletions test/serializers/associations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,20 @@ def setup

def test_has_many
assert_equal({comments: {type: :has_many, options: {}}}, @post_serializer.class._associations)
assert_kind_of(ActiveModel::Serializer::ArraySerializer, @post_serializer.associations[:comments])
@post_serializer.each_association do |name, serializer, options|
assert_equal(:comments, name)
assert_equal({}, options)
assert_kind_of(ActiveModel::Serializer.config.array_serializer, serializer)
end
end

def test_has_one
assert_equal({post: {type: :belongs_to, options: {}}}, @comment_serializer.class._associations)
assert_kind_of(PostSerializer, @comment_serializer.associations[:post])
@comment_serializer.each_association do |name, serializer, options|
assert_equal(:post, name)
assert_equal({}, options)
assert_kind_of(PostSerializer, serializer)
end
end
end
end
Expand Down

2 comments on commit 71a43a4

@i-arindam
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi,

Does this commit mean I can serialize specific objects from my association ?

In other words, can I say in my serializer

class UserSerializer < ActiveModel::Serializer
  has_many :comments, -> { where(login: 'xxx') }
end

And @user.to_json will only serialize those comments where login = xxx ?

@steveklabnik
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please ask questions about using AMS on StackOverflow: http://stackoverflow.com/questions/tagged/active-model-serializers

Please sign in to comment.