Skip to content
This repository
Browse code

Rely solely on active_model_serializer and remove the fancy constant …

…lookup.
  • Loading branch information...
commit 7fcc8c0a1f38c77b12cb6ffe81fb2887e6c60b85 1 parent 6da52c6
José Valim josevalim authored
4 actionpack/lib/action_controller/metal/serialization.rb
@@ -13,7 +13,9 @@ def serialization_scope
13 13 end
14 14
15 15 def _render_option_json(json, options)
16   - json = json.active_model_serializer.new(json, serialization_scope) if json.respond_to?(:active_model_serializer)
  16 + if json.respond_to?(:active_model_serializer) && (serializer = json.active_model_serializer)
  17 + json = serializer.new(json, serialization_scope)
  18 + end
17 19 super
18 20 end
19 21
16 actionpack/test/controller/render_json_test.rb
@@ -26,8 +26,12 @@ def as_json(*)
26 26 end
27 27
28 28 class JsonSerializable
  29 + def initialize(skip=false)
  30 + @skip = skip
  31 + end
  32 +
29 33 def active_model_serializer
30   - JsonSerializer
  34 + JsonSerializer unless @skip
31 35 end
32 36
33 37 def as_json(*)
@@ -89,6 +93,11 @@ def render_json_with_serializer
89 93 @current_user = Struct.new(:as_json).new(:current_user => true)
90 94 render :json => JsonSerializable.new
91 95 end
  96 +
  97 + def render_json_with_serializer_api_but_without_serializer
  98 + @current_user = Struct.new(:as_json).new(:current_user => true)
  99 + render :json => JsonSerializable.new(true)
  100 + end
92 101 end
93 102
94 103 tests TestController
@@ -166,4 +175,9 @@ def test_render_json_with_serializer
166 175 assert_match '"scope":{"current_user":true}', @response.body
167 176 assert_match '"object":{"serializable_object":true}', @response.body
168 177 end
  178 +
  179 + def test_render_json_with_serializer_api_but_without_serializer
  180 + get :render_json_with_serializer_api_but_without_serializer
  181 + assert_match '{"serializable_object":true}', @response.body
  182 + end
169 183 end
12 activemodel/lib/active_model/serializable.rb
... ... @@ -1,6 +1,7 @@
1 1 require 'active_support/core_ext/hash/except'
2 2 require 'active_support/core_ext/hash/slice'
3 3 require 'active_support/core_ext/array/wrap'
  4 +require 'active_support/core_ext/string/inflections'
4 5
5 6 module ActiveModel
6 7 # == Active Model Serializable
@@ -72,11 +73,10 @@ module Serializable
72 73 autoload :JSON, "active_model/serializable/json"
73 74 autoload :XML, "active_model/serializable/xml"
74 75
75   - include ActiveModel::Serializer::Scope
76   -
77 76 module ClassMethods #:nodoc:
78   - def _model_serializer
79   - @_model_serializer ||= ActiveModel::Serializer::Finder.find(self, self)
  77 + def active_model_serializer
  78 + return @active_model_serializer if defined?(@active_model_serializer)
  79 + @active_model_serializer = "#{self.name}Serializer".safe_constantize
80 80 end
81 81 end
82 82
@@ -108,8 +108,8 @@ def serializable_hash(options = nil)
108 108 end
109 109
110 110 # Returns a model serializer for this object considering its namespace.
111   - def model_serializer
112   - self.class._model_serializer
  111 + def active_model_serializer
  112 + self.class.active_model_serializer
113 113 end
114 114
115 115 private
45 activemodel/lib/active_model/serializer.rb
... ... @@ -1,7 +1,6 @@
1 1 require "active_support/core_ext/class/attribute"
2 2 require "active_support/core_ext/string/inflections"
3 3 require "active_support/core_ext/module/anonymous"
4   -require "active_support/core_ext/module/introspection"
5 4 require "set"
6 5
7 6 module ActiveModel
@@ -15,7 +14,7 @@ def initialize(object, scope)
15 14
16 15 def serializable_array
17 16 @object.map do |item|
18   - if serializer = Serializer::Finder.find(item, scope)
  17 + if item.respond_to?(:active_model_serializer) && (serializer = item.active_model_serializer)
