active_model_serializers 0.10.0.rc3 → 0.10.0.rc4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (161) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/.rubocop.yml +37 -33
  4. data/.rubocop_todo.yml +13 -88
  5. data/.simplecov +23 -11
  6. data/.travis.yml +17 -12
  7. data/CHANGELOG.md +417 -12
  8. data/CONTRIBUTING.md +206 -17
  9. data/Gemfile +12 -11
  10. data/README.md +78 -286
  11. data/Rakefile +44 -8
  12. data/active_model_serializers.gemspec +9 -1
  13. data/appveyor.yml +6 -4
  14. data/docs/ARCHITECTURE.md +120 -0
  15. data/docs/DESIGN.textile +8 -0
  16. data/docs/README.md +21 -15
  17. data/docs/general/adapters.md +79 -27
  18. data/docs/general/caching.md +52 -0
  19. data/docs/general/configuration_options.md +18 -2
  20. data/docs/general/getting_started.md +44 -19
  21. data/docs/general/instrumentation.md +40 -0
  22. data/docs/general/logging.md +14 -0
  23. data/docs/general/rendering.md +153 -0
  24. data/docs/general/serializers.md +207 -0
  25. data/docs/how-open-source-maintained.jpg +0 -0
  26. data/docs/howto/add_pagination_links.md +16 -7
  27. data/docs/howto/add_root_key.md +3 -3
  28. data/docs/howto/outside_controller_use.md +25 -9
  29. data/docs/howto/test.md +152 -0
  30. data/docs/integrations/ember-and-json-api.md +112 -0
  31. data/docs/integrations/grape.md +19 -0
  32. data/docs/jsonapi/schema.md +140 -0
  33. data/docs/jsonapi/schema/schema.json +366 -0
  34. data/lib/action_controller/serialization.rb +13 -9
  35. data/lib/active_model/serializable_resource.rb +9 -7
  36. data/lib/active_model/serializer.rb +93 -129
  37. data/lib/active_model/serializer/adapter.rb +37 -105
  38. data/lib/active_model/serializer/adapter/attributes.rb +66 -0
  39. data/lib/active_model/serializer/adapter/base.rb +58 -0
  40. data/lib/active_model/serializer/adapter/cached_serializer.rb +45 -0
  41. data/lib/active_model/serializer/adapter/fragment_cache.rb +43 -7
  42. data/lib/active_model/serializer/adapter/json.rb +11 -37
  43. data/lib/active_model/serializer/adapter/json/fragment_cache.rb +9 -1
  44. data/lib/active_model/serializer/adapter/json_api.rb +127 -62
  45. data/lib/active_model/serializer/adapter/json_api/deserialization.rb +207 -0
  46. data/lib/active_model/serializer/adapter/json_api/fragment_cache.rb +9 -1
  47. data/lib/active_model/serializer/adapter/json_api/link.rb +44 -0
  48. data/lib/active_model/serializer/adapter/json_api/pagination_links.rb +12 -4
  49. data/lib/active_model/serializer/adapter/null.rb +7 -1
  50. data/lib/active_model/serializer/array_serializer.rb +6 -37
  51. data/lib/active_model/serializer/associations.rb +21 -18
  52. data/lib/active_model/serializer/attribute.rb +25 -0
  53. data/lib/active_model/serializer/attributes.rb +82 -0
  54. data/lib/active_model/serializer/caching.rb +100 -0
  55. data/lib/active_model/serializer/collection_serializer.rb +47 -0
  56. data/lib/active_model/serializer/configuration.rb +17 -3
  57. data/lib/active_model/serializer/field.rb +56 -0
  58. data/lib/active_model/serializer/fieldset.rb +9 -18
  59. data/lib/active_model/serializer/include_tree.rb +111 -0
  60. data/lib/active_model/serializer/links.rb +33 -0
  61. data/lib/active_model/serializer/lint.rb +16 -3
  62. data/lib/active_model/serializer/reflection.rb +25 -8
  63. data/lib/active_model/serializer/type.rb +25 -0
  64. data/lib/active_model/serializer/version.rb +1 -1
  65. data/lib/active_model_serializers.rb +16 -26
  66. data/lib/active_model_serializers/callbacks.rb +55 -0
  67. data/lib/active_model_serializers/deserialization.rb +13 -0
  68. data/lib/active_model_serializers/logging.rb +119 -0
  69. data/lib/active_model_serializers/model.rb +39 -0
  70. data/lib/active_model_serializers/railtie.rb +38 -0
  71. data/lib/active_model_serializers/serialization_context.rb +10 -0
  72. data/lib/active_model_serializers/test.rb +7 -0
  73. data/lib/active_model_serializers/test/schema.rb +103 -0
  74. data/lib/active_model_serializers/test/serializer.rb +125 -0
  75. data/lib/generators/{serializer → rails}/USAGE +1 -1
  76. data/lib/generators/{serializer → rails}/resource_override.rb +1 -3
  77. data/lib/generators/{serializer → rails}/serializer_generator.rb +2 -3
  78. data/lib/generators/{serializer → rails}/templates/serializer.rb.erb +0 -0
  79. data/lib/grape/active_model_serializers.rb +14 -0
  80. data/lib/grape/formatters/active_model_serializers.rb +15 -0
  81. data/lib/grape/helpers/active_model_serializers.rb +16 -0
  82. data/test/action_controller/adapter_selector_test.rb +1 -1
  83. data/test/action_controller/json/include_test.rb +167 -0
  84. data/test/action_controller/json_api/deserialization_test.rb +59 -0
  85. data/test/action_controller/json_api/linked_test.rb +20 -3
  86. data/test/action_controller/json_api/pagination_test.rb +7 -7
  87. data/test/action_controller/serialization_scope_name_test.rb +8 -12
  88. data/test/action_controller/serialization_test.rb +44 -29
  89. data/test/active_model_serializers/logging_test.rb +77 -0
  90. data/test/active_model_serializers/model_test.rb +9 -0
  91. data/test/active_model_serializers/railtie_test_isolated.rb +57 -0
  92. data/test/active_model_serializers/serialization_context_test.rb +18 -0
  93. data/test/active_model_serializers/test/schema_test.rb +128 -0
  94. data/test/active_model_serializers/test/serializer_test.rb +63 -0
  95. data/test/active_record_test.rb +1 -1
  96. data/test/adapter/fragment_cache_test.rb +3 -2
  97. data/test/adapter/json/belongs_to_test.rb +2 -2
  98. data/test/adapter/json/collection_test.rb +15 -5
  99. data/test/adapter/json/has_many_test.rb +3 -3
  100. data/test/adapter/json_api/belongs_to_test.rb +3 -3
  101. data/test/adapter/json_api/collection_test.rb +8 -6
  102. data/test/adapter/json_api/fields_test.rb +89 -0
  103. data/test/adapter/json_api/has_many_embed_ids_test.rb +2 -2
  104. data/test/adapter/json_api/has_many_explicit_serializer_test.rb +2 -2
  105. data/test/adapter/json_api/has_many_test.rb +3 -3
  106. data/test/adapter/json_api/has_one_test.rb +2 -2
  107. data/test/adapter/json_api/json_api_test.rb +2 -2
  108. data/test/adapter/json_api/linked_test.rb +116 -5
  109. data/test/adapter/json_api/links_test.rb +68 -0
  110. data/test/adapter/json_api/pagination_links_test.rb +4 -4
  111. data/test/adapter/json_api/parse_test.rb +139 -0
  112. data/test/adapter/json_api/resource_type_config_test.rb +27 -15
  113. data/test/adapter/json_api/toplevel_jsonapi_test.rb +84 -0
  114. data/test/adapter/json_test.rb +2 -2
  115. data/test/adapter/null_test.rb +2 -2
  116. data/test/adapter_test.rb +3 -3
  117. data/test/array_serializer_test.rb +30 -93
  118. data/test/collection_serializer_test.rb +100 -0
  119. data/test/fixtures/poro.rb +19 -51
  120. data/test/generators/scaffold_controller_generator_test.rb +1 -0
  121. data/test/generators/serializer_generator_test.rb +2 -1
  122. data/test/grape_test.rb +82 -0
  123. data/test/include_tree/from_include_args_test.rb +26 -0
  124. data/test/include_tree/from_string_test.rb +94 -0
  125. data/test/include_tree/include_args_to_hash_test.rb +64 -0
  126. data/test/lint_test.rb +4 -1
  127. data/test/logger_test.rb +2 -2
  128. data/test/poro_test.rb +1 -1
  129. data/test/serializable_resource_test.rb +1 -1
  130. data/test/serializers/adapter_for_test.rb +24 -28
  131. data/test/serializers/association_macros_test.rb +1 -1
  132. data/test/serializers/associations_test.rb +143 -26
  133. data/test/serializers/attribute_test.rb +64 -3
  134. data/test/serializers/attributes_test.rb +1 -6
  135. data/test/serializers/cache_test.rb +46 -2
  136. data/test/serializers/configuration_test.rb +20 -3
  137. data/test/serializers/fieldset_test.rb +5 -16
  138. data/test/serializers/meta_test.rb +38 -29
  139. data/test/serializers/options_test.rb +1 -1
  140. data/test/serializers/root_test.rb +1 -1
  141. data/test/serializers/serializer_for_test.rb +78 -9
  142. data/test/support/custom_schemas/active_model_serializers/test/schema_test/my/index.json +6 -0
  143. data/test/support/isolated_unit.rb +77 -0
  144. data/test/support/rails5_shims.rb +29 -0
  145. data/test/support/rails_app.rb +7 -3
  146. data/test/support/schemas/active_model_serializers/test/schema_test/my/index.json +6 -0
  147. data/test/support/schemas/active_model_serializers/test/schema_test/my/show.json +6 -0
  148. data/test/support/schemas/custom/show.json +7 -0
  149. data/test/support/schemas/hyper_schema.json +93 -0
  150. data/test/support/schemas/render_using_json_api.json +43 -0
  151. data/test/support/schemas/simple_json_pointers.json +10 -0
  152. data/test/support/serialization_testing.rb +46 -6
  153. data/test/support/test_case.rb +14 -0
  154. data/test/test_helper.rb +21 -14
  155. metadata +160 -16
  156. data/lib/active_model/serializer/adapter/flatten_json.rb +0 -12
  157. data/lib/active_model/serializer/railtie.rb +0 -15
  158. data/lib/active_model/serializer/utils.rb +0 -35
  159. data/lib/tasks/rubocop.rake +0 -0
  160. data/test/capture_warnings.rb +0 -65
  161. data/test/utils/include_args_to_hash_test.rb +0 -79
