active_model_serializers 0.9.9 → 0.10.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (135) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +21 -0
  3. data/.travis.yml +27 -0
  4. data/CHANGELOG.md +8 -204
  5. data/CONTRIBUTING.md +23 -12
  6. data/Gemfile +17 -0
  7. data/{MIT-LICENSE → LICENSE.txt} +3 -2
  8. data/README.md +151 -781
  9. data/Rakefile +12 -0
  10. data/active_model_serializers.gemspec +26 -0
  11. data/lib/action_controller/serialization.rb +30 -75
  12. data/lib/active_model/serializer/adapter/fragment_cache.rb +78 -0
  13. data/lib/active_model/serializer/adapter/json/fragment_cache.rb +15 -0
  14. data/lib/active_model/serializer/adapter/json.rb +52 -0
  15. data/lib/active_model/serializer/adapter/json_api/fragment_cache.rb +22 -0
  16. data/lib/active_model/serializer/adapter/json_api.rb +152 -0
  17. data/lib/active_model/serializer/adapter/null.rb +11 -0
  18. data/lib/active_model/serializer/adapter.rb +87 -0
  19. data/lib/active_model/serializer/array_serializer.rb +32 -0
  20. data/lib/active_model/serializer/configuration.rb +13 -0
  21. data/lib/active_model/serializer/fieldset.rb +40 -0
  22. data/lib/active_model/serializer/version.rb +1 -1
  23. data/lib/active_model/serializer.rb +192 -267
  24. data/lib/active_model_serializers.rb +5 -7
  25. data/lib/generators/serializer/USAGE +6 -0
  26. data/lib/{active_model/serializer/generators → generators}/serializer/serializer_generator.rb +8 -8
  27. data/lib/{active_model/serializer/generators → generators}/serializer/templates/serializer.rb +2 -2
  28. data/test/action_controller/adapter_selector_test.rb +51 -0
  29. data/test/action_controller/explicit_serializer_test.rb +110 -0
  30. data/test/action_controller/json_api_linked_test.rb +173 -0
  31. data/test/action_controller/serialization_scope_name_test.rb +63 -0
  32. data/test/action_controller/serialization_test.rb +365 -0
  33. data/test/adapter/fragment_cache_test.rb +27 -0
  34. data/test/adapter/json/belongs_to_test.rb +41 -0
  35. data/test/adapter/json/collection_test.rb +59 -0
  36. data/test/adapter/json/has_many_test.rb +36 -0
  37. data/test/adapter/json_api/belongs_to_test.rb +147 -0
  38. data/test/adapter/json_api/collection_test.rb +89 -0
  39. data/test/adapter/json_api/has_many_embed_ids_test.rb +45 -0
  40. data/test/adapter/json_api/has_many_explicit_serializer_test.rb +98 -0
  41. data/test/adapter/json_api/has_many_test.rb +106 -0
  42. data/test/adapter/json_api/has_one_test.rb +59 -0
  43. data/test/adapter/json_api/linked_test.rb +257 -0
  44. data/test/adapter/json_test.rb +34 -0
  45. data/test/adapter/null_test.rb +25 -0
  46. data/test/adapter_test.rb +43 -0
  47. data/test/array_serializer_test.rb +29 -0
  48. data/test/fixtures/poro.rb +123 -171
  49. data/test/serializers/adapter_for_test.rb +50 -0
  50. data/test/serializers/associations_test.rb +106 -0
  51. data/test/serializers/attribute_test.rb +23 -0
  52. data/test/serializers/attributes_test.rb +28 -0
  53. data/test/serializers/cache_test.rb +128 -0
  54. data/test/serializers/configuration_test.rb +15 -0
  55. data/test/serializers/fieldset_test.rb +26 -0
  56. data/test/serializers/generators_test.rb +59 -0
  57. data/test/serializers/meta_test.rb +78 -0
  58. data/test/serializers/options_test.rb +21 -0
  59. data/test/serializers/serializer_for_test.rb +56 -0
  60. data/test/serializers/urls_test.rb +26 -0
  61. data/test/test_helper.rb +21 -6
  62. metadata +121 -159
  63. data/DESIGN.textile +0 -586
  64. data/lib/action_controller/serialization_test_case.rb +0 -79
  65. data/lib/active_model/array_serializer.rb +0 -68
  66. data/lib/active_model/default_serializer.rb +0 -28
  67. data/lib/active_model/serializable/utils.rb +0 -16
  68. data/lib/active_model/serializable.rb +0 -59
  69. data/lib/active_model/serializer/association/has_many.rb +0 -39
  70. data/lib/active_model/serializer/association/has_one.rb +0 -25
  71. data/lib/active_model/serializer/association.rb +0 -58
  72. data/lib/active_model/serializer/config.rb +0 -31
  73. data/lib/active_model/serializer/generators/resource_override.rb +0 -13
  74. data/lib/active_model/serializer/generators/serializer/USAGE +0 -9
  75. data/lib/active_model/serializer/generators/serializer/scaffold_controller_generator.rb +0 -14
  76. data/lib/active_model/serializer/generators/serializer/templates/controller.rb +0 -93
  77. data/lib/active_model/serializer/railtie.rb +0 -22
  78. data/lib/active_model/serializer_support.rb +0 -5
  79. data/lib/active_model_serializers/model/caching.rb +0 -26
  80. data/test/benchmark/app.rb +0 -60
  81. data/test/benchmark/benchmarking_support.rb +0 -67
  82. data/test/benchmark/bm_active_record.rb +0 -41
  83. data/test/benchmark/setup.rb +0 -75
  84. data/test/benchmark/tmp/miniprofiler/mp_timers_6eqewtfgrhitvq5gqm25 +0 -0
  85. data/test/benchmark/tmp/miniprofiler/mp_timers_8083sx03hu72pxz1a4d0 +0 -0
  86. data/test/benchmark/tmp/miniprofiler/mp_timers_fyz2gsml4z0ph9kpoy1c +0 -0
  87. data/test/benchmark/tmp/miniprofiler/mp_timers_hjry5rc32imd42oxoi48 +0 -0
  88. data/test/benchmark/tmp/miniprofiler/mp_timers_m8fpoz2cvt3g9agz0bs3 +0 -0
  89. data/test/benchmark/tmp/miniprofiler/mp_timers_p92m2drnj1i568u3sta0 +0 -0
  90. data/test/benchmark/tmp/miniprofiler/mp_timers_qg52tpca3uesdfguee9i +0 -0
  91. data/test/benchmark/tmp/miniprofiler/mp_timers_s15t1a6mvxe0z7vjv790 +0 -0
  92. data/test/benchmark/tmp/miniprofiler/mp_timers_x8kal3d17nfds6vp4kcj +0 -0
  93. data/test/benchmark/tmp/miniprofiler/mp_views_127.0.0.1 +0 -0
  94. data/test/fixtures/active_record.rb +0 -96
  95. data/test/fixtures/template.html.erb +0 -1
  96. data/test/integration/action_controller/namespaced_serialization_test.rb +0 -105
  97. data/test/integration/action_controller/serialization_test.rb +0 -287
  98. data/test/integration/action_controller/serialization_test_case_test.rb +0 -71
  99. data/test/integration/active_record/active_record_test.rb +0 -94
  100. data/test/integration/generators/resource_generator_test.rb +0 -26
  101. data/test/integration/generators/scaffold_controller_generator_test.rb +0 -64
  102. data/test/integration/generators/serializer_generator_test.rb +0 -41
  103. data/test/test_app.rb +0 -14
  104. data/test/tmp/app/assets/javascripts/accounts.js +0 -2
  105. data/test/tmp/app/assets/stylesheets/accounts.css +0 -4
  106. data/test/tmp/app/controllers/accounts_controller.rb +0 -3
  107. data/test/tmp/app/helpers/accounts_helper.rb +0 -3
  108. data/test/tmp/app/serializers/account_serializer.rb +0 -4
  109. data/test/tmp/config/routes.rb +0 -2
  110. data/test/unit/active_model/array_serializer/except_test.rb +0 -18
  111. data/test/unit/active_model/array_serializer/key_format_test.rb +0 -18
  112. data/test/unit/active_model/array_serializer/meta_test.rb +0 -53
  113. data/test/unit/active_model/array_serializer/only_test.rb +0 -18
  114. data/test/unit/active_model/array_serializer/options_test.rb +0 -16
  115. data/test/unit/active_model/array_serializer/root_test.rb +0 -102
  116. data/test/unit/active_model/array_serializer/scope_test.rb +0 -24
  117. data/test/unit/active_model/array_serializer/serialization_test.rb +0 -239
  118. data/test/unit/active_model/default_serializer_test.rb +0 -13
  119. data/test/unit/active_model/serializer/associations/build_serializer_test.rb +0 -36
  120. data/test/unit/active_model/serializer/associations_test.rb +0 -49
  121. data/test/unit/active_model/serializer/attributes_test.rb +0 -57
  122. data/test/unit/active_model/serializer/config_test.rb +0 -91
  123. data/test/unit/active_model/serializer/filter_test.rb +0 -69
  124. data/test/unit/active_model/serializer/has_many_polymorphic_test.rb +0 -189
  125. data/test/unit/active_model/serializer/has_many_test.rb +0 -265
  126. data/test/unit/active_model/serializer/has_one_and_has_many_test.rb +0 -27
  127. data/test/unit/active_model/serializer/has_one_polymorphic_test.rb +0 -196
  128. data/test/unit/active_model/serializer/has_one_test.rb +0 -253
  129. data/test/unit/active_model/serializer/key_format_test.rb +0 -25
  130. data/test/unit/active_model/serializer/meta_test.rb +0 -39
  131. data/test/unit/active_model/serializer/options_test.rb +0 -42
  132. data/test/unit/active_model/serializer/root_test.rb +0 -117
  133. data/test/unit/active_model/serializer/scope_test.rb +0 -49
  134. data/test/unit/active_model/serializer/url_helpers_test.rb +0 -35
  135. data/test/unit/active_model/serilizable_test.rb +0 -50
