fix for ruby 1.9.

This commit is contained in:
Theodore Konukhov 2014-08-29 19:38:53 +02:00
parent 493041fdff
commit 095bae31d4
3 changed files with 30 additions and 32 deletions

View File

@ -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

View 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

View File

@ -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