@@ -0,0 +1,33 @@
1
+ module ActiveModel
2
+ class Serializer
3
+ module Links
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ with_options instance_writer: false, instance_reader: true do |serializer|
8
+ serializer.class_attribute :_links # @api private
9
+ self._links ||= {}
10
+ end
11
+
12
+ extend ActiveSupport::Autoload
13
+ end
14
+
15
+ module ClassMethods
16
+ def inherited(base)
17
+ super
18
+ base._links = _links.dup
19
+ end
20
+
21
+ # Define a link on a serializer.
22
+ # @example
23
+ # link :self { "//example.com/posts/#{object.id}" }
24
+ # @example
25
+ # link :self, "//example.com/user"
26
+ #
27
+ def link(name, value = nil, &block)
28
+ _links[name] = block || value
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -80,8 +80,8 @@ module ActiveModel::Serializer::Lint
80
80
  # arguments (Rails 4.0) or a splat (Rails 4.1+).
81
81
  # Fails otherwise.
82
82
  #
83
- # <tt>cache_key</tt> returns a (self-expiring) unique key for the object,
84
- # which is used by the adapter.
83
+ # <tt>cache_key</tt> returns a (self-expiring) unique key for the object, and
84
+ # is part of the (self-expiring) cache_key, which is used by the adapter.
85
85
  # It is not required unless caching is enabled.
