jsonapi-suite / jsonapi_compliable

MIT License
20 stars 35 forks source link

Add polymorphic_belongs_to #116

Closed richmolj closed 6 years ago

richmolj commented 6 years ago

Imagine you have a table with a foreign key, where the relationship must be fetched not from one other table, but N number of other data sources.

You can do this with:

polymorphic_belongs_to :credit_card do
  # Key off of this field to make the switch
  group_by(:credit_card_type) do
    on(:Visa) # will use VisaResource
    on(:Mastercard) # will use MastercardResource
  end
end

This can be customized:

polymorphic_belongs_to :credit_card, foreign_key: :cc_id do
  group_by(:credit_card_type) do
    on(:Visa).belongs_to :visa, resource: SpecialVisaResource do
      # all the normal sideload customizations
    end
  end
end

The low-level API looks like:

resource.sideloads[:credit_card].children[:visa]
richmolj commented 6 years ago

@wadetandy