Skip to content
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

Added hidden_attributes method for default hidden attributes. #537

Closed
wants to merge 1 commit 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 14 additions & 1 deletion lib/active_model/serializer.rb
Expand Up @@ -15,6 +15,7 @@ class << self
def inherited(base)
base._root = _root
base._attributes = (_attributes || []).dup
base._hidden_attributes = (_hidden_attributes || []).dup
base._associations = (_associations || {}).dup
end

Expand Down Expand Up @@ -60,7 +61,7 @@ def serializer_for(resource)
end
end

attr_accessor :_root, :_attributes, :_associations
attr_accessor :_root, :_attributes, :_hidden_attributes, :_associations
alias root _root=
alias root= _root=

Expand All @@ -86,6 +87,11 @@ def has_many(*attrs)
associate(Association::HasMany, *attrs)
end

def hidden_attributes(*attrs)
@_hidden_attributes.concat attrs
attributes(*attrs)
end

private

def associate(klass, *attrs)
Expand All @@ -110,6 +116,7 @@ def initialize(object, options={})
@wrap_in_array = options[:_wrap_in_array]
@only = Array(options[:only]) if options[:only]
@except = Array(options[:except]) if options[:except]
@expose = Array(options[:expose]) if options[:expose]
end
attr_accessor :object, :scope, :root, :meta_key, :meta

Expand All @@ -127,6 +134,10 @@ def attributes
end
end

def hidden_attributes
self.class._hidden_attributes
end

def associations
associations = self.class._associations
included_associations = filter(associations.keys)
Expand All @@ -142,6 +153,8 @@ def associations
end

def filter(keys)
@expose ||= []
hidden_attributes.each { |item| keys.delete(item) unless @expose.include?(item) }
if @only
keys & @only
elsif @except
Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/poro.rb
Expand Up @@ -35,6 +35,9 @@ def comments
class Comment < Model
end

class Desk < Model
end

###
## Serializers
###
Expand Down Expand Up @@ -62,3 +65,8 @@ class PostSerializer < ActiveModel::Serializer
class CommentSerializer < ActiveModel::Serializer
attributes :content
end

class DeskSerializer < ActiveModel::Serializer
attributes :drawer, :lamp
hidden_attributes :secret_document
end
15 changes: 15 additions & 0 deletions test/unit/active_model/serializer/filter_test.rb
Expand Up @@ -5,6 +5,7 @@ class Serializer
class FilterOptionsTest < Minitest::Test
def setup
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
@desk = Desk.new({ drawer: 'My drawer', lamp: 'My lamp', secret_document: 'Some secret document' })
end

def test_only_option
Expand All @@ -20,6 +21,20 @@ def test_except_option
'profile' => { name: 'Name 1', description: 'Description 1' }
}, @profile_serializer.as_json)
end

def test_not_expose_hidden_attribute
desk_serializer = DeskSerializer.new(@desk)
assert_equal({
'desk' => { drawer: 'My drawer', lamp: 'My lamp' }
}, desk_serializer.as_json)
end

def test_expose_hidden_attribute
desk_serializer = DeskSerializer.new(@desk, expose: :secret_document)
assert_equal({
'desk' => { drawer: 'My drawer', lamp: 'My lamp', secret_document: 'Some secret document' }
}, desk_serializer.as_json)
end
end

class FilterAttributesTest < Minitest::Test
Expand Down