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 authored and João M. D. Moura committed Jan 19, 2016
1 parent b836f05 commit 68a34d6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
15 changes: 11 additions & 4 deletions lib/active_model/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,19 +112,26 @@ def self.attributes(*attrs)
# attributes :id, :recent_edits
# attribute :name, key: :title
#
# attribute :full_name do
# "#{object.first_name} #{object.last_name}"
# end
#
# def recent_edits
# object.edits.last(5)
# end
def self.attribute(attr, options = {})
def self.attribute(attr, options = {}, &block)
key = options.fetch(:key, attr)
_attributes_keys[attr] = { key: key } if key != attr
_attributes << key unless _attributes.include?(key)

serialized_attributes[key] = ->(object) { object.read_attribute_for_serialization(attr) }
if block_given?
serialized_attributes[key] = ->(instance) { instance.instance_eval(&block) }
else
serialized_attributes[key] = ->(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 68a34d6

Please sign in to comment.