active_model_serializers 0.10.0.rc4 → 0.10.0.rc5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (179) hide show
  1. checksums.yaml +4 -4
  2. data/.github/ISSUE_TEMPLATE.md +29 -0
  3. data/.github/PULL_REQUEST_TEMPLATE.md +15 -0
  4. data/.gitignore +1 -0
  5. data/.rubocop.yml +19 -1
  6. data/.rubocop_todo.yml +30 -103
  7. data/.simplecov +0 -1
  8. data/.travis.yml +20 -8
  9. data/CHANGELOG.md +89 -5
  10. data/CONTRIBUTING.md +54 -179
  11. data/Gemfile +7 -2
  12. data/{LICENSE.txt → MIT-LICENSE} +0 -0
  13. data/README.md +27 -5
  14. data/Rakefile +44 -16
  15. data/active_model_serializers.gemspec +9 -1
  16. data/appveyor.yml +1 -0
  17. data/bin/bench +171 -0
  18. data/bin/bench_regression +316 -0
  19. data/bin/serve_benchmark +39 -0
  20. data/docs/ARCHITECTURE.md +13 -7
  21. data/docs/README.md +5 -1
  22. data/docs/STYLE.md +58 -0
  23. data/docs/general/adapters.md +99 -16
  24. data/docs/general/configuration_options.md +87 -14
  25. data/docs/general/deserialization.md +100 -0
  26. data/docs/general/getting_started.md +35 -0
  27. data/docs/general/instrumentation.md +1 -1
  28. data/docs/general/key_transforms.md +40 -0
  29. data/docs/general/rendering.md +115 -13
  30. data/docs/general/serializers.md +138 -6
  31. data/docs/howto/add_pagination_links.md +36 -18
  32. data/docs/howto/outside_controller_use.md +4 -4
  33. data/docs/howto/passing_arbitrary_options.md +27 -0
  34. data/docs/jsonapi/errors.md +56 -0
  35. data/docs/jsonapi/schema.md +29 -18
  36. data/docs/rfcs/0000-namespace.md +106 -0
  37. data/docs/rfcs/template.md +15 -0
  38. data/lib/action_controller/serialization.rb +10 -19
  39. data/lib/active_model/serializable_resource.rb +4 -65
  40. data/lib/active_model/serializer.rb +73 -18
  41. data/lib/active_model/serializer/adapter.rb +15 -82
  42. data/lib/active_model/serializer/adapter/attributes.rb +5 -56
  43. data/lib/active_model/serializer/adapter/base.rb +5 -47
  44. data/lib/active_model/serializer/adapter/json.rb +6 -12
  45. data/lib/active_model/serializer/adapter/json_api.rb +5 -213
  46. data/lib/active_model/serializer/adapter/null.rb +7 -3
  47. data/lib/active_model/serializer/array_serializer.rb +3 -3
  48. data/lib/active_model/serializer/association.rb +4 -5
  49. data/lib/active_model/serializer/attributes.rb +1 -1
  50. data/lib/active_model/serializer/caching.rb +56 -5
  51. data/lib/active_model/serializer/collection_serializer.rb +30 -13
  52. data/lib/active_model/serializer/configuration.rb +7 -0
  53. data/lib/active_model/serializer/error_serializer.rb +10 -0
  54. data/lib/active_model/serializer/errors_serializer.rb +27 -0
  55. data/lib/active_model/serializer/links.rb +4 -2
  56. data/lib/active_model/serializer/lint.rb +14 -0
  57. data/lib/active_model/serializer/meta.rb +29 -0
  58. data/lib/active_model/serializer/null.rb +17 -0
  59. data/lib/active_model/serializer/reflection.rb +57 -1
  60. data/lib/active_model/serializer/type.rb +1 -1
  61. data/lib/active_model/serializer/version.rb +1 -1
  62. data/lib/active_model_serializers.rb +17 -0
  63. data/lib/active_model_serializers/adapter.rb +92 -0
  64. data/lib/active_model_serializers/adapter/attributes.rb +94 -0
  65. data/lib/active_model_serializers/adapter/base.rb +90 -0
  66. data/lib/active_model_serializers/adapter/json.rb +11 -0
  67. data/lib/active_model_serializers/adapter/json_api.rb +513 -0
  68. data/lib/active_model_serializers/adapter/json_api/deserialization.rb +213 -0
  69. data/lib/active_model_serializers/adapter/json_api/error.rb +96 -0
  70. data/lib/active_model_serializers/adapter/json_api/jsonapi.rb +49 -0
  71. data/lib/active_model_serializers/adapter/json_api/link.rb +83 -0
  72. data/lib/active_model_serializers/adapter/json_api/meta.rb +37 -0
  73. data/lib/active_model_serializers/adapter/json_api/pagination_links.rb +57 -0
  74. data/lib/active_model_serializers/adapter/json_api/relationship.rb +52 -0
  75. data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +37 -0
  76. data/lib/active_model_serializers/adapter/null.rb +10 -0
  77. data/lib/active_model_serializers/cached_serializer.rb +87 -0
  78. data/lib/active_model_serializers/callbacks.rb +1 -1
  79. data/lib/active_model_serializers/deprecate.rb +55 -0
  80. data/lib/active_model_serializers/deserialization.rb +2 -2
  81. data/lib/active_model_serializers/fragment_cache.rb +118 -0
  82. data/lib/active_model_serializers/json_pointer.rb +14 -0
  83. data/lib/active_model_serializers/key_transform.rb +70 -0
  84. data/lib/active_model_serializers/logging.rb +4 -1
  85. data/lib/active_model_serializers/model.rb +11 -1
  86. data/lib/active_model_serializers/railtie.rb +9 -1
  87. data/lib/active_model_serializers/register_jsonapi_renderer.rb +64 -0
  88. data/lib/active_model_serializers/serializable_resource.rb +81 -0
  89. data/lib/active_model_serializers/serialization_context.rb +24 -2
  90. data/lib/active_model_serializers/test/schema.rb +2 -2
  91. data/lib/grape/formatters/active_model_serializers.rb +1 -1
  92. data/test/action_controller/adapter_selector_test.rb +1 -1
  93. data/test/action_controller/json_api/deserialization_test.rb +56 -3
  94. data/test/action_controller/json_api/errors_test.rb +41 -0
  95. data/test/action_controller/json_api/linked_test.rb +10 -9
  96. data/test/action_controller/json_api/pagination_test.rb +2 -2
  97. data/test/action_controller/json_api/transform_test.rb +180 -0
  98. data/test/action_controller/serialization_scope_name_test.rb +201 -35
  99. data/test/action_controller/serialization_test.rb +39 -7
  100. data/test/active_model_serializers/adapter_for_test.rb +208 -0
  101. data/test/active_model_serializers/cached_serializer_test.rb +80 -0
  102. data/test/active_model_serializers/fragment_cache_test.rb +34 -0
  103. data/test/active_model_serializers/json_pointer_test.rb +20 -0
  104. data/test/active_model_serializers/key_transform_test.rb +263 -0
  105. data/test/active_model_serializers/logging_test.rb +8 -8
  106. data/test/active_model_serializers/railtie_test_isolated.rb +6 -0
  107. data/test/active_model_serializers/serialization_context_test_isolated.rb +58 -0
  108. data/test/adapter/deprecation_test.rb +100 -0
  109. data/test/adapter/json/belongs_to_test.rb +32 -34
  110. data/test/adapter/json/collection_test.rb +73 -75
  111. data/test/adapter/json/has_many_test.rb +36 -38
  112. data/test/adapter/json/transform_test.rb +93 -0
  113. data/test/adapter/json_api/belongs_to_test.rb +127 -129
  114. data/test/adapter/json_api/collection_test.rb +80 -82
  115. data/test/adapter/json_api/errors_test.rb +78 -0
  116. data/test/adapter/json_api/fields_test.rb +68 -70
  117. data/test/adapter/json_api/has_many_embed_ids_test.rb +32 -34
  118. data/test/adapter/json_api/has_many_explicit_serializer_test.rb +75 -77
  119. data/test/adapter/json_api/has_many_test.rb +121 -123
  120. data/test/adapter/json_api/has_one_test.rb +59 -61
  121. data/test/adapter/json_api/json_api_test.rb +28 -30
  122. data/test/adapter/json_api/linked_test.rb +319 -321
  123. data/test/adapter/json_api/links_test.rb +75 -50
  124. data/test/adapter/json_api/pagination_links_test.rb +115 -82
  125. data/test/adapter/json_api/parse_test.rb +114 -116
  126. data/test/adapter/json_api/relationship_test.rb +161 -0
  127. data/test/adapter/json_api/relationships_test.rb +199 -0
  128. data/test/adapter/json_api/resource_identifier_test.rb +85 -0
  129. data/test/adapter/json_api/resource_meta_test.rb +100 -0
  130. data/test/adapter/json_api/toplevel_jsonapi_test.rb +61 -63
  131. data/test/adapter/json_api/transform_test.rb +500 -0
  132. data/test/adapter/json_api/type_test.rb +61 -0
  133. data/test/adapter/json_test.rb +35 -37
  134. data/test/adapter/null_test.rb +13 -15
  135. data/test/adapter/polymorphic_test.rb +72 -0
  136. data/test/adapter_test.rb +27 -29
  137. data/test/array_serializer_test.rb +7 -8
  138. data/test/benchmark/app.rb +65 -0
  139. data/test/benchmark/benchmarking_support.rb +67 -0
  140. data/test/benchmark/bm_caching.rb +117 -0
  141. data/test/benchmark/bm_transform.rb +34 -0
  142. data/test/benchmark/config.ru +3 -0
  143. data/test/benchmark/controllers.rb +77 -0
  144. data/test/benchmark/fixtures.rb +167 -0
  145. data/test/cache_test.rb +388 -0
  146. data/test/collection_serializer_test.rb +10 -0
  147. data/test/fixtures/active_record.rb +12 -0
  148. data/test/fixtures/poro.rb +28 -3
  149. data/test/grape_test.rb +5 -5
  150. data/test/lint_test.rb +9 -0
  151. data/test/serializable_resource_test.rb +59 -3
  152. data/test/serializers/associations_test.rb +8 -8
  153. data/test/serializers/attribute_test.rb +7 -7
  154. data/test/serializers/caching_configuration_test_isolated.rb +170 -0
  155. data/test/serializers/meta_test.rb +74 -6
  156. data/test/serializers/read_attribute_for_serialization_test.rb +79 -0
  157. data/test/serializers/serialization_test.rb +55 -0
  158. data/test/support/isolated_unit.rb +3 -0
  159. data/test/support/rails5_shims.rb +26 -8
  160. data/test/support/rails_app.rb +38 -18
  161. data/test/support/serialization_testing.rb +5 -5
  162. data/test/test_helper.rb +6 -10
  163. metadata +132 -37
  164. data/docs/DESIGN.textile +7 -1
  165. data/lib/active_model/serializer/adapter/cached_serializer.rb +0 -45
  166. data/lib/active_model/serializer/adapter/fragment_cache.rb +0 -111
  167. data/lib/active_model/serializer/adapter/json/fragment_cache.rb +0 -13
  168. data/lib/active_model/serializer/adapter/json_api/deserialization.rb +0 -207
  169. data/lib/active_model/serializer/adapter/json_api/fragment_cache.rb +0 -21
  170. data/lib/active_model/serializer/adapter/json_api/link.rb +0 -44
  171. data/lib/active_model/serializer/adapter/json_api/pagination_links.rb +0 -58
  172. data/test/active_model_serializers/serialization_context_test.rb +0 -18
  173. data/test/adapter/fragment_cache_test.rb +0 -38
  174. data/test/adapter/json_api/resource_type_config_test.rb +0 -71
  175. data/test/serializers/adapter_for_test.rb +0 -166
  176. data/test/serializers/cache_test.rb +0 -209
  177. data/test/support/simplecov.rb +0 -6
  178. data/test/support/stream_capture.rb +0 -50
  179. data/test/support/test_case.rb +0 -19
