jsonapi-suite / jsonapi_compliable

MIT License
20 stars 35 forks source link

Add polymorphic resources #115

Closed richmolj closed 6 years ago

richmolj commented 6 years ago

When we say "polymorphic", think "Rails STI":

class CreditCardResource < ApplicationResource
  self.polymorphic = %w(VisaResource MastercardResource)

  # everybody gets this
  attribute :number, :integer
end

class VisaResource < CreditCardResource
  attribute :custom, :string do
    'custom rendered attribute just for visa'
  end

  # This can be sideloaded via CreditCardResource
  has_many :rewards
end

class MastercardResource < CreditCardResource
  attribute :number, :integer do
    'override'
  end
end

In other words: query through the parent, then use the children for serialization and sideloading. The above would correctly handle CreditCard.all returning an array of Visa and MasterCard instances, rendering different jsonapi types and only serializing relationships when relevant.

richmolj commented 6 years ago

@wadetandy