Skip to content

Instantly share code, notes, and snippets.

@seanharmer
Last active May 6, 2022 09:49
Show Gist options
  • Save seanharmer/f0fc25e12bbc05287d4d9c3838a9b894 to your computer and use it in GitHub Desktop.
Save seanharmer/f0fc25e12bbc05287d4d9c3838a9b894 to your computer and use it in GitHub Desktop.
Example usage of new delegated_type facility
class MembershipProduct < ApplicationRecord
include Sellable
# Other stuff...
end
class Price < ApplicationRecord
belongs_to :product
has_many :subscriptions
end
# Schema: id, club_id, product_id, stripe_product_id, name, description, sellable_type, sellable_id
class Product < ApplicationRecord
delegated_type :sellable, types: %w[membership_product squad_membership_product store_product]
belongs_to :club
has_many :prices
has_many :subscriptions, through: :prices
# Other stuff...
end
module Sellable
extend ActiveSupport::Concern
included do
has_one :product, as: :sellable, touch: true, dependent: :destroy
accepts_nested_attributes_for :product
has_many :prices, through: :product
end
end
class SquadMembershipProduct < ApplicationRecord
include Sellable
belongs_to :squad
# Other stuff...
end
class StoreProduct < ApplicationRecord
include Sellable
# Other stuff...
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment