aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/lib
diff options
context:
space:
mode:
authorJosé Valim <jose.valim@gmail.com>2011-11-23 23:45:27 +0000
committerJosé Valim <jose.valim@gmail.com>2011-11-23 23:45:27 +0000
commit7fcc8c0a1f38c77b12cb6ffe81fb2887e6c60b85 (patch)
treeac081db672326a11910e9c46d93c9675c83c2e87 /activemodel/lib
parent6da52c617e2a075b9d9e7142786f316d8f2c7930 (diff)
downloadrails-7fcc8c0a1f38c77b12cb6ffe81fb2887e6c60b85.tar.gz
rails-7fcc8c0a1f38c77b12cb6ffe81fb2887e6c60b85.tar.bz2
rails-7fcc8c0a1f38c77b12cb6ffe81fb2887e6c60b85.zip
Rely solely on active_model_serializer and remove the fancy constant lookup.
Diffstat (limited to 'activemodel/lib')
-rw-r--r--activemodel/lib/active_model/serializable.rb12
-rw-r--r--activemodel/lib/active_model/serializer.rb45
2 files changed, 14 insertions, 43 deletions
diff --git a/activemodel/lib/active_model/serializable.rb b/activemodel/lib/active_model/serializable.rb
index 769e934dbe..70e27c5683 100644
--- a/activemodel/lib/active_model/serializable.rb
+++ b/activemodel/lib/active_model/serializable.rb
@@ -1,6 +1,7 @@
require 'active_support/core_ext/hash/except'
require 'active_support/core_ext/hash/slice'
require 'active_support/core_ext/array/wrap'
+require 'active_support/core_ext/string/inflections'
module ActiveModel
# == Active Model Serializable
@@ -72,11 +73,10 @@ module ActiveModel
autoload :JSON, "active_model/serializable/json"
autoload :XML, "active_model/serializable/xml"
- include ActiveModel::Serializer::Scope
-
module ClassMethods #:nodoc:
- def _model_serializer
- @_model_serializer ||= ActiveModel::Serializer::Finder.find(self, self)
+ def active_model_serializer
+ return @active_model_serializer if defined?(@active_model_serializer)
+ @active_model_serializer = "#{self.name}Serializer".safe_constantize
end
end
@@ -108,8 +108,8 @@ module ActiveModel
end
# Returns a model serializer for this object considering its namespace.
- def model_serializer
- self.class._model_serializer
+ def active_model_serializer
+ self.class.active_model_serializer
end
private
diff --git a/activemodel/lib/active_model/serializer.rb b/activemodel/lib/active_model/serializer.rb
index a541a1053d..5478da15c8 100644
--- a/activemodel/lib/active_model/serializer.rb
+++ b/activemodel/lib/active_model/serializer.rb
@@ -1,7 +1,6 @@
require "active_support/core_ext/class/attribute"
require "active_support/core_ext/string/inflections"
require "active_support/core_ext/module/anonymous"
-require "active_support/core_ext/module/introspection"
require "set"
module ActiveModel
@@ -15,7 +14,7 @@ module ActiveModel
def serializable_array
@object.map do |item|
- if serializer = Serializer::Finder.find(item, scope)
+ if item.respond_to?(:active_model_serializer) && (serializer = item.active_model_serializer)
serializer.new(item, scope)
else
item
@@ -30,41 +29,6 @@ module ActiveModel
# Active Model Serializer
class Serializer
- module Finder
- mattr_accessor :constantizer
- @@constantizer = ActiveSupport::Inflector
-
- # Finds a serializer for the given object in the given scope.
- # If the object implements a +model_serializer+ method, it does
- # not do a scope lookup but uses the model_serializer method instead.
- def self.find(object, scope)
- if object.respond_to?(:model_serializer)
- object.model_serializer
- else
- scope = scope.class unless scope.respond_to?(:const_defined?)
- object = object.class unless object.respond_to?(:name)
- serializer = "#{object.name.demodulize}Serializer"
-
- begin
- scope.const_get serializer
- rescue NameError => e
- raise unless e.message =~ /uninitialized constant ([\w_]+::)*#{serializer}$/
- scope.parents.each do |parent|
- return parent.const_get(serializer) if parent.const_defined?(serializer)
- end
- nil
- end
- end
- end
- end
-
- # Defines the serialization scope. Core extension serializers
- # are defined in this module so a scoped lookup is able to find
- # core extension serializers.
- module Scope
- ArraySerializer = ::ActiveModel::ArraySerializer
- end
-
module Associations
class Config < Struct.new(:name, :options)
def serializer
@@ -216,3 +180,10 @@ module ActiveModel
end
end
end
+
+class Array
+ # Array uses ActiveModel::ArraySerializer.
+ def active_model_serializer
+ ActiveModel::ArraySerializer
+ end
+end \ No newline at end of file