mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
fix for ruby 1.9.
This commit is contained in:
parent
493041fdff
commit
095bae31d4
@ -1,5 +1,11 @@
|
|||||||
|
require 'active_model/serializable/utils'
|
||||||
|
|
||||||
module ActiveModel
|
module ActiveModel
|
||||||
module Serializable
|
module Serializable
|
||||||
|
def self.included(base)
|
||||||
|
base.extend Utils
|
||||||
|
end
|
||||||
|
|
||||||
def as_json(options={})
|
def as_json(options={})
|
||||||
instrument('!serialize') do
|
instrument('!serialize') do
|
||||||
if root = options.fetch(:root, json_key)
|
if root = options.fetch(:root, json_key)
|
||||||
@ -26,14 +32,8 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if RUBY_VERSION >= '2.0'
|
def namespace
|
||||||
def namespace
|
get_namespace && Utils._const_get(get_namespace)
|
||||||
get_namespace && Object.const_get(get_namespace)
|
|
||||||
end
|
|
||||||
else
|
|
||||||
def namespace
|
|
||||||
get_namespace && get_namespace.safe_constantize
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def embedded_in_root_associations
|
def embedded_in_root_associations
|
||||||
|
|||||||
12
lib/active_model/serializable/utils.rb
Normal file
12
lib/active_model/serializable/utils.rb
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
module ActiveModel
|
||||||
|
module Serializable
|
||||||
|
module Utils
|
||||||
|
extend self
|
||||||
|
|
||||||
|
def _const_get(const)
|
||||||
|
method = RUBY_VERSION >= '2.0' ? :const_get : :qualified_const_get
|
||||||
|
Object.send method, const
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@ -55,32 +55,18 @@ end
|
|||||||
end
|
end
|
||||||
attr_reader :key_format
|
attr_reader :key_format
|
||||||
|
|
||||||
if RUBY_VERSION >= '2.0'
|
def serializer_for(resource, options = {})
|
||||||
def serializer_for(resource, options = {})
|
if resource.respond_to?(:to_ary)
|
||||||
if resource.respond_to?(:to_ary)
|
if Object.constants.include?(:ArraySerializer)
|
||||||
if Object.constants.include?(:ArraySerializer)
|
::ArraySerializer
|
||||||
::ArraySerializer
|
|
||||||
else
|
|
||||||
ArraySerializer
|
|
||||||
end
|
|
||||||
else
|
else
|
||||||
begin
|
ArraySerializer
|
||||||
Object.const_get build_serializer_class(resource, options)
|
|
||||||
rescue NameError
|
|
||||||
nil
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
else
|
||||||
else
|
begin
|
||||||
def serializer_for(resource, options = {})
|
_const_get build_serializer_class(resource, options)
|
||||||
if resource.respond_to?(:to_ary)
|
rescue NameError
|
||||||
if Object.constants.include?(:ArraySerializer)
|
nil
|
||||||
::ArraySerializer
|
|
||||||
else
|
|
||||||
ArraySerializer
|
|
||||||
end
|
|
||||||
else
|
|
||||||
build_serializer_class(resource, options).safe_constantize
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user