@@ -1,9 +1,13 @@
1
1
  module ActiveModel
2
2
  class Serializer
3
3
  module Adapter
4
- class Null < Base
5
- def serializable_hash(options = nil)
6
- {}
4
+ class Null < DelegateClass(ActiveModelSerializers::Adapter::Null)
5
+ def initialize(serializer, options = {})
6
+ super(ActiveModelSerializers::Adapter::Null.new(serializer, options))
7
+ end
8
+ class << self
9
+ extend ActiveModelSerializers::Deprecate
10
+ deprecate :new, 'ActiveModelSerializers::Adapter::Null.new'
7
11
  end
8
12
  end
9
13
  end
@@ -1,9 +1,9 @@
1
1
  require 'active_model/serializer/collection_serializer'
2
2
  class ActiveModel::Serializer
3
3
  class ArraySerializer < CollectionSerializer
4
- def initialize(*)
5
- warn "Calling deprecated ArraySerializer in #{caller[0..2].join(', ')}. Please use CollectionSerializer"
6
- super
4
+ class << self
5
+ extend ActiveModelSerializers::Deprecate
6
+ deprecate :new, 'ActiveModel::Serializer::CollectionSerializer.'
7
7
  end
8
8
  end
9
9
  end
@@ -2,16 +2,15 @@ module ActiveModel
2
2
  class Serializer
