Skip to content

Instantly share code, notes, and snippets.

@bearded-avenger
Created August 17, 2020 23:21
Show Gist options
  • Save bearded-avenger/b2747f40c3526efbbc55720fe9a54634 to your computer and use it in GitHub Desktop.
Save bearded-avenger/b2747f40c3526efbbc55720fe9a54634 to your computer and use it in GitHub Desktop.
class GiftCard < ApplicationRecord
belongs_to :site
belongs_to :subscription_plan, optional: true
acts_as_tenant :site
include ProductConcern
include ImageUploader[:image]
enum status: { draft:0, published:1}
validates_presence_of :title
validates_uniqueness_to_tenant :slug
before_create :set_slug
validates_presence_of :slug, on: :update
accepts_nested_attributes_for :product, allow_destroy: true
validates_associated :product
has_many :gift_card_purchases, dependent: :destroy
def to_param
slug
end
private
def set_slug
newSlug = self.site.gift_cards.exists?(slug:title.parameterize) ? "#{title.parameterize}-#{SecureRandom.hex(8)}" : title.parameterize
self.slug = newSlug
end
end
class GiftCardPurchase < ApplicationRecord
belongs_to :site
belongs_to :gift_card
belongs_to :coupon
acts_as_tenant :site
validates_presence_of :recipient_name
validates_presence_of :recipient_email, format: Devise.email_regexp
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment