Skip to content

Commit 87ca408

Browse files
committed
Merge pull request #1425 from beauby/extract-serializer-modules
[CLEANUP] Extract links and type-related methods to their own module.
2 parents 92e8a0a + 2e87c8e commit 87ca408

File tree

4 files changed

+63
-31
lines changed

4 files changed

+63
-31
lines changed

lib/active_model/serializer.rb

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
require 'active_model/serializer/configuration'
99
require 'active_model/serializer/fieldset'
1010
require 'active_model/serializer/lint'
11+
require 'active_model/serializer/links'
12+
require 'active_model/serializer/type'
1113

1214
# ActiveModel::Serializer is an abstract class that is
1315
# reified when subclassed to decorate a resource.
@@ -17,32 +19,10 @@ class Serializer
1719
include Associations
1820
include Attributes
1921
include Caching
22+
include Links
23+
include Type
2024
require 'active_model/serializer/adapter'
2125

22-
with_options instance_writer: false, instance_reader: false do |serializer|
23-
serializer.class_attribute :_type, instance_reader: true
24-
serializer.class_attribute :_links # @api private : links definitions, @see Serializer#link
25-
self._links ||= {}
26-
end
27-
28-
# Serializers inherit _attribute_mappings, _reflections, and _links.
29-
# Generates a unique digest for each serializer at load.
30-
def self.inherited(base)
31-
base._links = _links.dup
32-
super
33-
end
34-
35-
# @example
36-
# class AdminAuthorSerializer < ActiveModel::Serializer
37-
# type 'authors'
38-
def self.type(type)
39-
self._type = type
40-
end
41-
42-
def self.link(name, value = nil, &block)
43-
_links[name] = block || value
44-
end
45-
4626
# @param resource [ActiveRecord::Base, ActiveModelSerializers::Model]
4727
# @return [ActiveModel::Serializer]
4828
# Preferentially returns
@@ -148,12 +128,6 @@ def read_attribute_for_serialization(attr)
148128
end
149129
end
150130

151-
# @api private
152-
# Used by JsonApi adapter to build resource links.
153-
def links
154-
self.class._links
155-
end
156-
157131
protected
158132

159133
attr_accessor :instance_options

lib/active_model/serializer/adapter/json_api.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ def add_included_resources_for(serializer, include_tree, primary_data, included)
209209
end
210210

211211
def links_for(serializer)
212-
serializer.links.each_with_object({}) do |(name, value), hash|
212+
serializer._links.each_with_object({}) do |(name, value), hash|
213213
hash[name] =
214214
if value.respond_to?(:call)
215215
link = Link.new(serializer)

lib/active_model/serializer/links.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

lib/active_model/serializer/type.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

0 commit comments

Comments
 (0)