3
3
  # This class hold all information about serializer's association.
4
4
  #
5
- # @param [Symbol] name
6
- # @param [ActiveModel::Serializer] serializer
7
- # @param [Hash{Symbol => Object}] options
5
+ # @attr [Symbol] name
6
+ # @attr [ActiveModel::Serializer] serializer
7
+ # @attr [Hash{Symbol => Object}] options
8
8
  #
9
9
  # @example
10
10
  # Association.new(:comments, CommentSummarySerializer)
11
11
  #
12
- Association = Struct.new(:name, :serializer, :options) do
12
+ Association = Struct.new(:name, :serializer, :options, :links, :meta) do
13
13
  # @return [Symbol]
14
- #
15
14
  def key
16
15
  options.fetch(:key, name)
17
16
  end
@@ -68,7 +68,7 @@ module ActiveModel
68
68
  # @api private
69
69
  # maps attribute value to explict key name
70
70
  # @see Serializer::attribute
71
- # @see Adapter::FragmentCache#fragment_serializer
71
+ # @see FragmentCache#fragment_serializer
72
72
  def _attributes_keys
73
73
  _attributes_data
74
74
  .each_with_object({}) do |(key, attr), hash|
@@ -5,9 +5,9 @@ module ActiveModel
5
5
 
6
6
  included do