@@ -1,5 +1,5 @@
1
1
  module ActiveModel
2
2
  class Serializer
3
- VERSION = '0.9.9'.freeze
3
+ VERSION = "0.10.0.rc1"
4
4
  end
5
5
  end
@@ -1,333 +1,258 @@
1
- require 'active_model/array_serializer'
2
- require 'active_model/serializable'
3
- require 'active_model/serializer/association'
4
- require 'active_model/serializer/config'
5
-
6
- require 'thread'
7
- require 'concurrent/map'
1
+ require 'thread_safe'
8
2
 
9
3
  module ActiveModel
10
4
  class Serializer
11
- include Serializable
12
-
13
- @mutex = Mutex.new
5
+ extend ActiveSupport::Autoload
6
+ autoload :Configuration
7
+ autoload :ArraySerializer
8
+ autoload :Adapter
9
+ include Configuration
14
10
 
15
11
  class << self
16
- def inherited(base)
17
- base._root = _root
18
- base._attributes = (_attributes || []).dup
19
- base._associations = (_associations || {}).dup
20
- end
12
+ attr_accessor :_attributes
13
+ attr_accessor :_attributes_keys
14
+ attr_accessor :_associations
15
+ attr_accessor :_urls
16
+ attr_accessor :_cache
17
+ attr_accessor :_fragmented
18
+ attr_accessor :_cache_key
19
+ attr_accessor :_cache_only
20
+ attr_accessor :_cache_except
21
+ attr_accessor :_cache_options
22
+ end
21
23
 
