active_model_serializers 0.8.3 → 0.10.0.rc3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (124) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +5 -0
  3. data/.rubocop.yml +82 -0
  4. data/.rubocop_todo.yml +315 -0
  5. data/.simplecov +99 -0
  6. data/.travis.yml +26 -20
  7. data/CHANGELOG.md +14 -67
  8. data/CONTRIBUTING.md +31 -0
  9. data/Gemfile +45 -1
  10. data/{MIT-LICENSE.txt → LICENSE.txt} +3 -2
  11. data/README.md +186 -488
  12. data/Rakefile +33 -12
  13. data/active_model_serializers.gemspec +49 -23
  14. data/appveyor.yml +25 -0
  15. data/docs/README.md +29 -0
  16. data/docs/general/adapters.md +110 -0
  17. data/docs/general/configuration_options.md +11 -0
  18. data/docs/general/getting_started.md +73 -0
  19. data/docs/howto/add_pagination_links.md +112 -0
  20. data/docs/howto/add_root_key.md +51 -0
  21. data/docs/howto/outside_controller_use.md +42 -0
  22. data/lib/action_controller/serialization.rb +31 -31
  23. data/lib/active_model/serializable_resource.rb +70 -0
  24. data/lib/active_model/serializer/adapter/flatten_json.rb +12 -0
  25. data/lib/active_model/serializer/adapter/fragment_cache.rb +75 -0
  26. data/lib/active_model/serializer/adapter/json/fragment_cache.rb +5 -0
  27. data/lib/active_model/serializer/adapter/json.rb +47 -0
  28. data/lib/active_model/serializer/adapter/json_api/fragment_cache.rb +13 -0
  29. data/lib/active_model/serializer/adapter/json_api/pagination_links.rb +50 -0
  30. data/lib/active_model/serializer/adapter/json_api.rb +158 -0
  31. data/lib/active_model/serializer/adapter/null.rb +5 -0
  32. data/lib/active_model/serializer/adapter.rb +159 -0
  33. data/lib/active_model/serializer/array_serializer.rb +40 -0
  34. data/lib/active_model/serializer/association.rb +20 -0
  35. data/lib/active_model/serializer/associations.rb +83 -219
  36. data/lib/active_model/serializer/belongs_to_reflection.rb +10 -0
  37. data/lib/active_model/serializer/collection_reflection.rb +7 -0
  38. data/lib/active_model/serializer/configuration.rb +14 -0
  39. data/lib/active_model/serializer/fieldset.rb +40 -0
  40. data/lib/active_model/serializer/has_many_reflection.rb +10 -0
  41. data/lib/active_model/serializer/has_one_reflection.rb +10 -0
  42. data/lib/active_model/serializer/lint.rb +129 -0
  43. data/lib/active_model/serializer/railtie.rb +15 -0
  44. data/lib/active_model/serializer/reflection.rb +74 -0
  45. data/lib/active_model/serializer/singular_reflection.rb +7 -0
  46. data/lib/active_model/serializer/utils.rb +35 -0
  47. data/lib/active_model/{serializers → serializer}/version.rb +1 -1
  48. data/lib/active_model/serializer.rb +121 -465
  49. data/lib/active_model_serializers.rb +26 -88
  50. data/lib/generators/serializer/USAGE +0 -3
  51. data/lib/generators/serializer/resource_override.rb +12 -0
  52. data/lib/generators/serializer/serializer_generator.rb +8 -13
  53. data/lib/generators/serializer/templates/serializer.rb.erb +8 -0
  54. data/lib/tasks/rubocop.rake +0 -0
  55. data/test/action_controller/adapter_selector_test.rb +53 -0
  56. data/test/action_controller/explicit_serializer_test.rb +134 -0
  57. data/test/action_controller/json_api/linked_test.rb +179 -0
  58. data/test/action_controller/json_api/pagination_test.rb +116 -0
  59. data/test/{serialization_scope_name_test.rb → action_controller/serialization_scope_name_test.rb} +15 -15
  60. data/test/action_controller/serialization_test.rb +420 -0
  61. data/test/active_record_test.rb +9 -0
  62. data/test/adapter/fragment_cache_test.rb +37 -0
  63. data/test/adapter/json/belongs_to_test.rb +47 -0
  64. data/test/adapter/json/collection_test.rb +82 -0
  65. data/test/adapter/json/has_many_test.rb +47 -0
  66. data/test/adapter/json_api/belongs_to_test.rb +157 -0
  67. data/test/adapter/json_api/collection_test.rb +95 -0
  68. data/test/adapter/json_api/has_many_embed_ids_test.rb +45 -0
  69. data/test/adapter/json_api/has_many_explicit_serializer_test.rb +98 -0
  70. data/test/adapter/json_api/has_many_test.rb +145 -0
  71. data/test/adapter/json_api/has_one_test.rb +81 -0
  72. data/test/adapter/json_api/json_api_test.rb +37 -0
  73. data/test/adapter/json_api/linked_test.rb +283 -0
  74. data/test/adapter/json_api/pagination_links_test.rb +115 -0
  75. data/test/adapter/json_api/resource_type_config_test.rb +59 -0
  76. data/test/adapter/json_test.rb +47 -0
  77. data/test/adapter/null_test.rb +25 -0
  78. data/test/adapter_test.rb +42 -0
  79. data/test/array_serializer_test.rb +99 -73
  80. data/test/capture_warnings.rb +65 -0
  81. data/test/fixtures/active_record.rb +56 -0
  82. data/test/fixtures/poro.rb +261 -0
  83. data/test/generators/scaffold_controller_generator_test.rb +23 -0
  84. data/test/generators/serializer_generator_test.rb +56 -0
  85. data/test/lint_test.rb +37 -0
  86. data/test/logger_test.rb +18 -0
  87. data/test/poro_test.rb +9 -0
  88. data/test/serializable_resource_test.rb +27 -0
  89. data/test/serializers/adapter_for_test.rb +170 -0
  90. data/test/serializers/association_macros_test.rb +36 -0
  91. data/test/serializers/associations_test.rb +150 -0
  92. data/test/serializers/attribute_test.rb +62 -0
  93. data/test/serializers/attributes_test.rb +57 -0
  94. data/test/serializers/cache_test.rb +165 -0
  95. data/test/serializers/configuration_test.rb +15 -0
  96. data/test/serializers/fieldset_test.rb +25 -0
  97. data/test/serializers/meta_test.rb +121 -0
  98. data/test/serializers/options_test.rb +21 -0
  99. data/test/serializers/root_test.rb +21 -0
  100. data/test/serializers/serializer_for_test.rb +65 -0
  101. data/test/support/rails_app.rb +21 -0
  102. data/test/support/serialization_testing.rb +13 -0
  103. data/test/support/simplecov.rb +6 -0
  104. data/test/support/stream_capture.rb +50 -0
  105. data/test/support/test_case.rb +5 -0
  106. data/test/test_helper.rb +48 -24
  107. data/test/utils/include_args_to_hash_test.rb +79 -0
  108. metadata +219 -47
  109. data/DESIGN.textile +0 -586
  110. data/Gemfile.edge +0 -9
  111. data/bench/perf.rb +0 -43
  112. data/cruft.md +0 -19
  113. data/lib/active_model/array_serializer.rb +0 -104
  114. data/lib/active_record/serializer_override.rb +0 -16
  115. data/lib/generators/resource_override.rb +0 -13
  116. data/lib/generators/serializer/templates/serializer.rb +0 -19
  117. data/test/association_test.rb +0 -592
  118. data/test/caching_test.rb +0 -96
  119. data/test/generators_test.rb +0 -85
  120. data/test/no_serialization_scope_test.rb +0 -34
  121. data/test/serialization_test.rb +0 -392
  122. data/test/serializer_support_test.rb +0 -51
  123. data/test/serializer_test.rb +0 -1465
  124. data/test/test_fakes.rb +0 -217