7
7
  with_options instance_writer: false, instance_reader: false do |serializer|
8
- serializer.class_attribute :_cache # @api private : the cache object
8
+ serializer.class_attribute :_cache # @api private : the cache store
9
9
  serializer.class_attribute :_fragmented # @api private : @see ::fragmented
10
- serializer.class_attribute :_cache_key # @api private : when present, is first item in cache_key
10
+ serializer.class_attribute :_cache_key # @api private : when present, is first item in cache_key. Ignored if the serializable object defines #cache_key.
11
11
  serializer.class_attribute :_cache_only # @api private : when fragment caching, whitelists cached_attributes. Cannot combine with except
12
12
  serializer.class_attribute :_cache_except # @api private : when fragment caching, blacklists cached_attributes. Cannot combine with only
13
13
  serializer.class_attribute :_cache_options # @api private : used by CachedSerializer, passed to _cache.fetch
@@ -58,6 +58,10 @@ module ActiveModel
58
58
  ''.freeze
59
59
  end
60
60
 
61
+ def _skip_digest?
62
+ _cache_options && _cache_options[:skip_digest]
63
+ end
64
+
61
65
  # @api private
62
66
  # Used by FragmentCache on the CachedSerializer
63
67
  # to call attribute methods on the fragmented cached serializer.
@@ -70,7 +74,8 @@ module ActiveModel
70
74
  # Sets +::_cache+ object to <tt>ActionController::Base.cache_store</tt>
71
75
  # when Rails.configuration.action_controller.perform_caching
72
76
  #
73
- # @params options [Hash] with valid keys:
77
+ # @param options [Hash] with valid keys:
78
+ # cache_store : @see ::_cache
74
79
  # key : @see ::_cache_key
75
80
  # only : @see ::_cache_only
76
81
  # except : @see ::_cache_except
@@ -88,11 +93,57 @@ module ActiveModel
88
93
  # @todo require less code comments. See
89
94
  # https://github.com/rails-api/active_model_serializers/pull/1249#issuecomment-146567837
90
95
  def cache(options = {})