22
- def setup
23
- @mutex.synchronize do
24
- yield CONFIG
25
- end
26
- end
24
+ def self.inherited(base)
25
+ base._attributes = []
26
+ base._attributes_keys = {}
27
+ base._associations = {}
28
+ base._urls = []
29
+ end
27
30
 
28
- EMBED_IN_ROOT_OPTIONS = [
29
- :include,
30
- :embed_in_root,
31
- :embed_in_root_key,
32
- :embed_namespace
33
- ].freeze
34
-
35
- def embed(type, options={})
36
- CONFIG.embed = type
37
- if EMBED_IN_ROOT_OPTIONS.any? { |opt| options[opt].present? }
38
- CONFIG.embed_in_root = true
39
- end
40
- if options[:embed_in_root_key].present?
41
- CONFIG.embed_in_root_key = options[:embed_in_root_key]
42
- end
43
- ActiveSupport::Deprecation.warn <<-WARN
44
- ** Notice: embed is deprecated. **
45
- The use of .embed method on a Serializer will be soon removed, as this should have a global scope and not a class scope.
46
- Please use the global .setup method instead:
47
- ActiveModel::Serializer.setup do |config|
48
- config.embed = :#{type}
49
- config.embed_in_root = #{CONFIG.embed_in_root || false}
50
- end
51
- WARN
52
- end
31
+ def self.attributes(*attrs)
32
+ attrs = attrs.first if attrs.first.class == Array
33
+ @_attributes.concat attrs
53
34
 