@@ -1,230 +1,94 @@
1
1
  module ActiveModel
2
2
  class Serializer
3
- module Associations #:nodoc:
4
- class Config #:nodoc:
5
- class_attribute :options
6
-
7
- def self.refine(name, class_options)
8
- current_class = self
9
-
10
- Class.new(self) do
11
- singleton_class.class_eval do
12
- define_method(:to_s) do
13
- "(subclass of #{current_class.name})"
14
- end
15
-
16
- alias inspect to_s
17
- end
18
-
19
- self.options = class_options
20
-
21
- # cache the root so we can reuse it without falling back on a per-instance basis
22
- begin
23
- self.options[:root] ||= self.new(name, nil).root
24
- rescue
25
- # this could fail if it needs a valid source, for example a polymorphic association
26
- end
27
-
28
- end
29
- end
30
-
31
- self.options = {}
32
-
33
- def initialize(name, source, options={})
34
- @name = name
35
- @source = source
36
- @options = options
37
- end
38
-
39
- def option(key, default=nil)
40
- if @options.key?(key)
41
- @options[key]
42
- elsif self.class.options.key?(key)
43
- self.class.options[key]
44
- else
45
- default
46
- end
47
- end
48
-
49
- def target_serializer
50
- serializer = option(:serializer)
51
- serializer.is_a?(String) ? serializer.constantize : serializer
52
- end
53
-
54
- def source_serializer
55
- @source
56
- end
57
-
58
- def key
59
- option(:key) || @name
60
- end
61
-
62
- def root
63
- option(:root) || @name
64
- end
65
-
66
- def name
67
- option(:name) || @name
68
- end
69
-
70
- def associated_object
71
- option(:value) || source_serializer.send(name)
72
- end
73
-
74
- def embed_ids?
75
- [:id, :ids].include? option(:embed, source_serializer._embed)
76
- end
77
-
78
- def embed_objects?
79
- [:object, :objects].include? option(:embed, source_serializer._embed)
80
- end
81
-
82
- def embed_in_root?
83
- option(:include, source_serializer._root_embed)
84
- end
85
-
86
- def embeddable?
87
- !associated_object.nil?
88
- end
89
-
90
- protected
91
-
92
- def find_serializable(object)
93
- if target_serializer
94
- target_serializer.new(object, source_serializer.options)
95
- elsif object.respond_to?(:active_model_serializer) && (ams = object.active_model_serializer)
96
- ams.new(object, source_serializer.options)
97
- else
98
- object
99
- end
100
- end
3
+ # Defines an association in the object should be rendered.
4
+ #
5
+ # The serializer object should implement the association name
6
+ # as a method which should return an array when invoked. If a method
7
+ # with the association name does not exist, the association name is
8
+ # dispatched to the serialized object.
9
+ #
10
+ module Associations
11
+ extend ActiveSupport::Concern
12
+
13
+ included do |base|
14
+ class << base
15
+ attr_accessor :_reflections
16
+ end
17
+
18
+ autoload :Association
19
+ autoload :Reflection
20
+ autoload :SingularReflection
21
+ autoload :CollectionReflection
22
+ autoload :BelongsToReflection
23
+ autoload :HasOneReflection
24
+ autoload :HasManyReflection
101
25
  end
