Skip to content

Commit

Permalink
Merge pull request #331 from larissa/set-id-block
Browse files Browse the repository at this point in the history
Allow block for id customization
  • Loading branch information
shishirmk committed Nov 3, 2018
2 parents 326f978 + 0ba5f23 commit cc7f888
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 21 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ Option | Purpose | Example
------------ | ------------- | -------------
set_type | Type name of Object | ```set_type :movie ```
key | Key of Object | ```belongs_to :owner, key: :user ```
set_id | ID of Object | ```set_id :owner_id ```
set_id | ID of Object | ```set_id :owner_id ``` or ```set_id { |record| "#{record.name.downcase}-#{record.id}" }```
cache_options | Hash to enable caching and set cache length | ```cache_options enabled: true, cache_length: 12.hours, race_condition_ttl: 10.seconds```
id_method_name | Set custom method name to get ID of an object (If block is provided for the relationship, `id_method_name` is invoked on the return value of the block instead of the resource object) | ```has_many :locations, id_method_name: :place_ids ```
object_method_name | Set custom method name to get related objects | ```has_many :locations, object_method_name: :places ```
Expand Down
4 changes: 2 additions & 2 deletions lib/fast_jsonapi/object_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ def set_type(type_name)
self.record_type = run_key_transform(type_name)
end

def set_id(id_name)
self.record_id = id_name
def set_id(id_name = nil, &block)
self.record_id = block || id_name
end

def cache_options(cache_options)
Expand Down
7 changes: 4 additions & 3 deletions lib/fast_jsonapi/serialization_core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,10 @@ def record_hash(record, fieldset, params = {})
end

def id_from_record(record)
return record.send(record_id) if record_id
raise MandatoryField, 'id is a mandatory field in the jsonapi spec' unless record.respond_to?(:id)
record.id
return record_id.call(record) if record_id.is_a?(Proc)
return record.send(record_id) if record_id
raise MandatoryField, 'id is a mandatory field in the jsonapi spec' unless record.respond_to?(:id)
record.id
end

# Override #to_json for alternative implementation
Expand Down
59 changes: 44 additions & 15 deletions spec/lib/object_serializer_class_methods_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -195,28 +195,57 @@
describe '#set_id' do
subject(:serializable_hash) { MovieSerializer.new(resource).serializable_hash }

before do
MovieSerializer.set_id :owner_id
end
context 'method name' do
before do
MovieSerializer.set_id :owner_id
end

after do
MovieSerializer.set_id nil
end
after do
MovieSerializer.set_id nil
end

context 'when one record is given' do
let(:resource) { movie }

it 'returns correct hash which id equals owner_id' do
expect(serializable_hash[:data][:id].to_i).to eq movie.owner_id
end
end

context 'when one record is given' do
let(:resource) { movie }
context 'when an array of records is given' do
let(:resource) { [movie, movie] }

it 'returns correct hash which id equals owner_id' do
expect(serializable_hash[:data][:id].to_i).to eq movie.owner_id
it 'returns correct hash which id equals owner_id' do
expect(serializable_hash[:data][0][:id].to_i).to eq movie.owner_id
expect(serializable_hash[:data][1][:id].to_i).to eq movie.owner_id
end
end
end

context 'when an array of records is given' do
let(:resource) { [movie, movie] }
context 'with block' do
before do
MovieSerializer.set_id { |record| "movie-#{record.owner_id}" }
end

after do
MovieSerializer.set_id nil
end

it 'returns correct hash which id equals owner_id' do
expect(serializable_hash[:data][0][:id].to_i).to eq movie.owner_id
expect(serializable_hash[:data][1][:id].to_i).to eq movie.owner_id
context 'when one record is given' do
let(:resource) { movie }

it 'returns correct hash which id equals movie-id' do
expect(serializable_hash[:data][:id]).to eq "movie-#{movie.owner_id}"
end
end

context 'when an array of records is given' do
let(:resource) { [movie, movie] }

it 'returns correct hash which id equals movie-id' do
expect(serializable_hash[:data][0][:id]).to eq "movie-#{movie.owner_id}"
expect(serializable_hash[:data][1][:id]).to eq "movie-#{movie.owner_id}"
end
end
end
end
Expand Down

0 comments on commit cc7f888

Please sign in to comment.