mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 15:23:06 +00:00
Add support for customizing fields
This commit is contained in:
@@ -246,12 +246,16 @@ module ActiveModel
|
||||
self._attributes = _attributes.dup
|
||||
|
||||
attrs.each do |attr|
|
||||
self._attributes[attr] = attr
|
||||
attribute attr
|
||||
end
|
||||
end
|
||||
|
||||
def attribute(attr, options={})
|
||||
self._attributes = _attributes.merge(attr => options[:key] || attr)
|
||||
|
||||
unless method_defined?(attr)
|
||||
class_eval "def #{attr}() object.read_attribute_for_serialization(:#{attr}) end", __FILE__, __LINE__
|
||||
end
|
||||
end
|
||||
|
||||
def associate(klass, attrs) #:nodoc:
|
||||
@@ -490,11 +494,15 @@ module ActiveModel
|
||||
hash = {}
|
||||
|
||||
_attributes.each do |name,key|
|
||||
hash[key] = @object.read_attribute_for_serialization(name)
|
||||
hash[key] = read_attribute_for_serialization(name)
|
||||
end
|
||||
|
||||
hash
|
||||
end
|
||||
|
||||
def read_attribute_for_serialization(name)
|
||||
send name
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user