Merge pull request #1425 from beauby/extract-serializer-modules

[CLEANUP] Extract links and type-related methods to their own module.
This commit is contained in:
Benjamin Fleischer
2016-01-15 00:56:40 -06:00
4 changed files with 63 additions and 31 deletions

View File

@@ -209,7 +209,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)

View 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 { "//example.com/posts/#{object.id}" }
# @example
# link :self, "//example.com/user"
#
def link(name, value = nil, &block)
_links[name] = block || value
end
end
end
end
end

View 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