Sha256: e40961bf3737af229520d5e055cd21174c899df0ba3d61f40f5ceb803be00e78

Contents?: true

Size: 1.52 KB

Versions: 3

Compression:

Stored size: 1.52 KB

Contents

class ShopDiscount < ActiveRecord::Base

  # Return all discounts which are valid
  default_scope :conditions => [
    '(starts_at IS NULL and finishes_at IS NULL) OR (starts_at >= :now AND finishes_at <= :now) OR (starts_at IS NULL and finishes_at <= :now)', 
    { :now => Time.now }
  ]
  
  belongs_to  :created_by,  :class_name => 'User'
  belongs_to  :updated_by,  :class_name => 'User'
  
  has_many    :discountables, :class_name => 'ShopDiscountable', :foreign_key  => :discount_id
  has_many    :categories,  :through => :discountables, :source => :category, :conditions => "shop_discountables.discounted_type = 'ShopCategory'"
  has_many    :products,    :through => :discountables, :source => :product,  :conditions => "shop_discountables.discounted_type = 'ShopProduct'"
  has_many    :orders,      :through => :discountables, :source => :order,    :conditions => "shop_discountables.discounted_type = 'ShopOrder'"
  has_many    :line_items,  :through => :discountables, :source => :line_item,:conditions => "shop_discountables.discounted_type = 'ShopLineItem'"
  
  validates_presence_of     :name, :code, :amount
  validates_uniqueness_of   :name, :code
  validates_numericality_of :amount
  
  # This will override the default scope
  def self.all_including_invalid(*attrs)
    with_exclusive_scope{find(:all,*attrs)}
  end
  
  # Return all categories minus its own
  def available_categories
    ShopCategory.all - categories
  end
  
  # Returns all products minus its own
  def available_products
    ShopProduct.all - products
  end
  
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
radiant-shop_discounts-extension-0.0.3 app/models/shop_discount.rb
radiant-shop_discounts-extension-0.0.1 app/models/shop_discount.rb
radiant-shop-extension-0.11.6 app/models/shop_discount.rb