91
- self._cache = ActiveModelSerializers.config.cache_store if ActiveModelSerializers.config.perform_caching
96
+ self._cache =
97
+ options.delete(:cache_store) ||
98
+ ActiveModelSerializers.config.cache_store ||
99
+ ActiveSupport::Cache.lookup_store(:null_store)
92
100
  self._cache_key = options.delete(:key)
93
101
  self._cache_only = options.delete(:only)
94
102
  self._cache_except = options.delete(:except)
95
- self._cache_options = (options.empty?) ? nil : options
103
+ self._cache_options = options.empty? ? nil : options
104
+ end
105
+
106
+ # Value is from ActiveModelSerializers.config.perform_caching. Is used to
107
+ # globally enable or disable all serializer caching, just like
108
+ # Rails.configuration.action_controller.perform_caching, which is its
109
+ # default value in a Rails application.
110
+ # @return [true, false]
111
+ # Memoizes value of config first time it is called with a non-nil value.
112
+ # rubocop:disable Style/ClassVars
113
+ def perform_caching
114
+ return @@perform_caching if defined?(@@perform_caching) && !@@perform_caching.nil?
115
+ @@perform_caching = ActiveModelSerializers.config.perform_caching
116
+ end
117
+ alias perform_caching? perform_caching
118
+ # rubocop:enable Style/ClassVars
119
+
120
+ # The canonical method for getting the cache store for the serializer.
121
+ #
122
+ # @return [nil] when _cache is not set (i.e. when `cache` has not been called)
123
+ # @return [._cache] when _cache is not the NullStore
124
+ # @return [ActiveModelSerializers.config.cache_store] when _cache is the NullStore.
125
+ # This is so we can use `cache` being called to mean the serializer should be cached
126
+ # even if ActiveModelSerializers.config.cache_store has not yet been set.
127
+ # That means that when _cache is the NullStore and ActiveModelSerializers.config.cache_store
128
+ # is configured, `cache_store` becomes `ActiveModelSerializers.config.cache_store`.
129
+ # @return [nil] when _cache is the NullStore and ActiveModelSerializers.config.cache_store is nil.
130
+ def cache_store
131
+ return nil if _cache.nil?
132
+ return _cache if _cache.class != ActiveSupport::Cache::NullStore
133
+ if ActiveModelSerializers.config.cache_store
134
+ self._cache = ActiveModelSerializers.config.cache_store
135
+ else
136
+ nil
137
+ end
138
+ end
139
+
140
+ def cache_enabled?
141
+ perform_caching? && cache_store && !_cache_only && !_cache_except
142
+ end
143
+
144
+ def fragment_cache_enabled?
145
+ perform_caching? && cache_store &&
146
+ (_cache_only && !_cache_except || !_cache_only && _cache_except)
96
147
  end
97
148
  end
98
149
  end
@@ -8,13 +8,14 @@ module ActiveModel
8
8
  attr_reader :object, :root
9
9
 
10
10
  def initialize(resources, options = {})
11
- @root = options[:root]
12
- @object = resources
11
+ @object = resources
12
+ @options = options
13
+ @root = options[:root]
14
+ serializer_context_class = options.fetch(:serializer_context_class, ActiveModel::Serializer)
13
15
  @serializers = resources.map do |resource|
14
- serializer_context_class = options.fetch(:serializer_context_class, ActiveModel::Serializer)
15
16
  serializer_class = options.fetch(:serializer) { serializer_context_class.serializer_for(resource) }
16
17
 
17
- if serializer_class.nil?
18
+ if serializer_class.nil? # rubocop:disable Style/GuardClause
18
19
  fail NoSerializerError, "No serializer found for resource: #{resource.inspect}"
19
20
  else
20
21
  serializer_class.new(resource, options.except(:serializer))
@@ -22,9 +23,32 @@ module ActiveModel
22
23
  end
23
24
  end
24
25
 
26
+ def success?
27
+ true
28
+ end
29
+
30
+ # TODO: unify naming of root, json_key, and _type. Right now, a serializer's
31
+ # json_key comes from the root option or the object's model name, by default.
32
+ # But, if a dev defines a custom `json_key` method with an explicit value,
33
+ # we have no simple way to know that it is safe to call that instance method.
34
+ # (which is really a class property at this point, anyhow).
35
+ # rubocop:disable Metrics/CyclomaticComplexity
36
+ # Disabling cop since it's good to highlight the complexity of this method by
37
+ # including all the logic right here.
25
38
  def json_key