54
- def format_keys(format)
55
- @key_format = format
56
- end
57
- attr_reader :key_format
58
-
59
- def serializer_for(resource, options = {})
60
- if resource.respond_to?(:serializer_class)
61
- resource.serializer_class
62
- elsif resource.respond_to?(:to_ary)
63
- if Object.constants.include?(:ArraySerializer)
64
- ::ArraySerializer
65
- else
66
- ArraySerializer
67
- end
68
- else
69
- search_list = build_serializer_class_list(resource, options)
70
- result = search_list.map do |klass_name|
71
- Serializer.serializers_cache.fetch_or_store(klass_name) do
72
- _const_get(klass_name)
73
- end
74
- end
75
-
76
- result.find { |serializer| !serializer.nil? }
77
- end
35
+ attrs.each do |attr|
36
+ define_method attr do
37
+ object && object.read_attribute_for_serialization(attr)
38
+ end unless method_defined?(attr) || _fragmented.respond_to?(attr)
78
39
  end
40
+ end
79
41
 
80
- attr_accessor :_root, :_attributes, :_associations
81
- alias root _root=
82
- alias root= _root=
42
+ def self.attribute(attr, options = {})
43
+ key = options.fetch(:key, attr)
44
+ @_attributes_keys[attr] = {key: key} if key != attr
45
+ @_attributes.concat [key]
46
+ define_method key do
47
+ object.read_attribute_for_serialization(attr)
48
+ end unless method_defined?(key) || _fragmented.respond_to?(attr)
49
+ end
83
50
 
84
- def root_name
85
- if name
86
- root_name = name.demodulize.underscore.sub(/_serializer$/, '')
87
- CONFIG.plural_default_root ? root_name.pluralize : root_name
88
- end
89
- end
51
+ def self.fragmented(serializer)
52
+ @_fragmented = serializer
53
+ end
90
54
 
91
- def attributes(*attrs)
92
- attrs.each do |attr|
93
- striped_attr = strip_attribute attr
55
+ # Enables a serializer to be automatically cached
56
+ def self.cache(options = {})
57
+ @_cache = ActionController::Base.cache_store if Rails.configuration.action_controller.perform_caching
58
+ @_cache_key = options.delete(:key)
59
+ @_cache_only = options.delete(:only)
60
+ @_cache_except = options.delete(:except)
61
+ @_cache_options = (options.empty?) ? nil : options
62
+ end
94
63
 
95
- @_attributes << striped_attr
64
+ # Defines an association in the object should be rendered.
65
+ #
66
+ # The serializer object should implement the association name
67
+ # as a method which should return an array when invoked. If a method
68
+ # with the association name does not exist, the association name is
69
+ # dispatched to the serialized object.
70
+ def self.has_many(*attrs)
71
+ associate(:has_many, attrs)
72
+ end
96
73
 
97
- define_method striped_attr do
98
- object.read_attribute_for_serialization attr
99
- end unless method_defined?(attr)
100
- end
101
- end
74
+ # Defines an association in the object that should be rendered.
75
+ #
76
+ # The serializer object should implement the association name
77
+ # as a method which should return an object when invoked. If a method
78
+ # with the association name does not exist, the association name is
79
+ # dispatched to the serialized object.
80
+ def self.belongs_to(*attrs)
81
+ associate(:belongs_to, attrs)
82
+ end
102
83
 
103
- def has_one(*attrs)
104
- associate(Association::HasOne, *attrs)
105
- end
84
+ # Defines an association in the object should be rendered.
85
+ #
86
+ # The serializer object should implement the association name
87
+ # as a method which should return an object when invoked. If a method
88
+ # with the association name does not exist, the association name is
89
+ # dispatched to the serialized object.
90
+ def self.has_one(*attrs)
91
+ associate(:has_one, attrs)
92
+ end
106
93
 
