Skip to content

Commit

Permalink
Allow specifying attributes with a block
Browse files Browse the repository at this point in the history
Adapted from rails-api#1262
  • Loading branch information
bf4 committed Nov 30, 2015
1 parent f1ae0e8 commit 773f40c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
13 changes: 10 additions & 3 deletions lib/active_model/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -213,20 +213,27 @@ def self.attributes(*attrs)
# class AdminAuthorSerializer < ActiveModel::Serializer
# attributes :id, :recent_edits
# attribute :name, key: :title
# attribute :name do
# "#{object.first_name} #{object.last_name}"
# end
#
# def recent_edits
# object.edits.last(5)
# end
# TODO: remove dynamic method definition
def self.attribute(attr, options = {})
def self.attribute(attr, options = {}, &block)
key = options.fetch(:key, attr)
_attributes_keys[attr] = { key: key } if key != attr

serialized_attributes[key] = Attribute.new(->(object) { object.read_attribute_for_serialization(attr) })
if block_given?
serialized_attributes[key] = Attribute.new(->(instance) { instance.instance_eval(&block) })
else
serialized_attributes[key] = Attribute.new(->(instance) { instance.object.read_attribute_for_serialization(attr) })
end

ActiveModelSerializers.silence_warnings do
define_method key do
serialized_attributes[key].call(object)
serialized_attributes[key].call(self)
end unless method_defined?(key) || _fragmented.respond_to?(attr)
end
end
Expand Down
15 changes: 15 additions & 0 deletions test/serializers/attribute_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,21 @@ def id

assert_equal('custom', hash[:blog][:id])
end

PostWithVirtualAttribute = Class.new(::Model)
class PostWithVirtualAttributeSerializer < ActiveModel::Serializer
attribute :name do
"#{object.first_name} #{object.last_name}"
end
end

def test_virtual_attribute_block
post = PostWithVirtualAttribute.new(first_name: 'Lucas', last_name: 'Hosseini')
hash = serializable(post).serializable_hash
expected = { name: 'Lucas Hosseini' }

assert_equal(expected, hash)
end
end
end
end

0 comments on commit 773f40c

Please sign in to comment.