mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
Rename :as to :key, as it's confusing
This commit is contained in:
parent
5054d2a449
commit
ca7786c2e0
@ -70,7 +70,7 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
def key
|
def key
|
||||||
options[:as] || name
|
options[:key] || name
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -126,10 +126,10 @@ module ActiveModel
|
|||||||
class_eval "def #{attr}() object.#{attr} end", __FILE__, __LINE__
|
class_eval "def #{attr}() object.#{attr} end", __FILE__, __LINE__
|
||||||
end
|
end
|
||||||
|
|
||||||
# if :as is specified without :serializer, then use conventions
|
# if :key is specified without :serializer, then use conventions
|
||||||
# to determine the serializer
|
# to determine the serializer
|
||||||
if options[:as] && !options[:serializer]
|
if options[:key] && !options[:serializer]
|
||||||
options[:serializer] = const_get("#{options[:as].to_s.camelize.singularize}Serializer")
|
options[:serializer] = const_get("#{options[:key].to_s.camelize.singularize}Serializer")
|
||||||
else
|
else
|
||||||
options[:serializer] ||= const_get("#{attr.to_s.camelize}Serializer")
|
options[:serializer] ||= const_get("#{attr.to_s.camelize}Serializer")
|
||||||
end
|
end
|
||||||
@ -171,10 +171,10 @@ module ActiveModel
|
|||||||
#
|
#
|
||||||
# { :posts => { :has_many => :posts } }
|
# { :posts => { :has_many => :posts } }
|
||||||
#
|
#
|
||||||
# If :as is used:
|
# If :key is used:
|
||||||
#
|
#
|
||||||
# class PostsSerializer < ActiveModel::Serializer
|
# class PostsSerializer < ActiveModel::Serializer
|
||||||
# has_many :my_posts, :as => :posts
|
# has_many :posts, :key => :my_posts
|
||||||
# end
|
# end
|
||||||
#
|
#
|
||||||
# the hash looks like this:
|
# the hash looks like this:
|
||||||
@ -198,7 +198,7 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
associations = _associations.inject({}) do |hash, association|
|
associations = _associations.inject({}) do |hash, association|
|
||||||
model_association = klass.reflect_on_association(association.key)
|
model_association = klass.reflect_on_association(association.name)
|
||||||
hash.merge association.key => { model_association.macro => model_association.name }
|
hash.merge association.key => { model_association.macro => model_association.name }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -440,8 +440,8 @@ class SerializerTest < ActiveModel::TestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
class CustomBlogSerializer < ActiveModel::Serializer
|
class CustomBlogSerializer < ActiveModel::Serializer
|
||||||
has_many :public_posts, :as => :posts, :serializer => PostSerializer
|
has_many :public_posts, :key => :posts, :serializer => PostSerializer
|
||||||
has_one :public_user, :as => :user, :serializer => UserSerializer
|
has_one :public_user, :key => :user, :serializer => UserSerializer
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_associations_with_as
|
def test_associations_with_as
|
||||||
@ -478,8 +478,8 @@ class SerializerTest < ActiveModel::TestCase
|
|||||||
const_set(:UserSerializer, UserSerializer)
|
const_set(:UserSerializer, UserSerializer)
|
||||||
const_set(:PostSerializer, PostSerializer)
|
const_set(:PostSerializer, PostSerializer)
|
||||||
|
|
||||||
has_many :public_posts, :as => :posts
|
has_many :public_posts, :key => :posts
|
||||||
has_one :public_user, :as => :user
|
has_one :public_user, :key => :user
|
||||||
end
|
end
|
||||||
|
|
||||||
posts = [
|
posts = [
|
||||||
@ -559,8 +559,8 @@ class SerializerTest < ActiveModel::TestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
attributes :name, :age
|
attributes :name, :age
|
||||||
has_many :my_posts, :as => :posts, :serializer => Class.new
|
has_many :posts, :key => :my_posts, :serializer => Class.new
|
||||||
has_one :my_parent, :as => :parent, :serializer => Class.new
|
has_one :parent, :key => :my_parent, :serializer => Class.new
|
||||||
end
|
end
|
||||||
|
|
||||||
assert_equal serializer.schema, {
|
assert_equal serializer.schema, {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user