107
- def has_many(*attrs)
108
- associate(Association::HasMany, *attrs)
109
- end
94
+ def self.associate(type, attrs) #:nodoc:
95
+ options = attrs.extract_options!
96
+ self._associations = _associations.dup
97
+
98
+ attrs.each do |attr|
99
+ unless method_defined?(attr)
100
+ define_method attr do
101
+ object.send attr
102
+ end
103
+ end
110
104
 
111
- def serializers_cache
112
- @serializers_cache ||= Concurrent::Map.new
105
+ self._associations[attr] = {type: type, association_options: options}
113
106
  end
107
+ end
114
108
 
115
- private
109
+ def self.url(attr)
110
+ @_urls.push attr
111
+ end
116
112
 
117
- def strip_attribute(attr)
118
- symbolized = attr.is_a?(Symbol)
113
+ def self.urls(*attrs)
114
+ @_urls.concat attrs
115
+ end
119
116
 
120
- attr = attr.to_s.gsub(/\?\Z/, '')
121
- attr = attr.to_sym if symbolized
122
- attr
117
+ def self.serializer_for(resource, options = {})
118
+ if resource.respond_to?(:to_ary)
119
+ config.array_serializer
120
+ else
121
+ options
122
+ .fetch(:association_options, {})
123
+ .fetch(:serializer, get_serializer_for(resource.class))
123
124
  end
125
+ end
124
126
 
125
- def build_serializer_class_list(resource, options)
126
- list = []
127
- list << build_serializer_class(resource, options)
128
- list << build_serializer_class(resource, {})
129
- list << build_serializer_class(resource.class.name.demodulize, {})
127
+ def self.adapter
128
+ adapter_class = case config.adapter
129
+ when Symbol
130
+ ActiveModel::Serializer::Adapter.adapter_class(config.adapter)
131
+ when Class
132
+ config.adapter
130
133
  end
131
-
132
- def build_serializer_class(resource, options)
133
- "".tap do |klass_name|
134
- klass_name << "#{options[:namespace]}::" if options[:namespace]
135
- klass_name << options[:prefix].to_s.classify if options[:prefix]
136
- if resource.is_a?(String)
137
- klass_name << "#{resource}Serializer"
138
- else
139
- klass_name << "#{resource.class.name}Serializer"
140
- end
141
- end
134
+ unless adapter_class
135
+ valid_adapters = Adapter.constants.map { |klass| ":#{klass.to_s.downcase}" }
136
+ raise ArgumentError, "Unknown adapter: #{config.adapter}. Valid adapters are: #{valid_adapters}"
142
137
  end
143
138
 
144
- def associate(klass, *attrs)
145
- options = attrs.extract_options!
139
+ adapter_class
140
+ end
146
141
 
147
- attrs.each do |attr|
148
- define_method attr do
149
- object.send attr
150
- end unless method_defined?(attr)
142
+ def self._root
143
+ @@root ||= false
144
+ end
151
145
 
152
- @_associations[attr] = klass.new(attr, options)
153
- end
154
- end
146
+ def self._root=(root)
147
+ @@root = root
155
148
  end
156
149
 
157
- def initialize(object, options={})
158
- @object = object
159
- @scope = options[:scope]
160
- @root = options.fetch(:root, self.class._root)
161
- @polymorphic = options.fetch(:polymorphic, false)
162
- @meta_key = options[:meta_key] || :meta
163
- @meta = options[@meta_key]
164
- @wrap_in_array = options[:_wrap_in_array]
165
- @only = options[:only] ? Array(options[:only]) : nil
166
- @except = options[:except] ? Array(options[:except]) : nil
167
- @key_format = options[:key_format]
168
- @context = options[:context]
169
- @namespace = options[:namespace]
150
+ def self.root_name
151
+ name.demodulize.underscore.sub(/_serializer$/, '') if name
152
+ end
153
+
154
+ attr_accessor :object, :root, :meta, :meta_key, :scope
155
+
156
+ def initialize(object, options = {})
157
+ @object = object
158
+ @options = options
159
+ @root = options[:root] || (self.class._root ? self.class.root_name : false)
160
+ @meta = options[:meta]
161
+ @meta_key = options[:meta_key]
162
+ @scope = options[:scope]
163
+
164
+ scope_name = options[:scope_name]
165
+ if scope_name && !respond_to?(scope_name)
166
+ self.class.class_eval do
167
+ define_method scope_name, lambda { scope }
168
+ end
169
+ end
170
170
  end
