Last active
May 6, 2022 09:49
-
-
Save seanharmer/f0fc25e12bbc05287d4d9c3838a9b894 to your computer and use it in GitHub Desktop.
Example usage of new delegated_type facility
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MembershipProduct < ApplicationRecord | |
include Sellable | |
# Other stuff... | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Price < ApplicationRecord | |
belongs_to :product | |
has_many :subscriptions | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SquadMembershipProduct < ApplicationRecord | |
include Sellable | |
belongs_to :squad | |
# Other stuff... | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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