mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 15:23:06 +00:00
Extract links and type-related methods to their own module.
This commit is contained in:
@@ -208,7 +208,7 @@ module ActiveModel
|
||||
end
|
||||
|
||||
def links_for(serializer)
|
||||
serializer.links.each_with_object({}) do |(name, value), hash|
|
||||
serializer._links.each_with_object({}) do |(name, value), hash|
|
||||
hash[name] =
|
||||
if value.respond_to?(:call)
|
||||
link = Link.new(serializer)
|
||||
|
||||
33
lib/active_model/serializer/links.rb
Normal file
33
lib/active_model/serializer/links.rb
Normal file
@@ -0,0 +1,33 @@
|
||||
module ActiveModel
|
||||
class Serializer
|
||||
module Links
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
included do
|
||||
with_options instance_writer: false, instance_reader: true do |serializer|
|
||||
serializer.class_attribute :_links # @api private
|
||||
self._links ||= {}
|
||||
end
|
||||
|
||||
extend ActiveSupport::Autoload
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
def inherited(base)
|
||||
super
|
||||
base._links = _links.dup
|
||||
end
|
||||
|
||||
# Define a link on a serializer.
|
||||
# @example
|
||||
# link :self { "/posts/#{object.id}" }
|
||||
# @example
|
||||
# link :self, "/user"
|
||||
#
|
||||
def link(name, value = nil, &block)
|
||||
_links[name] = block || value
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
25
lib/active_model/serializer/type.rb
Normal file
25
lib/active_model/serializer/type.rb
Normal file
@@ -0,0 +1,25 @@
|
||||
module ActiveModel
|
||||
class Serializer
|
||||
module Type
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
included do
|
||||
with_options instance_writer: false, instance_reader: true do |serializer|
|
||||
serializer.class_attribute :_type # @api private
|
||||
end
|
||||
|
||||
extend ActiveSupport::Autoload
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
# Set the JSON API type of a serializer.
|
||||
# @example
|
||||
# class AdminAuthorSerializer < ActiveModel::Serializer
|
||||
# type 'authors'
|
||||
def type(type)
|
||||
self._type = type
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user