Skip to content

Fixes filter method being called multiple times. #460

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,15 +263,15 @@ authorization context to your serializer. By default, the context
is the current user of your application, but this
[can be customized](#customizing-scope).

Serializers provides a method named `filter` used to determine what
Serializers provides a method named `filter_attributes` and `filter_associations` used to determine what
attributes and associations should be included in the output. This is
typically used to customize output based on `current_user`. For example:

```ruby
class PostSerializer < ActiveModel::Serializer
attributes :id, :title, :body, :author

def filter(keys)
def filter_attributes(keys)
if scope.admin?
keys
else
Expand Down
12 changes: 8 additions & 4 deletions lib/active_model/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,14 @@ def json_key
end

def attributes
filter(self.class._attributes.dup).each_with_object({}) do |name, hash|
filter_attributes(self.class._attributes.dup).each_with_object({}) do |name, hash|
hash[name] = send(name)
end
end

def associations
associations = self.class._associations
included_associations = filter(associations.keys)
included_associations = filter_associations(associations.keys)
associations.each_with_object({}) do |(name, association), hash|
if included_associations.include? name
if association.embed_ids?
Expand All @@ -139,13 +139,17 @@ def associations
end
end

def filter(keys)
def filter_attributes(keys)
keys
end

def filter_associations(keys)
keys
end

def embedded_in_root_associations
associations = self.class._associations
included_associations = filter(associations.keys)
included_associations = filter_associations(associations.keys)
associations.each_with_object({}) do |(name, association), hash|
if included_associations.include? name
if association.embed_in_root?
Expand Down
29 changes: 26 additions & 3 deletions test/unit/active_model/serializer/filter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def setup
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
@profile_serializer = ProfileSerializer.new(@profile)
@profile_serializer.instance_eval do
def filter(keys)
def filter_attributes(keys)
keys - [:description]
end
end
Expand All @@ -29,8 +29,12 @@ def setup
@post = Post.new({ title: 'Title 1', body: 'Body 1', date: '1/1/2000' })
@post_serializer = PostSerializer.new(@post)
@post_serializer.instance_eval do
def filter(keys)
keys - [:body, :comments]
def filter_attributes(keys)
keys & [:title]
end

def filter_associations(keys)
keys - [:comments]
end
end
end
Expand All @@ -44,6 +48,25 @@ def test_filtered_associations_serialization
'post' => { title: 'Title 1' }
}, @post_serializer.as_json)
end

def test_filter_associations_and_filter_attributes
@post_serializer.instance_eval do

def filter_attributes(keys)
keys & [:title]
end

def filter_associations(keys)
keys
end
end

assert_equal({
title: 'Title 1', 'comment_ids' => @post.comments.map { |c| c.object_id }
}, @post_serializer.serializable_hash)

end
end

end
end