mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 14:56:50 +00:00
upgrade hash syntax
This commit is contained in:
parent
b686b73edf
commit
74ba9dc76c
2
Gemfile
2
Gemfile
@ -3,4 +3,4 @@ source 'https://rubygems.org'
|
|||||||
# Specify gem dependencies in active_model_serializers.gemspec
|
# Specify gem dependencies in active_model_serializers.gemspec
|
||||||
gemspec
|
gemspec
|
||||||
|
|
||||||
gem "coveralls", :require => false
|
gem "coveralls", require: false
|
||||||
|
|||||||
2
Rakefile
2
Rakefile
@ -15,4 +15,4 @@ task :bench do
|
|||||||
load 'bench/perf.rb'
|
load 'bench/perf.rb'
|
||||||
end
|
end
|
||||||
|
|
||||||
task :default => :test
|
task default: :test
|
||||||
|
|||||||
@ -52,7 +52,7 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
serializer ||= DefaultSerializer
|
serializer ||= DefaultSerializer
|
||||||
|
|
||||||
serializable = serializer.new(item, options.merge(:root => nil))
|
serializable = serializer.new(item, options.merge(root: nil))
|
||||||
|
|
||||||
if serializable.respond_to?(:serializable_hash)
|
if serializable.respond_to?(:serializable_hash)
|
||||||
serializable.serializable_hash
|
serializable.serializable_hash
|
||||||
|
|||||||
@ -13,7 +13,7 @@ module ActiveModel
|
|||||||
# it expects two objects as arguments, a resource and options. For example,
|
# it expects two objects as arguments, a resource and options. For example,
|
||||||
# one may do in a controller:
|
# one may do in a controller:
|
||||||
#
|
#
|
||||||
# PostSerializer.new(@post, :scope => current_user).to_json
|
# PostSerializer.new(@post, scope: current_user).to_json
|
||||||
#
|
#
|
||||||
# The object to be serialized is the +@post+ and the current user is passed
|
# The object to be serialized is the +@post+ and the current user is passed
|
||||||
# in for authorization purposes.
|
# in for authorization purposes.
|
||||||
@ -30,7 +30,7 @@ module ActiveModel
|
|||||||
#
|
#
|
||||||
# def attributes
|
# def attributes
|
||||||
# hash = super
|
# hash = super
|
||||||
# hash.merge!(:email => post.email) if author?
|
# hash.merge!(email: post.email) if author?
|
||||||
# hash
|
# hash
|
||||||
# end
|
# end
|
||||||
#
|
#
|
||||||
@ -46,7 +46,7 @@ module ActiveModel
|
|||||||
include ActiveModel::Serializer::Caching
|
include ActiveModel::Serializer::Caching
|
||||||
|
|
||||||
INCLUDE_METHODS = {}
|
INCLUDE_METHODS = {}
|
||||||
INSTRUMENT = { :serialize => :"serialize.serializer", :associations => :"associations.serializer" }
|
INSTRUMENT = { serialize: :"serialize.serializer", associations: :"associations.serializer" }
|
||||||
|
|
||||||
class IncludeError < StandardError
|
class IncludeError < StandardError
|
||||||
attr_reader :source, :association
|
attr_reader :source, :association
|
||||||
@ -86,7 +86,7 @@ module ActiveModel
|
|||||||
|
|
||||||
attrs.each do |attr|
|
attrs.each do |attr|
|
||||||
if Hash === attr
|
if Hash === attr
|
||||||
attr.each {|attr_real, key| attribute attr_real, :key => key }
|
attr.each {|attr_real, key| attribute(attr_real, key: key) }
|
||||||
else
|
else
|
||||||
attribute attr
|
attribute attr
|
||||||
end
|
end
|
||||||
@ -172,20 +172,20 @@ module ActiveModel
|
|||||||
#
|
#
|
||||||
# The +attributes+ hash looks like this:
|
# The +attributes+ hash looks like this:
|
||||||
#
|
#
|
||||||
# { :name => :string, :age => :integer }
|
# { name: :string, age: :integer }
|
||||||
#
|
#
|
||||||
# The +associations+ hash looks like this:
|
# The +associations+ hash looks like this:
|
||||||
# { :posts => { :has_many => :posts } }
|
# { posts: { has_many: :posts } }
|
||||||
#
|
#
|
||||||
# If :key is used:
|
# If :key is used:
|
||||||
#
|
#
|
||||||
# class PostsSerializer < ActiveModel::Serializer
|
# class PostsSerializer < ActiveModel::Serializer
|
||||||
# has_many :posts, :key => :my_posts
|
# has_many :posts, key: :my_posts
|
||||||
# end
|
# end
|
||||||
#
|
#
|
||||||
# the hash looks like this:
|
# the hash looks like this:
|
||||||
#
|
#
|
||||||
# { :my_posts => { :has_many => :posts }
|
# { my_posts: { has_many: :posts }
|
||||||
#
|
#
|
||||||
# This information is extracted from the serializer's model class,
|
# This information is extracted from the serializer's model class,
|
||||||
# which is provided by +SerializerClass.model_class+.
|
# which is provided by +SerializerClass.model_class+.
|
||||||
@ -232,7 +232,7 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
{ :attributes => attrs, :associations => associations }
|
{ attributes: attrs, associations: associations }
|
||||||
end
|
end
|
||||||
|
|
||||||
# The model class associated with this serializer.
|
# The model class associated with this serializer.
|
||||||
@ -244,7 +244,7 @@ module ActiveModel
|
|||||||
#
|
#
|
||||||
# embed :objects # Embed associations as full objects
|
# embed :objects # Embed associations as full objects
|
||||||
# embed :ids # Embed only the association ids
|
# embed :ids # Embed only the association ids
|
||||||
# embed :ids, :include => true # Embed the association ids and include objects in the root
|
# embed :ids, include: true # Embed the association ids and include objects in the root
|
||||||
#
|
#
|
||||||
def embed(type, options={})
|
def embed(type, options={})
|
||||||
self._embed = type
|
self._embed = type
|
||||||
@ -323,7 +323,7 @@ module ActiveModel
|
|||||||
# Returns a json representation of the serializable
|
# Returns a json representation of the serializable
|
||||||
# object including the root.
|
# object including the root.
|
||||||
def as_json(args={})
|
def as_json(args={})
|
||||||
super(:root => args.fetch(:root, options.fetch(:root, root_name)))
|
super(root: args.fetch(:root, options.fetch(:root, root_name)))
|
||||||
end
|
end
|
||||||
|
|
||||||
def serialize_object
|
def serialize_object
|
||||||
@ -451,8 +451,8 @@ module ActiveModel
|
|||||||
|
|
||||||
def default_embed_options
|
def default_embed_options
|
||||||
{
|
{
|
||||||
:embed => _embed,
|
embed: _embed,
|
||||||
:include => _root_embed
|
include: _root_embed
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -12,7 +12,7 @@ module ActiveModel
|
|||||||
# embed: Define how associations should be embedded.
|
# embed: Define how associations should be embedded.
|
||||||
# - :objects # Embed associations as full objects.
|
# - :objects # Embed associations as full objects.
|
||||||
# - :ids # Embed only the association ids.
|
# - :ids # Embed only the association ids.
|
||||||
# - :ids, :include => true # Embed the association ids and include objects in the root.
|
# - :ids, include: true # Embed the association ids and include objects in the root.
|
||||||
#
|
#
|
||||||
# include: Used in conjunction with embed :ids. Includes the objects in the root.
|
# include: Used in conjunction with embed :ids. Includes the objects in the root.
|
||||||
#
|
#
|
||||||
@ -158,8 +158,8 @@ module ActiveModel
|
|||||||
|
|
||||||
if polymorphic?
|
if polymorphic?
|
||||||
{
|
{
|
||||||
:type => polymorphic_key,
|
type: polymorphic_key,
|
||||||
:id => id
|
id: id
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
id
|
id
|
||||||
|
|||||||
@ -74,8 +74,8 @@ Array.send(:include, ActiveModel::ArraySerializerSupport)
|
|||||||
Set.send(:include, ActiveModel::ArraySerializerSupport)
|
Set.send(:include, ActiveModel::ArraySerializerSupport)
|
||||||
|
|
||||||
{
|
{
|
||||||
:active_record => 'ActiveRecord::Relation',
|
active_record: 'ActiveRecord::Relation',
|
||||||
:mongoid => 'Mongoid::Criteria'
|
mongoid: 'Mongoid::Criteria'
|
||||||
}.each do |orm, rel_class|
|
}.each do |orm, rel_class|
|
||||||
ActiveSupport.on_load(orm) do
|
ActiveSupport.on_load(orm) do
|
||||||
include ActiveModel::SerializerSupport
|
include ActiveModel::SerializerSupport
|
||||||
|
|||||||
@ -2,11 +2,11 @@ module Rails
|
|||||||
module Generators
|
module Generators
|
||||||
class SerializerGenerator < NamedBase
|
class SerializerGenerator < NamedBase
|
||||||
source_root File.expand_path("../templates", __FILE__)
|
source_root File.expand_path("../templates", __FILE__)
|
||||||
check_class_collision :suffix => "Serializer"
|
check_class_collision suffix: "Serializer"
|
||||||
|
|
||||||
argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
|
argument :attributes, type: :array, default: [], banner: "field:type field:type"
|
||||||
|
|
||||||
class_option :parent, :type => :string, :desc => "The parent class for the generated serializer"
|
class_option :parent, type: :string, desc: "The parent class for the generated serializer"
|
||||||
|
|
||||||
def create_serializer_file
|
def create_serializer_file
|
||||||
template 'serializer.rb', File.join('app/serializers', class_path, "#{file_name}_serializer.rb")
|
template 'serializer.rb', File.join('app/serializers', class_path, "#{file_name}_serializer.rb")
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user