-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Add inline syntax to override attributes/associations. #1262
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
module ActiveModel | ||
class Serializer | ||
# Defines the attributes to be serialized. | ||
# | ||
module Attributes | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This module is directly extracted from the serializer, no modifications to the code have been made, except for:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed the situation. Reverted to overriding |
||
extend ActiveSupport::Concern | ||
|
||
included do |base| | ||
base.class_attribute :_attributes # @api private : names of attribute methods, @see #attribute | ||
base._attributes ||= [] | ||
base.class_attribute :_attributes_keys # @api private : maps attribute value to explict key name, @see #attribute | ||
base._attributes_keys ||= {} | ||
end | ||
|
||
module ClassMethods | ||
# Serializers inherit _attributes and _attributes_keys. | ||
def inherited(base) | ||
super | ||
base._attributes = _attributes.dup | ||
base._attributes_keys = _attributes_keys.dup | ||
end | ||
|
||
# @example | ||
# class AdminAuthorSerializer < ActiveModel::Serializer | ||
# attributes :id, :name, :recent_edits | ||
def attributes(*attrs) | ||
attrs = attrs.first if attrs.first.class == Array | ||
|
||
attrs.each do |attr| | ||
attribute(attr) | ||
end | ||
end | ||
|
||
# @example | ||
# class AdminAuthorSerializer < ActiveModel::Serializer | ||
# attributes :id, :recent_edits | ||
# attribute :name, key: :title | ||
# | ||
# def recent_edits | ||
# object.edits.last(5) | ||
# end | ||
# | ||
# @example | ||
# class AdminAuthorSerializer < ActiveModel::Serializer | ||
# attribute :name do | ||
# "#{object.first_name} #{object.last_name}" | ||
# end | ||
def attribute(attr, options = {}, &block) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added the |
||
key = options.fetch(:key, attr) | ||
_attributes_keys[attr] = { key: key } if key != attr | ||
_attributes << key unless _attributes.include?(key) | ||
|
||
ActiveModelSerializers.silence_warnings do | ||
define_method key do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this absolutely needs to be document above the method signature with some examples. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed, although this code is already in AMS. I just added the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Never hurts to add documentation. Code pre-existing isn't an excuse to not add docs. :-\ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, as I said, I agree. I was just making clear that I didn't just throw loads of metaprogramming magic without comments. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh :-) 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
if block_given? | ||
instance_eval(&block) | ||
else | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added the |
||
object.read_attribute_for_serialization(attr) | ||
end | ||
end unless method_defined?(key) || _fragmented.respond_to?(attr) | ||
end | ||
end | ||
end | ||
|
||
# Return the +attributes+ of +object+ as presented | ||
# by the serializer. | ||
def attributes | ||
attributes = self.class._attributes.dup | ||
|
||
attributes.each_with_object({}) do |name, hash| | ||
if self.class._fragmented | ||
hash[name] = self.class._fragmented.public_send(name) | ||
else | ||
hash[name] = send(name) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in this case, you might want to test There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd like to see this too. |
||
hash = serializable(post).serializable_hash | ||
expected = { name: 'Lucas Hosseini' } | ||
|
||
assert_equal(expected, hash) | ||
end | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
did you intend to allow instance reader/writers? did you intend to make it inheritable?