mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 23:06:50 +00:00
Allow attribute aliasing
This commit is contained in:
parent
7dd3bdaca9
commit
57ef67dcb4
@ -103,7 +103,7 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
class_attribute :_attributes
|
class_attribute :_attributes
|
||||||
self._attributes = Set.new
|
self._attributes = {}
|
||||||
|
|
||||||
class_attribute :_associations
|
class_attribute :_associations
|
||||||
self._associations = []
|
self._associations = []
|
||||||
@ -116,7 +116,15 @@ module ActiveModel
|
|||||||
class << self
|
class << self
|
||||||
# Define attributes to be used in the serialization.
|
# Define attributes to be used in the serialization.
|
||||||
def attributes(*attrs)
|
def attributes(*attrs)
|
||||||
self._attributes += attrs
|
self._attributes = _attributes.dup
|
||||||
|
|
||||||
|
attrs.each do |attr|
|
||||||
|
self._attributes[attr] = attr
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def attribute(attr, options={})
|
||||||
|
self._attributes = _attributes.merge(attr => options[:key] || attr)
|
||||||
end
|
end
|
||||||
|
|
||||||
def associate(klass, attrs) #:nodoc:
|
def associate(klass, attrs) #:nodoc:
|
||||||
@ -192,9 +200,9 @@ module ActiveModel
|
|||||||
klass = model_class
|
klass = model_class
|
||||||
columns = klass.columns_hash
|
columns = klass.columns_hash
|
||||||
|
|
||||||
attrs = _attributes.inject({}) do |hash, name|
|
attrs = _attributes.inject({}) do |hash, (name,key)|
|
||||||
column = columns[name]
|
column = columns[name]
|
||||||
hash.merge name => column[:type]
|
hash.merge key => column[:type]
|
||||||
end
|
end
|
||||||
|
|
||||||
associations = _associations.inject({}) do |hash, association|
|
associations = _associations.inject({}) do |hash, association|
|
||||||
@ -299,8 +307,8 @@ module ActiveModel
|
|||||||
def attributes
|
def attributes
|
||||||
hash = {}
|
hash = {}
|
||||||
|
|
||||||
_attributes.each do |name|
|
_attributes.each do |name,key|
|
||||||
hash[name] = @object.read_attribute_for_serialization(name)
|
hash[key] = @object.read_attribute_for_serialization(name)
|
||||||
end
|
end
|
||||||
|
|
||||||
hash
|
hash
|
||||||
|
|||||||
@ -509,6 +509,26 @@ class SerializerTest < ActiveModel::TestCase
|
|||||||
}, serializer.as_json)
|
}, serializer.as_json)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_attribute_key
|
||||||
|
serializer_class = Class.new(ActiveModel::Serializer) do
|
||||||
|
root :user
|
||||||
|
|
||||||
|
attribute :first_name, :key => :firstName
|
||||||
|
attribute :last_name, :key => :lastName
|
||||||
|
attribute :password
|
||||||
|
end
|
||||||
|
|
||||||
|
serializer = serializer_class.new(User.new, nil)
|
||||||
|
|
||||||
|
assert_equal({
|
||||||
|
:user => {
|
||||||
|
:firstName => "Jose",
|
||||||
|
:lastName => "Valim",
|
||||||
|
:password => "oh noes yugive my password"
|
||||||
|
}
|
||||||
|
}, serializer.as_json)
|
||||||
|
end
|
||||||
|
|
||||||
def setup_model
|
def setup_model
|
||||||
Class.new do
|
Class.new do
|
||||||
class << self
|
class << self
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user