86
86
  def test_cache_key
87
87
  assert_respond_to resource, :cache_key
@@ -92,6 +92,19 @@ module ActiveModel::Serializer::Lint
92
92
  assert_includes [-1, 0], actual_arity, "expected #{actual_arity.inspect} to be 0 or -1"
93
93
  end
94
94
 
95
+ # Passes if the object responds to <tt>updated_at</tt> and if it takes no
96
+ # arguments.
97
+ # Fails otherwise.
98
+ #
99
+ # <tt>updated_at</tt> returns a Time object or iso8601 string and
100
+ # is part of the (self-expiring) cache_key, which is used by the adapter.
101
+ # It is not required unless caching is enabled.
102
+ def test_updated_at
103
+ assert_respond_to resource, :updated_at
104
+ actual_arity = resource.method(:updated_at).arity
105
+ assert_equal 0, actual_arity
106
+ end
107
+
95
108
  # Passes if the object responds to <tt>id</tt> and if it takes no
96
109
  # arguments.
97
110
  # Fails otherwise.
@@ -100,7 +113,7 @@ module ActiveModel::Serializer::Lint
100
113
  # It is not required unless caching is enabled.
101
114
  def test_id
102
115
  assert_respond_to resource, :id
103
- assert_equal resource.method(:id).arity, 0
116
+ assert_equal 0, resource.method(:id).arity
104
117
  end
