mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
define include_XXX? methods, which can be overridden to conditionally include attributes and associations
This commit is contained in:
parent
68dc57eb73
commit
42221a6140
@ -328,6 +328,8 @@ module ActiveModel
|
|||||||
object.read_attribute_for_serialization(attr.to_sym)
|
object.read_attribute_for_serialization(attr.to_sym)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
define_include_method attr
|
||||||
end
|
end
|
||||||
|
|
||||||
def associate(klass, attrs) #:nodoc:
|
def associate(klass, attrs) #:nodoc:
|
||||||
@ -341,10 +343,21 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
define_include_method attr
|
||||||
|
|
||||||
self._associations[attr] = klass.refine(attr, options)
|
self._associations[attr] = klass.refine(attr, options)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def define_include_method(name)
|
||||||
|
method = "include_#{name}?".to_sym
|
||||||
|
unless method_defined?(method)
|
||||||
|
define_method method do
|
||||||
|
true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# Defines an association in the object should be rendered.
|
# Defines an association in the object should be rendered.
|
||||||
#
|
#
|
||||||
# The serializer object should implement the association name
|
# The serializer object should implement the association name
|
||||||
@ -486,10 +499,14 @@ module ActiveModel
|
|||||||
|
|
||||||
def include_associations!
|
def include_associations!
|
||||||
_associations.each_key do |name|
|
_associations.each_key do |name|
|
||||||
include! name
|
include!(name) if include?(name)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def include?(name)
|
||||||
|
send "include_#{name}?".to_sym
|
||||||
|
end
|
||||||
|
|
||||||
def include!(name, options={})
|
def include!(name, options={})
|
||||||
# Make sure that if a special options[:hash] was passed in, we generate
|
# Make sure that if a special options[:hash] was passed in, we generate
|
||||||
# a new unique values hash and don't clobber the original. If the hash
|
# a new unique values hash and don't clobber the original. If the hash
|
||||||
@ -569,7 +586,7 @@ module ActiveModel
|
|||||||
hash = {}
|
hash = {}
|
||||||
|
|
||||||
_attributes.each do |name,key|
|
_attributes.each do |name,key|
|
||||||
hash[key] = read_attribute_for_serialization(name)
|
hash[key] = read_attribute_for_serialization(name) if include?(name)
|
||||||
end
|
end
|
||||||
|
|
||||||
hash
|
hash
|
||||||
|
|||||||
@ -227,6 +227,62 @@ class SerializerTest < ActiveModel::TestCase
|
|||||||
}, post_serializer.as_json)
|
}, post_serializer.as_json)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class PostWithMultipleConditionalsSerializer < ActiveModel::Serializer
|
||||||
|
root :post
|
||||||
|
attributes :title, :body, :author
|
||||||
|
has_many :comments, :serializer => CommentSerializer
|
||||||
|
|
||||||
|
def include_comments?
|
||||||
|
!object.comments_disabled
|
||||||
|
end
|
||||||
|
|
||||||
|
def include_author?
|
||||||
|
scope.super_user?
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_conditionally_included_associations_and_attributes
|
||||||
|
user = User.new
|
||||||
|
|
||||||
|
post = Post.new(:title => "New Post", :body => "Body of new post", :author => 'Sausage King', :email => "tenderlove@tenderlove.com")
|
||||||
|
comments = [Comment.new(:title => "Comment1"), Comment.new(:title => "Comment2")]
|
||||||
|
post.comments = comments
|
||||||
|
|
||||||
|
post_serializer = PostWithMultipleConditionalsSerializer.new(post, :scope => user)
|
||||||
|
|
||||||
|
# comments enabled
|
||||||
|
post.comments_disabled = false
|
||||||
|
assert_equal({
|
||||||
|
:post => {
|
||||||
|
:title => "New Post",
|
||||||
|
:body => "Body of new post",
|
||||||
|
:comments => [
|
||||||
|
{ :title => "Comment1" },
|
||||||
|
{ :title => "Comment2" }
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}, post_serializer.as_json)
|
||||||
|
|
||||||
|
# comments disabled
|
||||||
|
post.comments_disabled = true
|
||||||
|
assert_equal({
|
||||||
|
:post => {
|
||||||
|
:title => "New Post",
|
||||||
|
:body => "Body of new post"
|
||||||
|
}
|
||||||
|
}, post_serializer.as_json)
|
||||||
|
|
||||||
|
# superuser - should see author
|
||||||
|
user.superuser = true
|
||||||
|
assert_equal({
|
||||||
|
:post => {
|
||||||
|
:title => "New Post",
|
||||||
|
:body => "Body of new post",
|
||||||
|
:author => "Sausage King"
|
||||||
|
}
|
||||||
|
}, post_serializer.as_json)
|
||||||
|
end
|
||||||
|
|
||||||
class Blog < Model
|
class Blog < Model
|
||||||
attr_accessor :author
|
attr_accessor :author
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user