102
26
 
103
- class HasMany < Config #:nodoc:
104
- def key
105
- if key = option(:key)
106
- key
107
- elsif embed_ids?
108
- "#{@name.to_s.singularize}_ids".to_sym
109
- else
110
- @name
111
- end
112
- end
113
-
114
- def embed_key
115
- if key = option(:embed_key)
116
- key
117
- else
118
- :id
119
- end
120
- end
121
-
122
- def serialize
123
- associated_object.map do |item|
124
- find_serializable(item).serializable_hash
125
- end
126
- end
127
-
128
- def serializables
129
- associated_object.map do |item|
130
- find_serializable(item)
131
- end
132
- end
133
-
134
- def serialize_ids
135
- ids_key = "#{@name.to_s.singularize}_ids".to_sym
136
- if !option(:embed_key) && !source_serializer.respond_to?(@name.to_s) && source_serializer.object.respond_to?(ids_key)
137
- source_serializer.object.read_attribute_for_serialization(ids_key)
138
- else
139
- associated_object.map do |item|
140
- item.read_attribute_for_serialization(embed_key)
141
- end
142
- end
27
+ module ClassMethods
28
+ def inherited(base)
29
+ base._reflections = self._reflections.try(:dup) || []
30
+ end
31
+
32
+ # @param [Symbol] name of the association
33
+ # @param [Hash<Symbol => any>] options for the reflection
34
+ # @return [void]
35
+ #
36
+ # @example
37
+ # has_many :comments, serializer: CommentSummarySerializer
38
+ #
39
+ def has_many(name, options = {})
40
+ associate HasManyReflection.new(name, options)
41
+ end
42
+
43
+ # @param [Symbol] name of the association
44
+ # @param [Hash<Symbol => any>] options for the reflection
45
+ # @return [void]
46
+ #
47
+ # @example
48
+ # belongs_to :author, serializer: AuthorSerializer
49
+ #
50
+ def belongs_to(name, options = {})
51
+ associate BelongsToReflection.new(name, options)
52
+ end
53
+
54
+ # @param [Symbol] name of the association
55
+ # @param [Hash<Symbol => any>] options for the reflection
56
+ # @return [void]
57
+ #
58
+ # @example
59
+ # has_one :author, serializer: AuthorSerializer
60
+ #
61
+ def has_one(name, options = {})
62
+ associate HasOneReflection.new(name, options)
63
+ end
64
+
65
+ private
66
+
67
+ # Add reflection and define {name} accessor.
68
+ # @param [ActiveModel::Serializer::Reflection] reflection
69
+ # @return [void]
70
+ #
71
+ # @api private
72
+ #
73
+ def associate(reflection)
74
+ self._reflections = _reflections.dup
75
+
76
+ define_method reflection.name do
77
+ object.send reflection.name
78
+ end unless method_defined?(reflection.name)
79
+
80
+ self._reflections << reflection
143
81
  end