171
- attr_accessor :object, :scope, :root, :meta_key, :meta, :key_format, :context, :polymorphic
172
171
 
173
172
  def json_key
174
- key = if root == true || root.nil?
173
+ if root == true || root.nil?
175
174
  self.class.root_name
176
175
  else
177
176
  root
178
177
  end
179
-
180
- key_format == :lower_camel && key.present? ? key.camelize(:lower) : key
181
178
  end
182
179
 
183
- def attributes
184
- filter(self.class._attributes.dup).each_with_object({}) do |name, hash|
185
- hash[name] = send(name)
186
- end
180
+ def id
181
+ object.id if object
187
182
  end
188
183
 
189
- def associations(options={})
190
- associations = self.class._associations
191
- included_associations = filter(associations.keys)
192
- associations.each_with_object({}) do |(name, association), hash|
193
- if included_associations.include? name
194
- if association.embed_ids?
195
- ids = serialize_ids association
196
- if association.embed_namespace?
197
- hash = hash[association.embed_namespace] ||= {}
198
- hash[association.key] = ids
199
- else
200
- hash[association.key] = ids
201
- end
202
- elsif association.embed_objects?
203
- if association.embed_namespace?
204
- hash = hash[association.embed_namespace] ||= {}
205
- end
206
- hash[association.embedded_key] = serialize association, options
207
- end
208
- end
209
- end
184
+ def type
185
+ object.class.to_s.demodulize.underscore.pluralize
210
186
  end
211
187
 
212
- def filter(keys)
213
- if @only
214
- keys & @only
215
- elsif @except
216
- keys - @except
217
- else
218
- keys
219
- end
220
- end
188
+ def attributes(options = {})
189
+ attributes =
190
+ if options[:fields]
191
+ self.class._attributes & options[:fields]
192
+ else
193
+ self.class._attributes.dup
194
+ end
221
195
 
222
- def embedded_in_root_associations
223
- associations = self.class._associations
224
- included_associations = filter(associations.keys)
225
- associations.each_with_object({}) do |(name, association), hash|
226
- if included_associations.include? name
227
- association_serializer = build_serializer(association)
228
- # we must do this always because even if the current association is not
229
- # embedded in root, it might have its own associations that are embedded in root
230
- hash.merge!(association_serializer.embedded_in_root_associations) do |key, oldval, newval|
231
- if oldval.respond_to?(:to_ary)
232
- [oldval, newval].flatten.uniq
233
- else
234
- oldval.merge(newval) { |_, oldval, newval| [oldval, newval].flatten.uniq }
235
- end
236
- end
196
+ attributes += options[:required_fields] if options[:required_fields]
237
197
 
238
- if association.embed_in_root?
239
- if association.embed_in_root_key?
240
- hash = hash[association.embed_in_root_key] ||= {}
241
- end
242
-
243
- serialized_data = association_serializer.serializable_object
244
- key = association.root_key
245
- if hash.has_key?(key)
246
- hash[key].concat(serialized_data).uniq!
247
- else
248
- hash[key] = serialized_data
249
- end
250
- end
198
+ attributes.each_with_object({}) do |name, hash|
199
+ unless self.class._fragmented
200
+ hash[name] = send(name)
201
+ else
202
+ hash[name] = self.class._fragmented.public_send(name)
251
203
  end
252
204
  end
253
205
  end
254
206
 
255
- def build_serializer(association)
256
- object = send(association.name)
257
- association.build_serializer(object, association_options_for_serializer(association))
258
- end
259
-
260
- def association_options_for_serializer(association)
261
- prefix = association.options[:prefix]
262
- namespace = association.options[:namespace] || @namespace || self.namespace
207
+ def each_association(&block)
208
+ self.class._associations.dup.each do |name, association_options|
209
+ next unless object
210
+ association_value = send(name)
263
211
 