105
118
 
106
119
  # Passes if the object's class responds to <tt>model_name</tt> and if it
@@ -1,23 +1,39 @@
1
+ require 'active_model/serializer/field'
2
+
1
3
  module ActiveModel
2
4
  class Serializer
3
5
  # Holds all the meta-data about an association as it was specified in the
4
6
  # ActiveModel::Serializer class.
5
7
  #
6
8
  # @example
7
- # class PostSerializer < ActiveModel::Serializer
9
+ # class PostSerializer < ActiveModel::Serializer
8
10
  # has_one :author, serializer: AuthorSerializer
9
11
  # has_many :comments
10
- # end
12
+ # has_many :comments, key: :last_comments do
13
+ # object.comments.last(1)
14
+ # end
15
+ # has_many :secret_meta_data, if: :is_admin?
16
+ #
17
+ # def is_admin?
18
+ # current_user.admin?
19
+ # end
20
+ # end
21
+ #
22
+ # Specifically, the association 'comments' is evaluated two different ways:
23
+ # 1) as 'comments' and named 'comments'.
24
+ # 2) as 'object.comments.last(1)' and named 'last_comments'.
11
25
  #
12
26
  # PostSerializer._reflections #=>
13
27
  # # [
14
28
  # # HasOneReflection.new(:author, serializer: AuthorSerializer),
15
29
  # # HasManyReflection.new(:comments)
30
+ # # HasManyReflection.new(:comments, { key: :last_comments }, #<Block>)
31
+ # # HasManyReflection.new(:secret_meta_data, { if: :is_admin? })
16
32
  # # ]
17
33
  #
18
34
  # So you can inspect reflections in your Adapters.
19
35
  #
20
- Reflection = Struct.new(:name, :options) do
36
+ class Reflection < Field
21
37
  # Build association. This method is used internally to
22
38
  # build serializer's association by its reflection.
23
39
  #
@@ -40,17 +56,17 @@ module ActiveModel
40
56
  # @api private
41
57
  #
42
58
  def build_association(subject, parent_serializer_options)
