Skip to content

Commit e39de8c

Browse files
Shuhei Kitagawashishirmk
authored andcommitted
Enable to use block to define relationship
1 parent 4523508 commit e39de8c

2 files changed

Lines changed: 25 additions & 10 deletions

File tree

lib/fast_jsonapi/object_serializer.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -181,21 +181,21 @@ def add_relationship(name, relationship)
181181
self.relationships_to_serialize[name] = relationship
182182
end
183183

184-
def has_many(relationship_name, options = {})
184+
def has_many(relationship_name, options = {}, &block)
185185
name = relationship_name.to_sym
186-
hash = create_relationship_hash(relationship_name, :has_many, options)
186+
hash = create_relationship_hash(relationship_name, :has_many, options, block)
187187
add_relationship(name, hash)
188188
end
189189

190-
def has_one(relationship_name, options = {})
190+
def has_one(relationship_name, options = {}, &block)
191191
name = relationship_name.to_sym
192-
hash = create_relationship_hash(relationship_name, :has_one, options)
192+
hash = create_relationship_hash(relationship_name, :has_one, options, block)
193193
add_relationship(name, hash)
194194
end
195195

196196
alias belongs_to has_one
197197

198-
def create_relationship_hash(base_key, relationship_type, options)
198+
def create_relationship_hash(base_key, relationship_type, options, block)
199199
name = base_key.to_sym
200200
if relationship_type == :has_many
201201
base_serialization_key = base_key.to_s.singularize
@@ -212,6 +212,7 @@ def create_relationship_hash(base_key, relationship_type, options)
212212
id_method_name: options[:id_method_name] || "#{base_serialization_key}#{id_postfix}".to_sym,
213213
record_type: options[:record_type] || run_key_transform(base_key_sym),
214214
object_method_name: options[:object_method_name] || name,
215+
object_block: block,
215216
serializer: compute_serializer_name(options[:serializer] || base_key_sym),
216217
relationship_type: relationship_type,
217218
cached: options[:cached] || false,

lib/fast_jsonapi/serialization_core.rb

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ def ids_hash_from_record_and_relationship(record, relationship)
4848
polymorphic = relationship[:polymorphic]
4949

5050
return ids_hash(
51-
record.public_send(relationship[:id_method_name]),
51+
fetch_id(record, relationship),
5252
relationship[:record_type]
5353
) unless polymorphic
5454

55-
return unless associated_object = record.send(relationship[:object_method_name])
55+
return unless associated_object = fetch_associated_object(record, relationship)
5656

5757
return associated_object.map do |object|
5858
id_hash_from_record object, polymorphic
@@ -117,9 +117,7 @@ def to_json(payload)
117117

118118
def get_included_records(record, includes_list, known_included_objects)
119119
includes_list.each_with_object([]) do |item, included_records|
120-
included_objects = record.send(
121-
@relationships_to_serialize[item][:object_method_name]
122-
)
120+
included_objects = fetch_associated_object(record, @relationships_to_serialize[item])
123121
next if included_objects.blank?
124122

125123
record_type = @relationships_to_serialize[item][:record_type]
@@ -134,6 +132,22 @@ def get_included_records(record, includes_list, known_included_objects)
134132
end
135133
end
136134
end
135+
136+
def fetch_associated_object(record, relationship)
137+
return relationship[:object_block].call(record) unless relationship[:object_block].nil?
138+
record.send(relationship[:object_method_name])
139+
end
140+
141+
def fetch_id(record, relationship)
142+
unless relationship[:object_block].nil?
143+
object = relationship[:object_block].call(record)
144+
145+
return object.map(&:id) if object.respond_to? :map
146+
return object.id
147+
end
148+
149+
record.public_send(relationship[:id_method_name])
150+
end
137151
end
138152
end
139153
end

0 commit comments

Comments
 (0)