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 Serializable
def self.included(base)
base.extend Utils
end
def as_json(options={})
instrument('!serialize') do
if root = options.fetch(:root, json_key)
@ -26,14 +32,8 @@ module ActiveModel
end
end
if RUBY_VERSION >= '2.0'
def namespace
get_namespace && Object.const_get(get_namespace)
end
else
def namespace
get_namespace && get_namespace.safe_constantize
end
get_namespace && Utils._const_get(get_namespace)
end
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,7 +55,6 @@ end
end
attr_reader :key_format
if RUBY_VERSION >= '2.0'
def serializer_for(resource, options = {})
if resource.respond_to?(:to_ary)
if Object.constants.include?(:ArraySerializer)
@ -65,25 +64,12 @@ end
end
else
begin
Object.const_get build_serializer_class(resource, options)
_const_get build_serializer_class(resource, options)
rescue NameError
nil
end
end
end
else
def serializer_for(resource, options = {})
if resource.respond_to?(:to_ary)
if Object.constants.include?(:ArraySerializer)
::ArraySerializer
else
ArraySerializer
end
else
build_serializer_class(resource, options).safe_constantize
end
end
end
attr_accessor :_root, :_attributes, :_associations
alias root _root=