mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 15:23:06 +00:00
Clean up meta handling.
This commit is contained in:
29
lib/active_model/serializer/meta.rb
Normal file
29
lib/active_model/serializer/meta.rb
Normal file
@@ -0,0 +1,29 @@
|
||||
module ActiveModel
|
||||
class Serializer
|
||||
module Meta
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
included do
|
||||
with_options instance_writer: false, instance_reader: true do |serializer|
|
||||
serializer.class_attribute :_meta # @api private
|
||||
end
|
||||
|
||||
extend ActiveSupport::Autoload
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
# Set the JSON API meta attribute of a serializer.
|
||||
# @example
|
||||
# class AdminAuthorSerializer < ActiveModel::Serializer
|
||||
# meta { stuff: 'value' }
|
||||
# @example
|
||||
# meta do
|
||||
# { comment_count: object.comments.count }
|
||||
# end
|
||||
def meta(value = nil, &block)
|
||||
self._meta = block || value
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user