144
82
  end
145
83
 
146
- class HasOne < Config #:nodoc:
147
- def embeddable?
148
- if polymorphic? && associated_object.nil?
149
- false
150
- else
151
- true
152
- end
153
- end
154
-
155
- def polymorphic?
156
- option :polymorphic
157
- end
158
-
159
- def root
160
- if root = option(:root)
161
- root
162
- elsif polymorphic?
163
- associated_object.class.to_s.pluralize.demodulize.underscore.to_sym
164
- else
165
- @name.to_s.pluralize.to_sym
166
- end
167
- end
168
-
169
- def key
170
- if key = option(:key)
171
- key
172
- elsif embed_ids? && !polymorphic?
173
- "#{@name}_id".to_sym
174
- else
175
- @name
176
- end
177
- end
178
-
179
- def embed_key
180
- if key = option(:embed_key)
181
- key
182
- else
183
- :id
184
- end
185
- end
186
-
187
- def polymorphic_key
188
- associated_object.class.to_s.demodulize.underscore.to_sym
189
- end
190
-
191
- def serialize
192
- object = associated_object
193
-
194
- if object && polymorphic?
195
- {
196
- :type => polymorphic_key,
197
- polymorphic_key => find_serializable(object).serializable_hash
198
- }
199
- elsif object
200
- find_serializable(object).serializable_hash
201
- end
202
- end
203
-
204
- def serializables
205
- object = associated_object
206
- value = object && find_serializable(object)
207
- value ? [value] : []
208
- end
209
-
210
- def serialize_ids
211
- id_key = "#{@name}_id".to_sym
84
+ # @return [Enumerator<Association>]
85
+ #
86
+ def associations
87
+ return unless object
212
88
 
213
- if polymorphic?
214
- if associated_object
215
- {
216
- :type => polymorphic_key,
217
- :id => associated_object.read_attribute_for_serialization(embed_key)
218
- }
219
- else
220
- nil
221
- end
222
- elsif !option(:embed_key) && !source_serializer.respond_to?(@name.to_s) && source_serializer.object.respond_to?(id_key)
223
- source_serializer.object.read_attribute_for_serialization(id_key)
224
- elsif associated_object
225
- associated_object.read_attribute_for_serialization(embed_key)
226
- else
227
- nil
89
+ Enumerator.new do |y|
90
+ self.class._reflections.each do |reflection|
91
+ y.yield reflection.build_association(self, options)
228
92
  end
229
93
  end
230
94
  end
