Sitelet https://github.com/jsonapi-serializer/jsonapi-serializer/pull/145/files
Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@ 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` or `set_id { \|record, params\| params[:admin] ? record.id : "#{record.name.downcase}-#{record.id}" }`
cache_options | Hash with store to enable caching and optional further cache options | `cache_options store: ActiveSupport::Cache::MemoryStore.new, expires_in: 5.minutes`
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`
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` or `has_many :locations, id_method_name: ->(object) { (return ids value) }`
object_method_name | Set custom method name to get related objects | `has_many :locations, object_method_name: :places`
record_type | Set custom Object Type for a relationship | `belongs_to :owner, record_type: :user`
serializer | Set custom Serializer for a relationship | `has_many :actors, serializer: :custom_actor`, `has_many :actors, serializer: MyApp::Api::V1::ActorSerializer`, or `has_many :actors, serializer -> (object, params) { (return a serializer class) }`
Expand Down
18 changes: 14 additions & 4 deletions lib/fast_jsonapi/relationship.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def ids_hash_from_record_and_relationship(record, params = {})

def id_hash_from_record(record, params)
associated_record_type = record_type_for(record, params)
id_hash(record.public_send(id_method_name), associated_record_type)
id_hash(record_id_value(record), associated_record_type)
end

def ids_hash(ids, record_type)
Expand All @@ -138,11 +138,21 @@ def id_hash(id, record_type, default_return = false)
def fetch_id(record, params)
if object_block.present?
object = FastJsonapi.call_proc(object_block, record, params)
return object.map { |item| item.public_send(id_method_name) } if object.respond_to? :map
return object.map { |item| record_id_value(item) } if object.respond_to? :map

return object.try(id_method_name)
return record_id_value(object) { object.try(id_method_name) }
end
record_id_value(record)
end

def record_id_value(record)
if id_method_name.is_a?(Proc)
id_method_name.call(record)
elsif block_given?
yield(record)
else
record.public_send(id_method_name)
end
record.public_send(id_method_name)
end

def add_links_hash(record, params, output_hash)
Expand Down
2 changes: 1 addition & 1 deletion spec/fixtures/movie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class MovieSerializer
)
has_many(
:actors_and_users,
id_method_name: :uid,
id_method_name: ->(actor_or_user) { actor_or_user.uid },
polymorphic: {
Actor => :actor,
User => :user
Expand Down