264
- { scope: scope }.tap do |opts|
265
- opts[:namespace] = namespace if namespace
266
- opts[:prefix] = prefix if prefix
267
- end
268
- end
212
+ serializer_class = ActiveModel::Serializer.serializer_for(association_value, association_options)
269
213
 
270
- def serialize(association,options={})
271
- build_serializer(association).serializable_object(options)
272
- end
214
+ if serializer_class
215
+ serializer = serializer_class.new(
216
+ association_value,
217
+ options.merge(serializer_from_options(association_options))
218
+ )
219
+ elsif !association_value.nil? && !association_value.instance_of?(Object)
220
+ association_options[:association_options][:virtual_value] = association_value
221
+ end
273
222
 
274
- def serialize_ids(association)
275
- associated_data = send(association.name)
276
- if associated_data.respond_to?(:to_ary)
277
- associated_data.map { |elem| serialize_id(elem, association) }
278
- else
279
- serialize_id(associated_data, association) if associated_data
223
+ if block_given?
224
+ block.call(name, serializer, association_options[:association_options])
225
+ end
280
226
  end
281
227
  end
282
228
 
283
- def key_format
284
- @key_format || self.class.key_format || CONFIG.key_format
229
+ def serializer_from_options(options)
230
+ opts = {}
231
+ serializer = options.fetch(:association_options, {}).fetch(:serializer, nil)
232
+ opts[:serializer] = serializer if serializer
233
+ opts
285
234
  end
286
235
 
287
- def format_key(key)
288
- if key_format == :lower_camel
289
- key.to_s.camelize(:lower)
290
- else
291
- key
292
- end
236
+ def self.serializers_cache
237
+ @serializers_cache ||= ThreadSafe::Cache.new
293
238
  end
294
239
 
295
- def convert_keys(hash)
296
- Hash[hash.map do |k,v|
297
- key = if k.is_a?(Symbol)
298
- format_key(k).to_sym
299
- else
300
- format_key(k)
301
- end
240
+ private
302
241
 
303
- [key ,v]
304
- end]
305
- end
306
-
307
- attr_writer :serialization_options
308
- def serialization_options
309
- @serialization_options || {}
310
- end
242
+ attr_reader :options
311
243
 
312
- def serializable_object(options={})
313
- self.serialization_options = options
314
- return @wrap_in_array ? [] : nil if @object.nil?
315
- hash = attributes
316
- hash.merge! associations(options)
317
- hash = convert_keys(hash) if key_format.present?
318
- hash = { :type => type_name(@object), type_name(@object) => hash } if @polymorphic
319
- @wrap_in_array ? [hash] : hash
320
- end
321
- alias_method :serializable_hash, :serializable_object
244
+ def self.get_serializer_for(klass)
245
+ serializers_cache.fetch_or_store(klass) do
246
+ serializer_class_name = "#{klass.name}Serializer"
247
+ serializer_class = serializer_class_name.safe_constantize
322
248
 
323
- def serialize_id(elem, association)
324
- id = elem.read_attribute_for_serialization(association.embed_key)
325
- association.polymorphic? ? { id: id, type: type_name(elem) } : id
249
+ if serializer_class
250
+ serializer_class
251
+ elsif klass.superclass
252
+ get_serializer_for(klass.superclass)
253
+ end
254
+ end
326
255
  end
327
256
 
328
- def type_name(elem)
329
- elem.class.to_s.demodulize.underscore.to_sym
330
- end
331
257
  end
332
-
333
258
  end
@@ -1,18 +1,16 @@
1
1
  require 'active_model'
2
- require 'active_model/serializer'
3
- require 'active_model/serializer_support'
4
2
  require 'active_model/serializer/version'
5
- require 'active_model/serializer/railtie' if defined?(Rails)
3
+ require 'active_model/serializer'
4
+ require 'active_model/serializer/fieldset'
6
5
 
7
6
  begin
8
7
  require 'action_controller'
9
8
  require 'action_controller/serialization'
10
- require 'action_controller/serialization_test_case'
11
9
 
12
10
  ActiveSupport.on_load(:action_controller) do