@@ -0,0 +1,10 @@
1
+ module ActiveModel
2
+ class Serializer
3
+ # @api private
4
+ class BelongsToReflection < SingularReflection
5
+ def macro
6
+ :belongs_to
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,7 @@
1
+ module ActiveModel
2
+ class Serializer
3
+ # @api private
4
+ class CollectionReflection < Reflection
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,14 @@
1
+ module ActiveModel
2
+ class Serializer
3
+ module Configuration
4
+ include ActiveSupport::Configurable
5
+ extend ActiveSupport::Concern
6
+
7
+ included do |base|
8
+ base.config.array_serializer = ActiveModel::Serializer::ArraySerializer
9
+ base.config.adapter = :flatten_json
10
+ base.config.jsonapi_resource_type = :plural
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,40 @@
1
+ module ActiveModel
2
+ class Serializer
3
+ class Fieldset
4
+ def initialize(fields, root = nil)
5
+ @root = root
6
+ @raw_fields = fields
7
+ end
8
+
9
+ def fields
10
+ @fields ||= parsed_fields
11
+ end
12
+
13
+ def fields_for(serializer)
14
+ key = serializer.json_key
15
+ fields[key.to_sym] || fields[key.pluralize.to_sym]
16
+ end
17
+
18
+ private
19
+
20
+ ActiveModelSerializers.silence_warnings do
21
+ attr_reader :raw_fields, :root
22
+ end
23
+
24
+ def parsed_fields
25
+ if raw_fields.is_a?(Hash)
26
+ raw_fields.inject({}) { |h, (k, v)| h[k.to_sym] = v.map(&:to_sym); h }
27
+ elsif raw_fields.is_a?(Array)
28
+ if root.nil?
29
+ raise ArgumentError, 'The root argument must be specified if the fields argument is an array.'
30
+ end
31
+ hash = {}
32
+ hash[root.to_sym] = raw_fields.map(&:to_sym)
33
+ hash
34
+ else
35
+ {}
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,10 @@
1
+ module ActiveModel
2
+ class Serializer
3
+ # @api private
4
+ class HasManyReflection < CollectionReflection
5
+ def macro
6
+ :has_many
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ module ActiveModel
2
+ class Serializer
3
+ # @api private
4
+ class HasOneReflection < SingularReflection
5
+ def macro
6
+ :has_one
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,129 @@
1
+ module ActiveModel::Serializer::Lint
2
+ # == Active \Model \Serializer \Lint \Tests
3
+ #
4
+ # You can test whether an object is compliant with the Active \Model \Serializers
5
+ # API by including <tt>ActiveModel::Serializer::Lint::Tests</tt> in your TestCase.
6
+ # It will include tests that tell you whether your object is fully compliant,
7
+ # or if not, which aspects of the API are not implemented.
8
+ #
9
+ # Note an object is not required to implement all APIs in order to work
10
+ # with Active \Model \Serializers. This module only intends to provide guidance in case
11
+ # you want all features out of the box.
12
+ #
13
+ # These tests do not attempt to determine the semantic correctness of the
14
+ # returned values. For instance, you could implement <tt>serializable_hash</tt> to
15
+ # always return +{}+, and the tests would pass. It is up to you to ensure
16
+ # that the values are semantically meaningful.
17
+ module Tests
18
+ # Passes if the object responds to <tt>serializable_hash</tt> and if it takes
19
+ # zero or one arguments.
20
+ # Fails otherwise.
21
+ #
22
+ # <tt>serializable_hash</tt> returns a hash representation of a object's attributes.
23
+ # Typically, it is implemented by including ActiveModel::Serialization.
24
+ def test_serializable_hash
25
+ assert_respond_to resource, :serializable_hash, 'The resource should respond to serializable_hash'
26
+ resource.serializable_hash
27
+ resource.serializable_hash(nil)
28
+ end
29
+
30
+ # Passes if the object responds to <tt>read_attribute_for_serialization</tt>
31
+ # and if it requires one argument (the attribute to be read).
32
+ # Fails otherwise.
33
+ #
34
+ # <tt>read_attribute_for_serialization</tt> gets the attribute value for serialization
35
+ # Typically, it is implemented by including ActiveModel::Serialization.
36
+ def test_read_attribute_for_serialization
37
+ assert_respond_to resource, :read_attribute_for_serialization, 'The resource should respond to read_attribute_for_serialization'
38
+ actual_arity = resource.method(:read_attribute_for_serialization).arity
39
+ if defined?(::Rubinius)
40
+ # 1 for def read_attribute_for_serialization(name); end
41
+ # -2 for alias :read_attribute_for_serialization :send for rbx because :shrug:
42
+ assert_includes [1, -2], actual_arity, "expected #{actual_arity.inspect} to be 1 or -2"
43
+ else
44
+ # using absolute value since arity is:
45
+ # 1 for def read_attribute_for_serialization(name); end
46
+ # -1 for alias :read_attribute_for_serialization :send
47
+ assert_includes [1, -1], actual_arity, "expected #{actual_arity.inspect} to be 1 or -1"
48
+ end
49
+ end
50
+
51
+ # Passes if the object responds to <tt>as_json</tt> and if it takes
52
+ # zero or one arguments.
53
+ # Fails otherwise.
54
+ #
55
+ # <tt>as_json</tt> returns a hash representation of a serialized object.
56
+ # It may delegate to <tt>serializable_hash</tt>
57
+ # Typically, it is implemented either by including ActiveModel::Serialization
58
+ # which includes ActiveModel::Serializers::JSON.
59
+ # or by the JSON gem when required.
60
+ def test_as_json
61
+ assert_respond_to resource, :as_json
62
+ resource.as_json
63
+ resource.as_json(nil)
64
+ end
65
+
66
+ # Passes if the object responds to <tt>to_json</tt> and if it takes
67
+ # zero or one arguments.
68
+ # Fails otherwise.
69
+ #
70
+ # <tt>to_json</tt> returns a string representation (JSON) of a serialized object.
71
+ # It may be called on the result of <tt>as_json</tt>.
72
+ # Typically, it is implemented on all objects when the JSON gem is required.
73
+ def test_to_json
74
+ assert_respond_to resource, :to_json
75
+ resource.to_json
76
+ resource.to_json(nil)
77
+ end
78
+
79
+ # Passes if the object responds to <tt>cache_key</tt> and if it takes no
80
+ # arguments (Rails 4.0) or a splat (Rails 4.1+).
81
+ # Fails otherwise.
82
+ #
83
+ # <tt>cache_key</tt> returns a (self-expiring) unique key for the object,
84
+ # which is used by the adapter.
85
+ # It is not required unless caching is enabled.
86
+ def test_cache_key
87
+ assert_respond_to resource, :cache_key
88
+ actual_arity = resource.method(:cache_key).arity
89
+ # using absolute value since arity is:
90
+ # 0 for Rails 4.1+, *timestamp_names
91
+ # -1 for Rails 4.0, no arguments
92
+ assert_includes [-1, 0], actual_arity, "expected #{actual_arity.inspect} to be 0 or -1"
93
+ end
94
+
95
+ # Passes if the object responds to <tt>id</tt> and if it takes no
96
+ # arguments.
97
+ # Fails otherwise.
98
+ #
99
+ # <tt>id</tt> returns a unique identifier for the object.
100
+ # It is not required unless caching is enabled.
101
+ def test_id
102
+ assert_respond_to resource, :id
103
+ assert_equal resource.method(:id).arity, 0
104
+ end
105
+
106
+ # Passes if the object's class responds to <tt>model_name</tt> and if it
107
+ # is in an instance of +ActiveModel::Name+.
108
+ # Fails otherwise.
109
+ #
110
+ # <tt>model_name</tt> returns an ActiveModel::Name instance.
111
+ # It is used by the serializer to identify the object's type.
112
+ # It is not required unless caching is enabled.
113
+ def test_model_name
114
+ resource_class = resource.class
115
+ assert_respond_to resource_class, :model_name
116
+ assert_instance_of resource_class.model_name, ActiveModel::Name
117
+ end
118
+
119
+ private
120
+
121
+ def resource
122
+ @resource or fail "'@resource' must be set as the linted object"
123
+ end
124
+
125
+ def assert_instance_of(result, name)
126
+ assert result.instance_of?(name), "#{result} should be an instance of #{name}"
127
+ end
128
+ end
129
+ end
@@ -0,0 +1,15 @@
1
+ require 'rails/railtie'
2
+ module ActiveModel
3
+ class Railtie < Rails::Railtie
4
+ initializer 'active_model_serializers.logger' do
5
+ ActiveSupport.on_load(:action_controller) do
6
+ ActiveModelSerializers.logger = ActionController::Base.logger
7
+ end
8
+ end
9
+
10
+ initializer 'generators' do |app|
11
+ app.load_generators
12
+ require 'generators/serializer/resource_override'
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,74 @@
1
+ module ActiveModel
2
+ class Serializer
3
+ # Holds all the meta-data about an association as it was specified in the
4
+ # ActiveModel::Serializer class.
5
+ #
6
+ # @example
7
+ # class PostSerializer < ActiveModel::Serializer
8
+ # has_one :author, serializer: AuthorSerializer
9
+ # has_many :comments
10
+ # end
11
+ #
12
+ # PostSerializer._reflections #=>
13
+ # # [
14
+ # # HasOneReflection.new(:author, serializer: AuthorSerializer),
15
+ # # HasManyReflection.new(:comments)
16
+ # # ]
17
+ #
18
+ # So you can inspect reflections in your Adapters.
19
+ #
20
+ Reflection = Struct.new(:name, :options) do
21
+ # Build association. This method is used internally to
22
+ # build serializer's association by its reflection.
23
+ #
24
+ # @param [Serializer] subject is a parent serializer for given association
25
+ # @param [Hash{Symbol => Object}] parent_serializer_options
26
+ #
27
+ # @example
28
+ # # Given the following serializer defined:
29
+ # class PostSerializer < ActiveModel::Serializer
30
+ # has_many :comments, serializer: CommentSummarySerializer
31
+ # end
32
+ #
33
+ # # Then you instantiate your serializer
34
+ # post_serializer = PostSerializer.new(post, foo: 'bar') #
35
+ # # to build association for comments you need to get reflection
36
+ # comments_reflection = PostSerializer._reflections.detect { |r| r.name == :comments }
37
+ # # and #build_association
38
+ # comments_reflection.build_association(post_serializer, foo: 'bar')
39
+ #
40
+ # @api private
41
+ #
42
+ def build_association(subject, parent_serializer_options)
43
+ association_value = subject.send(name)
44
+ reflection_options = options.dup
45
+ serializer_class = ActiveModel::Serializer.serializer_for(association_value, reflection_options)
46
+
47
+ if serializer_class
48
+ begin
49
+ serializer = serializer_class.new(
50
+ association_value,
51
+ serializer_options(parent_serializer_options, reflection_options)
52
+ )
53
+ rescue ActiveModel::Serializer::ArraySerializer::NoSerializerError
54
+ reflection_options[:virtual_value] = association_value.try(:as_json) || association_value
55
+ end
56
+ elsif !association_value.nil? && !association_value.instance_of?(Object)
57
+ reflection_options[:virtual_value] = association_value
58
+ end
59
+
60
+ Association.new(name, serializer, reflection_options)
61
+ end
62
+
63
+ private
64
+
65
+ def serializer_options(parent_serializer_options, reflection_options)
66
+ serializer = reflection_options.fetch(:serializer, nil)
67
+
68
+ serializer_options = parent_serializer_options.except(:serializer)
69
+ serializer_options[:serializer] = serializer if serializer
70
+ serializer_options
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,7 @@
1
+ module ActiveModel
2
+ class Serializer
3
+ # @api private
4
+ class SingularReflection < Reflection
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,35 @@
1
+ module ActiveModel::Serializer::Utils
2
+ module_function
3
+
4
+ # Translates a comma separated list of dot separated paths (JSONAPI format) into a Hash.
5
+ # Example: `'posts.author, posts.comments.upvotes, posts.comments.author'` would become `{ posts: { author: {}, comments: { author: {}, upvotes: {} } } }`.
6
+ #
7
+ # @param [String] included
8
+ # @return [Hash] a Hash representing the same tree structure
9
+ def include_string_to_hash(included)
10
+ included.delete(' ').split(',').inject({}) do |hash, path|
11
+ hash.deep_merge!(path.split('.').reverse_each.inject({}) { |a, e| { e.to_sym => a } })
12
+ end
13
+ end
14
+
15
+ # Translates the arguments passed to the include option into a Hash. The format can be either
16
+ # a String (see #include_string_to_hash), an Array of Symbols and Hashes, or a mix of both.
17
+ # Example: `posts: [:author, comments: [:author, :upvotes]]` would become `{ posts: { author: {}, comments: { author: {}, upvotes: {} } } }`.
18
+ #
19
+ # @param [Symbol, Hash, Array, String] included
20
+ # @return [Hash] a Hash representing the same tree structure
21
+ def include_args_to_hash(included)
22
+ case included
23
+ when Symbol
24
+ { included => {} }
25
+ when Hash
26
+ included.each_with_object({}) { |(key, value), hash| hash[key] = include_args_to_hash(value) }
27
+ when Array
28
+ included.inject({}) { |a, e| a.merge!(include_args_to_hash(e)) }
29
+ when String
30
+ include_string_to_hash(included)
31
+ else
32
+ {}
33
+ end
34
+ end
35
+ end
@@ -1,5 +1,5 @@
1
1
  module ActiveModel
2
2
  class Serializer
3
- VERSION = "0.8.3"
3
+ VERSION = '0.10.0.rc3'
4
4
  end
5
5
  end