19 18 serializer.new(item, scope)
20 19 else
21 20 item
@@ -30,41 +29,6 @@ def as_json(*args)
30 29
31 30 # Active Model Serializer
32 31 class Serializer
33   - module Finder
34   - mattr_accessor :constantizer
35   - @@constantizer = ActiveSupport::Inflector
36   -
37   - # Finds a serializer for the given object in the given scope.
38   - # If the object implements a +model_serializer+ method, it does
39   - # not do a scope lookup but uses the model_serializer method instead.
40   - def self.find(object, scope)
41   - if object.respond_to?(:model_serializer)
42   - object.model_serializer
43   - else
44   - scope = scope.class unless scope.respond_to?(:const_defined?)
45   - object = object.class unless object.respond_to?(:name)
46   - serializer = "#{object.name.demodulize}Serializer"
47   -
48   - begin
49   - scope.const_get serializer
50   - rescue NameError => e
51   - raise unless e.message =~ /uninitialized constant ([\w_]+::)*#{serializer}$/
52   - scope.parents.each do |parent|
53   - return parent.const_get(serializer) if parent.const_defined?(serializer)
54   - end
55   - nil
56   - end
57   - end
58   - end
59   - end
60   -
61   - # Defines the serialization scope. Core extension serializers
62   - # are defined in this module so a scoped lookup is able to find
63   - # core extension serializers.
64   - module Scope
65   - ArraySerializer = ::ActiveModel::ArraySerializer
66   - end
67   -
68 32 module Associations
69 33 class Config < Struct.new(:name, :options)
70 34 def serializer
@@ -216,3 +180,10 @@ def attributes
216 180 end
217 181 end
218 182 end
  183 +
  184 +class Array
  185 + # Array uses ActiveModel::ArraySerializer.
  186 + def active_model_serializer
  187 + ActiveModel::ArraySerializer
  188 + end
  189 +end
37 activemodel/test/cases/serializer_test.rb
@@ -19,10 +19,8 @@ class User
19 19 include ActiveModel::Serializable
20 20
21 21 attr_accessor :superuser
22   - attr_writer :model_serializer
23 22
24 23 def initialize(hash={})
25   - @model_serializer = nil
26 24 @attributes = hash.merge(:first_name => "Jose", :last_name => "Valim", :password => "oh noes yugive my password")
27 25 end
28 26
@@ -33,17 +31,15 @@ def read_attribute_for_serialization(name)
33 31 def super_user?
34 32 @superuser
35 33 end
36   -
37   - def model_serializer
38   - @model_serializer || super
39   - end
40 34 end
41 35
42 36 class Post < Model
43 37 attr_accessor :comments
  38 + def active_model_serializer; PostSerializer; end
44 39 end
45 40
46 41 class Comment < Model
  42 + def active_model_serializer; CommentSerializer; end
47 43 end
48 44
49 45 class UserSerializer < ActiveModel::Serializer
@@ -428,38 +424,11 @@ def test_array_serializer
428 424 post.comments = []
429 425
430 426 array = [model, post, comments]
431   - serializer = ActiveModel::Serializer::Finder.find(array, user).new(array, user)
  427 + serializer = array.active_model_serializer.new(array, user)
432 428 assert_equal([
433 429 { :model => "Model" },
434 430 { :post => { :body => "Body of new post", :comments => [], :title => "New Post" } },
435 431 { :comment => { :title => "Comment1" } }
436 432 ], serializer.as_json)
437 433 end
438   -
439   - def test_array_serializer_respects_model_serializer
440   - user = User.new(:first_name => "Jose", :last_name => "Valim")
441   - user.model_serializer = User2Serializer
442   -
443   - array = [user]
444   - serializer = ActiveModel::Serializer::Finder.find(array, user).new(array, {})
445   - assert_equal([
446   - { :user2 => { :last_name => "Valim", :first_name => "Jose", :ok => true } },
447   - ], serializer.as_json)
448   - end
449   -
450   - def test_finder_respects_model_serializer
451   - user = User.new(:first_name => "Jose", :last_name => "Valim")
452   - assert_equal UserSerializer, user.model_serializer
453   -
454   - serializer = ActiveModel::Serializer::Finder.find(user, self).new(user, {})
455   - assert_equal({
456   - :user => { :last_name => "Valim", :first_name => "Jose"},
457   - }, serializer.as_json)
458   -
459   - user.model_serializer = User2Serializer
460   - serializer = ActiveModel::Serializer::Finder.find(user, self).new(user, {})
461   - assert_equal({
462   - :user2 => { :last_name => "Valim", :first_name => "Jose", :ok => true },
463   - }, serializer.as_json)
464   - end
465 434 end
1  activesupport/lib/active_support/dependencies.rb
@@ -542,6 +542,7 @@ def get(key)
542 542 key = key.name if key.respond_to?(:name)
543 543 @store[key] ||= Inflector.constantize(key)
544 544 end
  545 + alias :[] :get
545 546
546 547 def safe_get(key)
547 548 key = key.name if key.respond_to?(:name)
5 activesupport/test/class_cache_test.rb
@@ -49,6 +49,11 @@ def test_get_constantizes_fails_on_invalid_names
49 49 end
50 50 end
51 51
  52 + def test_get_alias
  53 + assert @cache.empty?
  54 + assert_equal @cache[ClassCacheTest.name], @cache.get(ClassCacheTest.name)
  55 + end
  56 +
52 57 def test_safe_get_constantizes
53 58 assert @cache.empty?
54 59 assert_equal ClassCacheTest, @cache.safe_get(ClassCacheTest.name)

0 comments on commit 7fcc8c0

Please sign in to comment.
Something went wrong with that request. Please try again.