Skip to content

Commit f27f13c

Browse files
committed
Fix style.
1 parent 070a2e6 commit f27f13c

File tree

1 file changed

+21
-25
lines changed

1 file changed

+21
-25
lines changed

lib/active_model/serializer/adapter/json_api.rb

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ def initialize(serializer, options = {})
1414
@options[:include] = @options[:include].split(',')
1515
end
1616

17-
if fields = options.delete(:fields)
17+
fields = options.delete(:fields)
18+
if fields
1819
@fieldset = ActiveModel::Serializer::Fieldset.new(fields, serializer.json_key)
1920
else
2021
@fieldset = options[:fieldset]
@@ -48,7 +49,7 @@ def serializable_hash(options = nil)
4849

4950
def fragment_cache(cached_hash, non_cached_hash)
5051
root = false if @options.include?(:include)
51-
JsonApi::FragmentCache.new().fragment_cache(root, cached_hash, non_cached_hash)
52+
JsonApi::FragmentCache.new.fragment_cache(root, cached_hash, non_cached_hash)
5253
end
5354

5455
private
@@ -103,14 +104,12 @@ def relationship_value_for(serializer, options = {})
103104
options[:virtual_value]
104105
elsif serializer && serializer.object
105106
resource_identifier_for(serializer)
106-
else
107-
nil
108107
end
109108
end
110109
end
111110

112111
def relationships_for(serializer)
113-
Hash[serializer.associations.map { |association| [ association.key, { data: relationship_value_for(association.serializer, association.options) } ] }]
112+
Hash[serializer.associations.map { |association| [association.key, { data: relationship_value_for(association.serializer, association.options) }] }]
114113
end
115114

116115
def included_for(serializer)
@@ -121,24 +120,23 @@ def _included_for(resource_name, serializer, parent = nil)
121120
if serializer.respond_to?(:each)
122121
serializer.flat_map { |s| _included_for(resource_name, s, parent) }.uniq
123122
else
123+
return [] unless serializer && serializer.object
124124
result = []
125-
if serializer && serializer.object
126-
resource_path = [parent, resource_name].compact.join('.')
127-
128-
if include_assoc?(resource_path)
129-
primary_data = primary_data_for(serializer, @options)
130-
relationships = relationships_for(serializer)
131-
primary_data[:relationships] = relationships if relationships.any?
132-
result.push(primary_data)
133-
end
125+
resource_path = [parent, resource_name].compact.join('.')
126+
127+
if include_assoc?(resource_path)
128+
primary_data = primary_data_for(serializer, @options)
129+
relationships = relationships_for(serializer)
130+
primary_data[:relationships] = relationships if relationships.any?
131+
result.push(primary_data)
132+
end
134133

135-
if include_nested_assoc?(resource_path)
136-
serializer.associations.each do |association|
137-
if association.serializer
138-
result.concat(_included_for(association.key, association.serializer, resource_path))
139-
result.uniq!
140-
end
141-
end
134+
if include_nested_assoc?(resource_path)
135+
non_empty_associations = serializer.associations.select(&:serializer)
136+
137+
non_empty_associations.each do |association|
138+
result.concat(_included_for(association.key, association.serializer, resource_path))
139+
result.uniq!
142140
end
143141
end
144142
result
@@ -160,17 +158,15 @@ def check_assoc(assoc)
160158
def add_links(options)
161159
links = @hash.fetch(:links) { {} }
162160
collection = serializer.object
163-
if is_paginated?(collection)
164-
@hash[:links] = add_pagination_links(links, collection, options)
165-
end
161+
@hash[:links] = add_pagination_links(links, collection, options) if paginated?(collection)
166162
end
167163

168164
def add_pagination_links(links, collection, options)
169165
pagination_links = JsonApi::PaginationLinks.new(collection, options[:context]).serializable_hash(options)
170166
links.update(pagination_links)
171167
end
172168

173-
def is_paginated?(collection)
169+
def paginated?(collection)
174170
collection.respond_to?(:current_page) &&
175171
collection.respond_to?(:total_pages) &&
176172
collection.respond_to?(:size)

0 commit comments

Comments
 (0)