26
- root || derived_root
39
+ return root if root
40
+ # 1. get from options[:serializer] for empty resource collection
41
+ key = object.empty? &&
42
+ (explicit_serializer_class = options[:serializer]) &&
43
+ explicit_serializer_class._type
44
+ # 2. get from first serializer instance in collection
45
+ key ||= (serializer = serializers.first) && serializer.json_key
46
+ # 3. get from collection name, if a named collection
47
+ key ||= object.respond_to?(:name) ? object.name && object.name.underscore : nil
48
+ # 4. key may be nil for empty collection and no serializer option
49
+ key && key.pluralize
27
50
  end
51
+ # rubocop:enable Metrics/CyclomaticComplexity
28
52
 
29
53
  def paginated?
30
54
  object.respond_to?(:current_page) &&
@@ -34,14 +58,7 @@ module ActiveModel
34
58
 
35
59
  protected
36
60
 
37
- attr_reader :serializers
38
-
39
- private
40
-
41
- def derived_root
42
- key = serializers.first.try(:json_key) || object.try(:name).try(:underscore)
43
- key.try(:pluralize)
44
- end
61
+ attr_reader :serializers, :options
45
62
  end
46
63
  end
47
64
  end
@@ -21,6 +21,13 @@ module ActiveModel
21
21
 
22
22
  config.adapter = :attributes
23
23
  config.jsonapi_resource_type = :plural
24
+ config.jsonapi_version = '1.0'
25
+ config.jsonapi_toplevel_meta = {}
26
+ # Make JSON API top-level jsonapi member opt-in
27
+ # ref: http://jsonapi.org/format/#document-top-level
28
+ config.jsonapi_include_toplevel_object = false
29
+ config.key_transform = nil
30
+
24
31
  config.schema_path = 'test/support/schemas'
25
32
  end
26
33
  end
@@ -0,0 +1,10 @@
1
+ class ActiveModel::Serializer::ErrorSerializer < ActiveModel::Serializer
2
+ # @return [Hash<field_name,Array<error_message>>]
3
+ def as_json
4
+ object.errors.messages
5
+ end
6
+
7
+ def success?
8
+ false
9
+ end
10
+ end
@@ -0,0 +1,27 @@
1
+ require 'active_model/serializer/error_serializer'
2
+ class ActiveModel::Serializer::ErrorsSerializer
3
+ include Enumerable
4
+ delegate :each, to: :@serializers
5
+ attr_reader :object, :root
6
+
7
+ def initialize(resources, options = {})
8
+ @root = options[:root]
9
+ @object = resources
10
+ @serializers = resources.map do |resource|
11
+ serializer_class = options.fetch(:serializer) { ActiveModel::Serializer::ErrorSerializer }
12
+ serializer_class.new(resource, options.except(:serializer))
13
+ end
14
+ end
15
+
16
+ def success?
17
+ false
18
+ end
19
+
20
+ def json_key
21
+ nil
22
+ end
23
+
24
+ protected
25
+
26
+ attr_reader :serializers
27
+ end
@@ -20,9 +20,11 @@ module ActiveModel
20
20
 
21
21
  # Define a link on a serializer.
22
22
  # @example
23
- # link :self { "//example.com/posts/#{object.id}" }
23
+ # link(:self) { resource_url(object) }
24
24
  # @example
25
- # link :self, "//example.com/user"
25
+ # link(:self) { "http://example.com/resource/#{object.id}" }
26
+ # @example
27
+ # link :resource, "http://example.com/resource"
26
28
  #
27
29
  def link(name, value = nil, &block)
28
30
  _links[name] = block || value
@@ -129,6 +129,20 @@ module ActiveModel::Serializer::Lint
129
129
  assert_instance_of resource_class.model_name, ActiveModel::Name
130
130
  end
131
131
 
132
+ def test_active_model_errors
133
+ assert_respond_to resource, :errors
134
+ end
135
+
136
+ def test_active_model_errors_human_attribute_name
137
+ assert_respond_to resource.class, :human_attribute_name
138
+ assert_equal(-2, resource.class.method(:human_attribute_name).arity)
139
+ end
140
+
141
+ def test_active_model_errors_lookup_ancestors
142
+ assert_respond_to resource.class, :lookup_ancestors
143
+ assert_equal 0, resource.class.method(:lookup_ancestors).arity
144
+ end
145
+
132
146
  private