43
- association_value = subject.send(name)
59
+ association_value = value(subject)
44
60
  reflection_options = options.dup
45
- serializer_class = ActiveModel::Serializer.serializer_for(association_value, reflection_options)
61
+ serializer_class = subject.class.serializer_for(association_value, reflection_options)
46
62
 
47
63
  if serializer_class
48
64
  begin
49
65
  serializer = serializer_class.new(
50
66
  association_value,
51
- serializer_options(parent_serializer_options, reflection_options)
67
+ serializer_options(subject, parent_serializer_options, reflection_options)
52
68
  )
53
- rescue ActiveModel::Serializer::ArraySerializer::NoSerializerError
69
+ rescue ActiveModel::Serializer::CollectionSerializer::NoSerializerError
54
70
  reflection_options[:virtual_value] = association_value.try(:as_json) || association_value
55
71
  end
56
72
  elsif !association_value.nil? && !association_value.instance_of?(Object)
@@ -62,11 +78,12 @@ module ActiveModel
62
78
 
63
79
  private
64
80
 
65
- def serializer_options(parent_serializer_options, reflection_options)
81
+ def serializer_options(subject, parent_serializer_options, reflection_options)
66
82
  serializer = reflection_options.fetch(:serializer, nil)
67
83
 
68
84
  serializer_options = parent_serializer_options.except(:serializer)
69
85
  serializer_options[:serializer] = serializer if serializer
86
+ serializer_options[:serializer_context_class] = subject.class
70
87
  serializer_options
71
88
  end
72
89
  end
@@ -0,0 +1,25 @@
1
+ module ActiveModel
2
+ class Serializer
3
+ module Type
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ with_options instance_writer: false, instance_reader: true do |serializer|
8
+ serializer.class_attribute :_type # @api private
9
+ end
10
+
11
+ extend ActiveSupport::Autoload
12
+ end
13
+
14
+ module ClassMethods
15
+ # Set the JSON API type of a serializer.
16
+ # @example
17
+ # class AdminAuthorSerializer < ActiveModel::Serializer
18
+ # type 'authors'
19
+ def type(type)
20
+ self._type = type
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -1,5 +1,5 @@
1
1
  module ActiveModel
2
2
  class Serializer
3
- VERSION = '0.10.0.rc3'
3
+ VERSION = '0.10.0.rc4'
4
4
  end
5
5
  end
@@ -1,33 +1,23 @@
1
- require 'logger'
2
1
  require 'active_model'
3
- require 'active_support/railtie'
4
- require 'action_controller'
5
- require 'action_controller/railtie'
2
+ require 'active_support'
3
+ require 'active_support/core_ext/object/with_options'
6
4
  module ActiveModelSerializers
7
- mattr_accessor :logger
8
- self.logger = Rails.logger || Logger.new(IO::NULL)
5
+ extend ActiveSupport::Autoload
6
+ autoload :Model
7
+ autoload :Callbacks
8
+ autoload :Deserialization
9
+ autoload :Logging
10
+ autoload :Test
9
11
 
10
- module_function
12
+ class << self; attr_accessor :logger; end
13
+ self.logger = ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new(STDOUT))
11
14
 
12
- def silence_warnings
13
- verbose = $VERBOSE
14
- $VERBOSE = nil
15
- yield
16
- ensure
17
- $VERBOSE = verbose
15
+ def self.config
16
+ ActiveModel::Serializer.config
18
17
  end
19
- end
20
-
21
- require 'active_model/serializer'
22
- require 'active_model/serializable_resource'
23
- require 'active_model/serializer/version'
24
18
 
25
- require 'action_controller/serialization'
26
- ActiveSupport.on_load(:action_controller) do
27
- include ::ActionController::Serialization
28
- ActionDispatch::Reloader.to_prepare do
29
- ActiveModel::Serializer.serializers_cache.clear
30
- end
19
+ require 'active_model/serializer/version'
20
+ require 'active_model/serializer'
21
+ require 'active_model/serializable_resource'
22
+ require 'active_model_serializers/railtie' if defined?(::Rails)
31
23
  end
