active_model_serializers 0.8.3 → 0.10.13

Sign up to get free protection for your applications and to get access to all the features.
Files changed (98) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +702 -6
  3. data/{MIT-LICENSE.txt → MIT-LICENSE} +3 -2
  4. data/README.md +194 -545
  5. data/lib/action_controller/serialization.rb +53 -38
  6. data/lib/active_model/serializable_resource.rb +13 -0
  7. data/lib/active_model/serializer/adapter/attributes.rb +17 -0
  8. data/lib/active_model/serializer/adapter/base.rb +20 -0
  9. data/lib/active_model/serializer/adapter/json.rb +17 -0
  10. data/lib/active_model/serializer/adapter/json_api.rb +17 -0
  11. data/lib/active_model/serializer/adapter/null.rb +17 -0
  12. data/lib/active_model/serializer/adapter.rb +26 -0
  13. data/lib/active_model/serializer/array_serializer.rb +14 -0
  14. data/lib/active_model/serializer/association.rb +73 -0
  15. data/lib/active_model/serializer/attribute.rb +27 -0
  16. data/lib/active_model/serializer/belongs_to_reflection.rb +13 -0
  17. data/lib/active_model/serializer/collection_serializer.rb +99 -0
  18. data/lib/active_model/serializer/concerns/caching.rb +305 -0
  19. data/lib/active_model/serializer/error_serializer.rb +16 -0
  20. data/lib/active_model/serializer/errors_serializer.rb +34 -0
  21. data/lib/active_model/serializer/field.rb +92 -0
  22. data/lib/active_model/serializer/fieldset.rb +33 -0
  23. data/lib/active_model/serializer/has_many_reflection.rb +12 -0
  24. data/lib/active_model/serializer/has_one_reflection.rb +9 -0
  25. data/lib/active_model/serializer/lazy_association.rb +99 -0
  26. data/lib/active_model/serializer/link.rb +23 -0
  27. data/lib/active_model/serializer/lint.rb +152 -0
  28. data/lib/active_model/serializer/null.rb +19 -0
  29. data/lib/active_model/serializer/reflection.rb +212 -0
  30. data/lib/active_model/serializer/version.rb +7 -0
  31. data/lib/active_model/serializer.rb +354 -442
  32. data/lib/active_model_serializers/adapter/attributes.rb +36 -0
  33. data/lib/active_model_serializers/adapter/base.rb +85 -0
  34. data/lib/active_model_serializers/adapter/json.rb +23 -0
  35. data/lib/active_model_serializers/adapter/json_api/deserialization.rb +215 -0
  36. data/lib/active_model_serializers/adapter/json_api/error.rb +98 -0
  37. data/lib/active_model_serializers/adapter/json_api/jsonapi.rb +51 -0
  38. data/lib/active_model_serializers/adapter/json_api/link.rb +85 -0
  39. data/lib/active_model_serializers/adapter/json_api/meta.rb +39 -0
  40. data/lib/active_model_serializers/adapter/json_api/pagination_links.rb +94 -0
  41. data/lib/active_model_serializers/adapter/json_api/relationship.rb +106 -0
  42. data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +68 -0
  43. data/lib/active_model_serializers/adapter/json_api.rb +535 -0
  44. data/lib/active_model_serializers/adapter/null.rb +11 -0
  45. data/lib/active_model_serializers/adapter.rb +100 -0
  46. data/lib/active_model_serializers/callbacks.rb +57 -0
  47. data/lib/active_model_serializers/deprecate.rb +56 -0
  48. data/lib/active_model_serializers/deserialization.rb +17 -0
  49. data/lib/active_model_serializers/json_pointer.rb +16 -0
  50. data/lib/active_model_serializers/logging.rb +124 -0
  51. data/lib/active_model_serializers/lookup_chain.rb +82 -0
  52. data/lib/active_model_serializers/model/caching.rb +26 -0
  53. data/lib/active_model_serializers/model.rb +132 -0
  54. data/lib/active_model_serializers/railtie.rb +52 -0
  55. data/lib/active_model_serializers/register_jsonapi_renderer.rb +80 -0
  56. data/lib/active_model_serializers/serializable_resource.rb +84 -0
  57. data/lib/active_model_serializers/serialization_context.rb +41 -0
  58. data/lib/active_model_serializers/test/schema.rb +140 -0
  59. data/lib/active_model_serializers/test/serializer.rb +127 -0
  60. data/lib/active_model_serializers/test.rb +9 -0
  61. data/lib/active_model_serializers.rb +49 -81
  62. data/lib/generators/rails/USAGE +6 -0
  63. data/lib/generators/rails/resource_override.rb +12 -0
  64. data/lib/generators/rails/serializer_generator.rb +38 -0
  65. data/lib/generators/rails/templates/serializer.rb.erb +8 -0
  66. data/lib/grape/active_model_serializers.rb +18 -0
  67. data/lib/grape/formatters/active_model_serializers.rb +34 -0
  68. data/lib/grape/helpers/active_model_serializers.rb +19 -0
  69. data/lib/tasks/rubocop.rake +55 -0
  70. metadata +265 -51
  71. data/.gitignore +0 -18
  72. data/.travis.yml +0 -28
  73. data/DESIGN.textile +0 -586
  74. data/Gemfile +0 -4
  75. data/Gemfile.edge +0 -9
  76. data/Rakefile +0 -18
  77. data/active_model_serializers.gemspec +0 -24
  78. data/bench/perf.rb +0 -43
  79. data/cruft.md +0 -19
  80. data/lib/active_model/array_serializer.rb +0 -104
  81. data/lib/active_model/serializer/associations.rb +0 -233
  82. data/lib/active_model/serializers/version.rb +0 -5
  83. data/lib/active_record/serializer_override.rb +0 -16
  84. data/lib/generators/resource_override.rb +0 -13
  85. data/lib/generators/serializer/USAGE +0 -9
  86. data/lib/generators/serializer/serializer_generator.rb +0 -42
  87. data/lib/generators/serializer/templates/serializer.rb +0 -19
  88. data/test/array_serializer_test.rb +0 -75
  89. data/test/association_test.rb +0 -592
  90. data/test/caching_test.rb +0 -96
  91. data/test/generators_test.rb +0 -85
  92. data/test/no_serialization_scope_test.rb +0 -34
  93. data/test/serialization_scope_name_test.rb +0 -67
  94. data/test/serialization_test.rb +0 -392
  95. data/test/serializer_support_test.rb +0 -51
  96. data/test/serializer_test.rb +0 -1465
  97. data/test/test_fakes.rb +0 -217
  98. data/test/test_helper.rb +0 -32
