mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 14:56:50 +00:00
Adding optional type for attributes
This commit is contained in:
parent
6780cd3df5
commit
25c564bd6f
@ -73,7 +73,9 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
def attribute(attr, options={})
|
def attribute(attr, options={})
|
||||||
self._attributes = _attributes.merge(attr => options[:key] || attr.to_s.gsub(/\?$/, '').to_sym)
|
self._attributes = _attributes.merge(attr.is_a?(Hash) ? attr : {attr => options[:key] || attr.to_s.gsub(/\?$/, '').to_sym})
|
||||||
|
|
||||||
|
attr = attr.keys[0] if attr.is_a? Hash
|
||||||
|
|
||||||
unless method_defined?(attr)
|
unless method_defined?(attr)
|
||||||
define_method attr do
|
define_method attr do
|
||||||
@ -175,8 +177,12 @@ module ActiveModel
|
|||||||
attrs[key] = column.type
|
attrs[key] = column.type
|
||||||
else
|
else
|
||||||
# Computed attribute (method on serializer or model). We cannot
|
# Computed attribute (method on serializer or model). We cannot
|
||||||
# infer the type, so we put nil.
|
# infer the type, so we put nil, unless specified in the attribute declaration
|
||||||
attrs[key] = nil
|
if name != key
|
||||||
|
attrs[name] = key
|
||||||
|
else
|
||||||
|
attrs[key] = nil
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -493,14 +493,14 @@ class SerializerTest < ActiveModel::TestCase
|
|||||||
def can_edit; end
|
def can_edit; end
|
||||||
def drafts; end
|
def drafts; end
|
||||||
|
|
||||||
attributes :name, :age, :can_edit
|
attributes :name, :age, :can_edit => :boolean
|
||||||
has_many :posts, :serializer => Class.new
|
has_many :posts, :serializer => Class.new
|
||||||
has_many :drafts, :serializer => Class.new
|
has_many :drafts, :serializer => Class.new
|
||||||
has_one :parent, :serializer => Class.new
|
has_one :parent, :serializer => Class.new
|
||||||
end
|
end
|
||||||
|
|
||||||
assert_equal serializer.schema, {
|
assert_equal serializer.schema, {
|
||||||
:attributes => { :name => :string, :age => :integer, :can_edit => nil },
|
:attributes => { :name => :string, :age => :integer, :can_edit => :boolean },
|
||||||
:associations => {
|
:associations => {
|
||||||
:posts => { :has_many => :posts },
|
:posts => { :has_many => :posts },
|
||||||
:drafts => nil,
|
:drafts => nil,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user