32
-
33
- require 'active_model/serializer/railtie'
@@ -0,0 +1,55 @@
1
+ # Adapted from
2
+ # https://github.com/rails/rails/blob/7f18ea14c8/activejob/lib/active_job/callbacks.rb
3
+ require 'active_support/callbacks'
4
+
5
+ module ActiveModelSerializers
6
+ # = ActiveModelSerializers Callbacks
7
+ #
8
+ # ActiveModelSerializers provides hooks during the life cycle of serialization and
9
+ # allow you to trigger logic. Available callbacks are:
10
+ #
11
+ # * <tt>around_render</tt>
12
+ #
13
+ module Callbacks
14
+ extend ActiveSupport::Concern
15
+ include ActiveSupport::Callbacks
16
+
17
+ included do
18
+ define_callbacks :render
19
+ end
20
+
21
+ # These methods will be included into any ActiveModelSerializers object, adding
22
+ # callbacks for +render+.
23
+ module ClassMethods
24
+ # Defines a callback that will get called around the render method,
25
+ # whether it is as_json, to_json, or serializable_hash
26
+ #
27
+ # class ActiveModel::SerializableResource
28
+ # include ActiveModelSerializers::Callbacks
29
+ #
30
+ # around_render do |args, block|
31
+ # tag_logger do
32
+ # notify_render do
33
+ # block.call(args)
34
+ # end
35
+ # end
36
+ # end
37
+ #
38
+ # def as_json
39
+ # run_callbacks :render do
40
+ # adapter.as_json
41
+ # end
42
+ # end
43
+ # # Note: So that we can re-use the instrumenter for as_json, to_json, and
44
+ # # serializable_hash, we aren't using the usual format, which would be:
45
+ # # def render(args)
46
+ # # adapter.as_json
47
+ # # end
48
+ # end
49
+ #
50
+ def around_render(*filters, &blk)
51
+ set_callback(:render, :around, *filters, &blk)
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,13 @@
1
+ module ActiveModelSerializers
2
+ module Deserialization
3
+ module_function
4
+
5
+ def jsonapi_parse(*args)
6
+ ActiveModel::Serializer::Adapter::JsonApi::Deserialization.parse(*args)
7
+ end
8
+
9
+ def jsonapi_parse!(*args)
10
+ ActiveModel::Serializer::Adapter::JsonApi::Deserialization.parse!(*args)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,119 @@
1
+ ##
2
+ # ActiveModelSerializers::Logging
3
+ #
4
+ # https://github.com/rails/rails/blob/280654ef88/activejob/lib/active_job/logging.rb
5
+ #
6
+ module ActiveModelSerializers
7
+ module Logging
8
+ RENDER_EVENT = 'render.active_model_serializers'.freeze
9
+ extend ActiveSupport::Concern
10
+
11
+ included do
12
+ include ActiveModelSerializers::Callbacks
13
+ extend Macros
14
+ instrument_rendering
15
+ end
16
+
17
+ module ClassMethods
18
+ def instrument_rendering
19
+ around_render do |args, block|
20
+ tag_logger do
21
+ notify_render do
22
+ block.call(args)
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+
29
+ # Macros that can be used to customize the logging of class or instance methods,
30
+ # by extending the class or its singleton.
31
+ #
32
+ # Adapted from:
33
+ # https://github.com/rubygems/rubygems/blob/cb28f5e991/lib/rubygems/deprecate.rb
34
+ #
35
+ # Provides a single method +notify+ to be used to declare when
36
+ # something a method notifies, with the argument +callback_name+ of the notification callback.
37
+ #
38
+ # class Adapter
39
+ # def self.klass_method
40
+ # # ...
41
+ # end
42
+ #
43
+ # def instance_method
44
+ # # ...
45
+ # end
46
+ #
47
+ # include ActiveModelSerializers::Logging::Macros
48
+ # notify :instance_method, :render
49
+ #
50
+ # class << self
51
+ # extend ActiveModelSerializers::Logging::Macros
52
+ # notify :klass_method, :render
53
+ # end
54
+ # end
55
+ module Macros
56
+ ##
57
+ # Simple notify method that wraps up +name+
58
+ # in a dummy method. It notifies on with the +callback_name+ notifier on
59
+ # each call to the dummy method, telling what the current serializer and adapter
60
+ # are being rendered.
61
+ # Adapted from:
62
+ # https://github.com/rubygems/rubygems/blob/cb28f5e991/lib/rubygems/deprecate.rb
63
+ def notify(name, callback_name)
64
+ class_eval do
65
+ old = "_notifying_#{callback_name}_#{name}"
66
+ alias_method old, name
67
+ define_method name do |*args, &block|
68
+ run_callbacks callback_name do
69
+ send old, *args, &block
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
75
+
76
+ def notify_render(*)
77
+ event_name = RENDER_EVENT
78
+ ActiveSupport::Notifications.instrument(event_name, notify_render_payload) do
79
+ yield
80
+ end
81
+ end
82
+
83
+ def notify_render_payload
84
+ { serializer: serializer, adapter: adapter }
85
+ end
86
+
87
+ private
88
+
89
+ def tag_logger(*tags)
90
+ if ActiveModelSerializers.logger.respond_to?(:tagged)
91
+ tags.unshift 'active_model_serializers'.freeze unless logger_tagged_by_active_model_serializers?
92
+ ActiveModelSerializers.logger.tagged(*tags) { yield }
93
+ else
94
+ yield
95
+ end
96
+ end
97
+
98
+ def logger_tagged_by_active_model_serializers?
99
+ ActiveModelSerializers.logger.formatter.current_tags.include?('active_model_serializers'.freeze)
100
+ end
101
+
102
+ class LogSubscriber < ActiveSupport::LogSubscriber
103
+ def render(event)
104
+ info do
105
+ serializer = event.payload[:serializer]
106
+ adapter = event.payload[:adapter]
107
+ duration = event.duration.round(2)
108
+ "Rendered #{serializer.name} with #{adapter.class} (#{duration}ms)"
109
+ end
110
+ end
111
+
112
+ def logger
113
+ ActiveModelSerializers.logger
114
+ end
115
+ end
116
+ end
117
+ end
118
+
119
+ ActiveModelSerializers::Logging::LogSubscriber.attach_to :active_model_serializers
@@ -0,0 +1,39 @@
1
+ # ActiveModelSerializers::Model is a convenient
2
+ # serializable class to inherit from when making
3
+ # serializable non-activerecord objects.
4
+ module ActiveModelSerializers
5
+ class Model
6
+ include ActiveModel::Model
7
+ include ActiveModel::Serializers::JSON
8
+
9
+ attr_reader :attributes
10
+
11
+ def initialize(attributes = {})
12
+ @attributes = attributes
13
+ super
14
+ end
15
+
16
+ # Defaults to the downcased model name.
17
+ def id
18
+ attributes.fetch(:id) { self.class.name.downcase }
19
+ end
20
+
21
+ # Defaults to the downcased model name and updated_at
22
+ def cache_key
23
+ attributes.fetch(:cache_key) { "#{self.class.name.downcase}/#{id}-#{updated_at.strftime("%Y%m%d%H%M%S%9N")}" }
24
+ end
25
+
26
+ # Defaults to the time the serializer file was modified.
27
+ def updated_at
28
+ attributes.fetch(:updated_at) { File.mtime(__FILE__) }
29
+ end
30
+
31
+ def read_attribute_for_serialization(key)
32
+ if key == :id || key == 'id'
33
+ attributes.fetch(key) { id }
34
+ else
35
+ attributes[key]
36
+ end
37
+ end
38
+ end
39
+ end