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

Delegate Serializer.attributes to Serializer.attribute #1167

Merged
merged 1 commit into from
Sep 18, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions lib/active_model/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,16 @@ def self.inherited(base)

def self.attributes(*attrs)
attrs = attrs.first if attrs.first.class == Array
_attributes.concat(attrs)
_attributes.uniq!

attrs.each do |attr|
define_method attr do
object && object.read_attribute_for_serialization(attr)
end unless method_defined?(attr) || _fragmented.respond_to?(attr)
attribute(attr)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

much cleaner!

end
end

def self.attribute(attr, options = {})
key = options.fetch(:key, attr)
self._attributes_keys[attr] = { key: key } if key != attr
self._attributes << key unless _attributes.include?(key)
_attributes_keys[attr] = { key: key } if key != attr
_attributes << key unless _attributes.include?(key)

ActiveModelSerializers.silence_warnings do
define_method key do
Expand Down