Skip to content

Commit daf4030

Browse files
authored
Merge pull request #274 from manojmj92/master
Evaluate ids via the specified 'id_method_name' when relationships are evaluated via a block
2 parents 5a5a5e5 + 6d03db3 commit daf4030

4 files changed

Lines changed: 43 additions & 7 deletions

File tree

lib/fast_jsonapi/object_serializer.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,11 @@ def create_relationship(base_key, relationship_type, options, block)
232232
Relationship.new(
233233
key: options[:key] || run_key_transform(base_key),
234234
name: name,
235-
id_method_name: options[:id_method_name] || "#{base_serialization_key}#{id_postfix}".to_sym,
235+
id_method_name: compute_id_method_name(
236+
options[:id_method_name],
237+
"#{base_serialization_key}#{id_postfix}".to_sym,
238+
block
239+
),
236240
record_type: options[:record_type] || run_key_transform(base_key_sym),
237241
object_method_name: options[:object_method_name] || name,
238242
object_block: block,
@@ -244,6 +248,14 @@ def create_relationship(base_key, relationship_type, options, block)
244248
)
245249
end
246250

251+
def compute_id_method_name(custom_id_method_name, id_method_name_from_relationship, block)
252+
if block.present?
253+
custom_id_method_name || :id
254+
else
255+
custom_id_method_name || id_method_name_from_relationship
256+
end
257+
end
258+
247259
def compute_serializer_name(serializer_key)
248260
return serializer_key unless serializer_key.is_a? Symbol
249261
namespace = self.name.gsub(/()?\w+Serializer$/, '')

lib/fast_jsonapi/relationship.rb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,11 @@ def id_hash(id, record_type, default_return=false)
8686
end
8787

8888
def fetch_id(record, params)
89-
unless object_block.nil?
89+
if object_block.present?
9090
object = object_block.call(record, params)
91-
92-
return object.map(&:id) if object.respond_to? :map
93-
return object.try(:id)
91+
return object.map { |item| item.public_send(id_method_name) } if object.respond_to? :map
92+
return object.try(id_method_name)
9493
end
95-
9694
record.public_send(id_method_name)
9795
end
9896
end

spec/lib/object_serializer_class_methods_spec.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,31 @@
8787
end
8888
end
8989

90+
describe '#has_many with block and id_method_name' do
91+
before do
92+
MovieSerializer.has_many(:awards, id_method_name: :imdb_award_id) do |movie|
93+
movie.actors.map(&:awards).flatten
94+
end
95+
end
96+
97+
after do
98+
MovieSerializer.relationships_to_serialize.delete(:awards)
99+
end
100+
101+
context 'awards is not included' do
102+
subject(:hash) { MovieSerializer.new(movie).serializable_hash }
103+
104+
it 'returns correct hash where id is obtained from the method specified via `id_method_name`' do
105+
expected_award_data = movie.actors.map(&:awards).flatten.map do |actor|
106+
{ id: actor.imdb_award_id.to_s, type: actor.class.name.downcase.to_sym }
107+
end
108+
serialized_award_data = hash[:data][:relationships][:awards][:data]
109+
110+
expect(serialized_award_data).to eq(expected_award_data)
111+
end
112+
end
113+
end
114+
90115
describe '#belongs_to' do
91116
subject(:relationship) { MovieSerializer.relationships_to_serialize[:area] }
92117

spec/shared/contexts/movie_context.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ def awards
8080
a.id = i
8181
a.title = "Test Award #{i}"
8282
a.actor_id = id
83+
a.imdb_award_id = i * 10
8384
a.year = 1990 + i
8485
end
8586
end
@@ -111,7 +112,7 @@ def state
111112
end
112113

113114
class Award
114-
attr_accessor :id, :title, :actor_id, :year
115+
attr_accessor :id, :title, :actor_id, :year, :imdb_award_id
115116
end
116117

117118
class State

0 commit comments

Comments
 (0)