13
- if ::ActionController::Serialization.enabled
14
- ActionController::Base.send(:include, ::ActionController::Serialization)
15
- ActionController::TestCase.send(:include, ::ActionController::SerializationAssertions)
11
+ include ::ActionController::Serialization
12
+ ActionDispatch::Reloader.to_prepare do
13
+ ActiveModel::Serializer.serializers_cache.clear
16
14
  end
17
15
  end
18
16
  rescue LoadError
@@ -0,0 +1,6 @@
1
+ Description:
2
+ Generates a serializer for the given resource with tests.
3
+
4
+ Example:
5
+ `rails generate serializer Account name created_at`
6
+
@@ -1,12 +1,12 @@
1
1
  module Rails
2
2
  module Generators
3
3
  class SerializerGenerator < NamedBase
4
- source_root File.expand_path('../templates', __FILE__)
5
- check_class_collision suffix: 'Serializer'
4
+ source_root File.expand_path("../templates", __FILE__)
5
+ check_class_collision :suffix => "Serializer"
6
6
 
7
- argument :attributes, type: :array, default: [], banner: 'field:type field:type'
7
+ argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
8
8
 
9
- class_option :parent, type: :string, desc: 'The parent class for the generated serializer'
9
+ class_option :parent, :type => :string, :desc => "The parent class for the generated serializer"
10
10
 
11
11
  def create_serializer_file
12
12
  template 'serializer.rb', File.join('app/serializers', class_path, "#{file_name}_serializer.rb")
@@ -25,13 +25,13 @@ module Rails
25
25
  def parent_class_name
26
26
  if options[:parent]
27
27
  options[:parent]
28
- elsif (ns = Rails::Generators.namespace) && ns.const_defined?(:ApplicationSerializer) ||
29
- (Object.const_get(:ApplicationSerializer) rescue nil)
30
- 'ApplicationSerializer'
28
+ elsif defined?(::ApplicationSerializer)
29
+ "ApplicationSerializer"
31
30
  else
32
- 'ActiveModel::Serializer'
31
+ "ActiveModel::Serializer"
33
32
  end
34
33
  end
35
34
  end
36
35
  end
37
36
  end
37
+
@@ -1,8 +1,8 @@
1
1
  <% module_namespacing do -%>
2
2
  class <%= class_name %>Serializer < <%= parent_class_name %>
3
3
  attributes <%= attributes_names.map(&:inspect).join(", ") %>
4
+ end
4
5
  <% association_names.each do |attribute| -%>
5
- has_one :<%= attribute %>
6
+ attribute :<%= attribute %>
6
7
  <% end -%>
7
- end
8
8
  <% end -%>
@@ -0,0 +1,51 @@
1
+ require 'test_helper'
2
+
3
+ module ActionController
4
+ module Serialization
5
+ class AdapterSelectorTest < ActionController::TestCase
6
+ class MyController < ActionController::Base
7
+ def render_using_default_adapter
8
+ @profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
9
+ render json: @profile
10
+ end
11
+
12
+ def render_using_adapter_override
13
+ @profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
14
+ render json: @profile, adapter: :json_api
15
+ end
16
+
17
+ def render_skipping_adapter
18
+ @profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
19
+ render json: @profile, adapter: false
20
+ end
21
+ end
22
+
23
+ tests MyController
24
+
25
+ def test_render_using_default_adapter
26
+ get :render_using_default_adapter
27
+ assert_equal '{"name":"Name 1","description":"Description 1"}', response.body
28
+ end
29
+
30
+ def test_render_using_adapter_override
31
+ get :render_using_adapter_override
32
+
33
+ expected = {
34
+ data: {
35
+ name: "Name 1",
36
+ description: "Description 1",
37
+ id: assigns(:profile).id.to_s,
38
+ type: "profiles"
39
+ }
40
+ }
41
+
42
+ assert_equal expected.to_json, response.body
43
+ end
44
+
45
+ def test_render_skipping_adapter
46
+ get :render_skipping_adapter
47
+ assert_equal '{"attributes":{"name":"Name 1","description":"Description 1","comments":"Comments 1"}}', response.body
48
+ end
49
+ end
50
+ end
51
+ end