@@ -0,0 +1,152 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveModel
4
+ class Serializer
5
+ module Lint
6
+ # == Active \Model \Serializer \Lint \Tests
7
+ #
8
+ # You can test whether an object is compliant with the Active \Model \Serializers
9
+ # API by including <tt>ActiveModel::Serializer::Lint::Tests</tt> in your TestCase.
10
+ # It will include tests that tell you whether your object is fully compliant,
11
+ # or if not, which aspects of the API are not implemented.
12
+ #
13
+ # Note an object is not required to implement all APIs in order to work
14
+ # with Active \Model \Serializers. This module only intends to provide guidance in case
15
+ # you want all features out of the box.
16
+ #
17
+ # These tests do not attempt to determine the semantic correctness of the
18
+ # returned values. For instance, you could implement <tt>serializable_hash</tt> to
19
+ # always return +{}+, and the tests would pass. It is up to you to ensure
20
+ # that the values are semantically meaningful.
21
+ module Tests
22
+ # Passes if the object responds to <tt>serializable_hash</tt> and if it takes
23
+ # zero or one arguments.
24
+ # Fails otherwise.
25
+ #
26
+ # <tt>serializable_hash</tt> returns a hash representation of a object's attributes.
27
+ # Typically, it is implemented by including ActiveModel::Serialization.
28
+ def test_serializable_hash
29
+ assert_respond_to resource, :serializable_hash, 'The resource should respond to serializable_hash'
30
+ resource.serializable_hash
31
+ resource.serializable_hash(nil)
32
+ end
33
+
34
+ # Passes if the object responds to <tt>read_attribute_for_serialization</tt>
35
+ # and if it requires one argument (the attribute to be read).
36
+ # Fails otherwise.
37
+ #
38
+ # <tt>read_attribute_for_serialization</tt> gets the attribute value for serialization
39
+ # Typically, it is implemented by including ActiveModel::Serialization.
40
+ def test_read_attribute_for_serialization
41
+ assert_respond_to resource, :read_attribute_for_serialization, 'The resource should respond to read_attribute_for_serialization'
42
+ actual_arity = resource.method(:read_attribute_for_serialization).arity
43
+ # using absolute value since arity is:
44
+ # 1 for def read_attribute_for_serialization(name); end
45
+ # -1 for alias :read_attribute_for_serialization :send
46
+ assert_equal 1, actual_arity.abs, "expected #{actual_arity.inspect}.abs to be 1 or -1"
47
+ end
48
+
49
+ # Passes if the object responds to <tt>as_json</tt> and if it takes
50
+ # zero or one arguments.
51
+ # Fails otherwise.
52
+ #
53
+ # <tt>as_json</tt> returns a hash representation of a serialized object.
54
+ # It may delegate to <tt>serializable_hash</tt>
55
+ # Typically, it is implemented either by including ActiveModel::Serialization
56
+ # which includes ActiveModel::Serializers::JSON.
57
+ # or by the JSON gem when required.
58
+ def test_as_json
59
+ assert_respond_to resource, :as_json
60
+ resource.as_json
61
+ resource.as_json(nil)
62
+ end
63
+
64
+ # Passes if the object responds to <tt>to_json</tt> and if it takes
65
+ # zero or one arguments.
66
+ # Fails otherwise.
67
+ #
68
+ # <tt>to_json</tt> returns a string representation (JSON) of a serialized object.
69
+ # It may be called on the result of <tt>as_json</tt>.
70
+ # Typically, it is implemented on all objects when the JSON gem is required.
71
+ def test_to_json
72
+ assert_respond_to resource, :to_json
73
+ resource.to_json
74
+ resource.to_json(nil)
75
+ end
76
+
77
+ # Passes if the object responds to <tt>cache_key</tt>
78
+ # Fails otherwise.
79
+ #
80
+ # <tt>cache_key</tt> returns a (self-expiring) unique key for the object,
81
+ # and is part of the (self-expiring) cache_key, which is used by the
82
+ # adapter. It is not required unless caching is enabled.
83
+ def test_cache_key
84
+ assert_respond_to resource, :cache_key
85
+ actual_arity = resource.method(:cache_key).arity
86
+ assert_includes [-1, 0], actual_arity, "expected #{actual_arity.inspect} to be 0 or -1"
87
+ end
88
+
89
+ # Passes if the object responds to <tt>updated_at</tt> and if it takes no
90
+ # arguments.
91
+ # Fails otherwise.
92
+ #
93
+ # <tt>updated_at</tt> returns a Time object or iso8601 string and
94
+ # is part of the (self-expiring) cache_key, which is used by the adapter.
95
+ # It is not required unless caching is enabled.
96
+ def test_updated_at
97
+ assert_respond_to resource, :updated_at
98
+ actual_arity = resource.method(:updated_at).arity
99
+ assert_equal 0, actual_arity
100
+ end
101
+
102
+ # Passes if the object responds to <tt>id</tt> and if it takes no
103
+ # arguments.
104
+ # Fails otherwise.
105
+ #
106
+ # <tt>id</tt> returns a unique identifier for the object.
107
+ # It is not required unless caching is enabled.
108
+ def test_id
109
+ assert_respond_to resource, :id
110
+ assert_equal 0, resource.method(:id).arity
111
+ end
112
+
113
+ # Passes if the object's class responds to <tt>model_name</tt> and if it
114
+ # is in an instance of +ActiveModel::Name+.
115
+ # Fails otherwise.
116
+ #
117
+ # <tt>model_name</tt> returns an ActiveModel::Name instance.
118
+ # It is used by the serializer to identify the object's type.
119
+ # It is not required unless caching is enabled.
120
+ def test_model_name
121
+ resource_class = resource.class
122
+ assert_respond_to resource_class, :model_name
123
+ assert_instance_of resource_class.model_name, ActiveModel::Name
124
+ end
125
+
126
+ def test_active_model_errors
127
+ assert_respond_to resource, :errors
128
+ end
129
+
130
+ def test_active_model_errors_human_attribute_name
131
+ assert_respond_to resource.class, :human_attribute_name
132
+ assert_equal(-2, resource.class.method(:human_attribute_name).arity)
133
+ end
134
+
135
+ def test_active_model_errors_lookup_ancestors
136
+ assert_respond_to resource.class, :lookup_ancestors
137
+ assert_equal 0, resource.class.method(:lookup_ancestors).arity
138
+ end
139
+
140
+ private
141
+
142
+ def resource
143
+ @resource or fail "'@resource' must be set as the linted object"
144
+ end
145
+
146
+ def assert_instance_of(result, name)
147
+ assert result.instance_of?(name), "#{result} should be an instance of #{name}"
148
+ end
149
+ end
150
+ end
151
+ end
152
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveModel
4
+ class Serializer
5
+ class Null < Serializer
6
+ def attributes(*)
7
+ {}
8
+ end
9
+
10
+ def associations(*)
11
+ {}
12
+ end
13
+
14
+ def serializable_hash(*)
15
+ {}
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,212 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_model/serializer/field'
4
+ require 'active_model/serializer/association'
5
+
6
+ module ActiveModel
7
+ class Serializer
8
+ # Holds all the meta-data about an association as it was specified in the
9
+ # ActiveModel::Serializer class.
10
+ #
11
+ # @example
12
+ # class PostSerializer < ActiveModel::Serializer
13
+ # has_one :author, serializer: AuthorSerializer
14
+ # belongs_to :boss, type: :users, foreign_key: :boss_id
15
+ # has_many :comments
16
+ # has_many :comments, key: :last_comments do
17
+ # object.comments.last(1)
18
+ # end
19
+ # has_many :secret_meta_data, if: :is_admin?
20
+ #
21
+ # has_one :blog do |serializer|
22
+ # meta count: object.roles.count
23
+ # serializer.cached_blog
24
+ # end
25
+ #
26
+ # private
27
+ #
28
+ # def cached_blog
29
+ # cache_store.fetch("cached_blog:#{object.updated_at}") do
30
+ # Blog.find(object.blog_id)
31
+ # end
32
+ # end
33
+ #
34
+ # def is_admin?
35
+ # current_user.admin?
36
+ # end
37
+ # end
38
+ #
39
+ # Specifically, the association 'comments' is evaluated two different ways:
40
+ # 1) as 'comments' and named 'comments'.
41
+ # 2) as 'object.comments.last(1)' and named 'last_comments'.
42
+ #
43
+ # PostSerializer._reflections # =>
44
+ # # {
45
+ # # author: HasOneReflection.new(:author, serializer: AuthorSerializer),
46
+ # # comments: HasManyReflection.new(:comments)
47
+ # # last_comments: HasManyReflection.new(:comments, { key: :last_comments }, #<Block>)
48
+ # # secret_meta_data: HasManyReflection.new(:secret_meta_data, { if: :is_admin? })
49
+ # # }
50
+ #
51
+ # So you can inspect reflections in your Adapters.
52
+ class Reflection < Field
53
+ attr_reader :foreign_key, :type
54
+
55
+ def initialize(*)
56
+ super
57
+ options[:links] = {}
58
+ options[:include_data_setting] = Serializer.config.include_data_default
59
+ options[:meta] = nil
60
+ @type = options.fetch(:type) do
61
+ class_name = options.fetch(:class_name, name.to_s.camelize.singularize)
62
+ class_name.underscore.pluralize.to_sym
63
+ end
64
+ @foreign_key = options.fetch(:foreign_key) do
65
+ if collection?
66
+ "#{name.to_s.singularize}_ids".to_sym
67
+ else
68
+ "#{name}_id".to_sym
69
+ end
70
+ end
71
+ end
72
+
73
+ # @api public
74
+ # @example
75
+ # has_one :blog do
76
+ # include_data false
77
+ # link :self, 'a link'
78
+ # link :related, 'another link'
79
+ # link :self, '//example.com/link_author/relationships/bio'
80
+ # id = object.profile.id
81
+ # link :related do
82
+ # "//example.com/profiles/#{id}" if id != 123
83
+ # end
84
+ # link :related do
85
+ # ids = object.likes.map(&:id).join(',')
86
+ # href "//example.com/likes/#{ids}"
87
+ # meta ids: ids
88
+ # end
89
+ # end
90
+ def link(name, value = nil, &block)
91
+ options[:links][name] = block_given? ? block : value
92
+ :nil
93
+ end
94
+
95
+ # @api public
96
+ # @example
97
+ # has_one :blog do
98
+ # include_data false
99
+ # meta(id: object.blog.id)
100
+ # meta liked: object.likes.any?
101
+ # link :self do
102
+ # href object.blog.id.to_s
103
+ # meta(id: object.blog.id)
104
+ # end
105
+ def meta(value = nil, &block)
106
+ options[:meta] = block_given? ? block : value
107
+ :nil
108
+ end
109
+
110
+ # @api public
111
+ # @example
112
+ # has_one :blog do
113
+ # include_data false
114
+ # link :self, 'a link'
115
+ # link :related, 'another link'
116
+ # end
117
+ #
118
+ # has_one :blog do
119
+ # include_data false
120
+ # link :self, 'a link'
121
+ # link :related, 'another link'
122
+ # end
123
+ #
124
+ # belongs_to :reviewer do
125
+ # meta name: 'Dan Brown'
126
+ # include_data true
127
+ # end
128
+ #
129
+ # has_many :tags, serializer: TagSerializer do
130
+ # link :self, '//example.com/link_author/relationships/tags'
131
+ # include_data :if_sideloaded
132
+ # end
133
+ def include_data(value = true)
134
+ options[:include_data_setting] = value
135
+ :nil
136
+ end
137
+
138
+ def collection?
139
+ false
140
+ end
141
+
142
+ def include_data?(include_slice)
143
+ include_data_setting = options[:include_data_setting]
144
+ case include_data_setting
145
+ when :if_sideloaded then include_slice.key?(options.fetch(:key, name))
146
+ when true then true
147
+ when false then false
148
+ else fail ArgumentError, "Unknown include_data_setting '#{include_data_setting.inspect}'"
149
+ end
150
+ end
151
+
152
+ # @param serializer [ActiveModel::Serializer]
153
+ # @yield [ActiveModel::Serializer]
154
+ # @return [:nil, associated resource or resource collection]
155
+ def value(serializer, include_slice)
156
+ # NOTE(BF): This method isn't thread-safe because the _reflections class attribute is not thread-safe
157
+ # Therefore, when we build associations from reflections, we dup the entire reflection instance.
158
+ # Better solutions much appreciated!
159
+ @object = serializer.object
160
+ @scope = serializer.scope
161
+
162
+ block_value = instance_exec(serializer, &block) if block
163
+ return unless include_data?(include_slice)
164
+
165
+ if block && block_value != :nil
166
+ block_value
167
+ else
168
+ serializer.read_attribute_for_serialization(name)
169
+ end
170
+ end
171
+
172
+ # @api private
173
+ def foreign_key_on
174
+ :related
175
+ end
176
+
177
+ # Build association. This method is used internally to
178
+ # build serializer's association by its reflection.
179
+ #
180
+ # @param [Serializer] parent_serializer for given association
181
+ # @param [Hash{Symbol => Object}] parent_serializer_options
182
+ #
183
+ # @example
184
+ # # Given the following serializer defined:
185
+ # class PostSerializer < ActiveModel::Serializer
186
+ # has_many :comments, serializer: CommentSummarySerializer
187
+ # end
188
+ #
189
+ # # Then you instantiate your serializer
190
+ # post_serializer = PostSerializer.new(post, foo: 'bar') #
191
+ # # to build association for comments you need to get reflection
192
+ # comments_reflection = PostSerializer._reflections.detect { |r| r.name == :comments }
193
+ # # and #build_association
194
+ # comments_reflection.build_association(post_serializer, foo: 'bar')
195
+ #
196
+ # @api private
197
+ def build_association(parent_serializer, parent_serializer_options, include_slice = {})
198
+ association_options = {
199
+ parent_serializer: parent_serializer,
200
+ parent_serializer_options: parent_serializer_options,
201
+ include_slice: include_slice
202
+ }
203
+ Association.new(self, association_options)
204
+ end
205
+
206
+ protected
207
+
208
+ # used in instance exec
209
+ attr_accessor :object, :scope
210
+ end
211
+ end
212
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveModel
4
+ class Serializer
5
+ VERSION = '0.10.13'.freeze
6
+ end
7
+ end