133
147
 
134
148
  def resource
@@ -0,0 +1,29 @@
1
+ module ActiveModel
2
+ class Serializer
3
+ module Meta
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ with_options instance_writer: false, instance_reader: true do |serializer|
8
+ serializer.class_attribute :_meta # @api private
9
+ end
10
+
11
+ extend ActiveSupport::Autoload
12
+ end
13
+
14
+ module ClassMethods
15
+ # Set the JSON API meta attribute of a serializer.
16
+ # @example
17
+ # class AdminAuthorSerializer < ActiveModel::Serializer
18
+ # meta { stuff: 'value' }
19
+ # @example
20
+ # meta do
21
+ # { comment_count: object.comments.count }
22
+ # end
23
+ def meta(value = nil, &block)
24
+ self._meta = block || value
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,17 @@
1
+ module ActiveModel
2
+ class Serializer
3
+ class Null < Serializer
4
+ def attributes(*)
5
+ {}
6
+ end
7
+
8
+ def associations(*)
9
+ {}
10
+ end
11
+
12
+ def serializable_hash(*)
13
+ {}
14
+ end
15
+ end
16
+ end
17
+ end
@@ -34,6 +34,57 @@ module ActiveModel
34
34
  # So you can inspect reflections in your Adapters.
35
35
  #
36
36
  class Reflection < Field
37
+ def initialize(*)
38
+ super
39
+ @_links = {}
40
+ @_include_data = true
41
+ @_meta = nil
42
+ end
43
+
44
+ def link(name, value = nil, &block)
45
+ @_links[name] = block || value
46
+ :nil
47
+ end
48
+
49
+ def meta(value = nil, &block)
50
+ @_meta = block || value
51
+ :nil
52
+ end
53
+
54
+ def include_data(value = true)
55
+ @_include_data = value
56
+ :nil
57
+ end
58
+
59
+ # @param serializer [ActiveModel::Serializer]
60
+ # @yield [ActiveModel::Serializer]
61
+ # @return [:nil, associated resource or resource collection]
62
+ # @example
63
+ # has_one :blog do |serializer|
64
+ # serializer.cached_blog
65
+ # end
66
+ #
67
+ # def cached_blog
68
+ # cache_store.fetch("cached_blog:#{object.updated_at}") do
69
+ # Blog.find(object.blog_id)
70
+ # end
71
+ # end
72
+ def value(serializer)
73
+ @object = serializer.object
74
+ @scope = serializer.scope
75
+
76
+ if block
77
+ block_value = instance_exec(serializer, &block)
78
+ if block_value == :nil
79
+ serializer.read_attribute_for_serialization(name)
80
+ else
81
+ block_value
82
+ end
83
+ else
84
+ serializer.read_attribute_for_serialization(name)
85
+ end
86
+ end
87
+
37
88
  # Build association. This method is used internally to
38
89
  # build serializer's association by its reflection.
39
90
  #
@@ -59,6 +110,7 @@ module ActiveModel
59
110
  association_value = value(subject)
60
111
  reflection_options = options.dup
61
112
  serializer_class = subject.class.serializer_for(association_value, reflection_options)
113
+ reflection_options[:include_data] = @_include_data
62
114
 
63
115
  if serializer_class
64
116
  begin
@@ -73,9 +125,13 @@ module ActiveModel
73
125
  reflection_options[:virtual_value] = association_value
74
126
  end
75
127
 
76
- Association.new(name, serializer, reflection_options)
128
+ Association.new(name, serializer, reflection_options, @_links, @_meta)
77
129
  end
78
130
 
131
+ protected
132
+
133
+ attr_accessor :object, :scope
134
+
79
135
  private
80
136
 
81
137
  def serializer_options(subject, parent_serializer_options, reflection_options)
@@ -17,7 +17,7 @@ module ActiveModel
17
17
  # class AdminAuthorSerializer < ActiveModel::Serializer
18
18
  # type 'authors'
19
19
  def type(type)
20
- self._type = type
20
+ self._type = type && type.to_s
21
21
  end
22
22
  end
23
23
  end