Sha256: 229d39c33619bd56ec06d7e5b0d5bc82e1c6649bdff25a34540881a994f8d254

Contents?: true

Size: 1.71 KB

Versions: 6

Compression:

Stored size: 1.71 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'"
  has_many    :users,       :through => :discountables, :source => :user,     :conditions => "shop_discountables.discounted_type = 'User'"
  
  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
  
  def available_users
    User.all - users
  end
  
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
radiant-shop_discounts-extension-0.0.10 app/models/shop_discount.rb
radiant-shop_discounts-extension-0.0.9 app/models/shop_discount.rb
radiant-shop_discounts-extension-0.0.8 app/models/shop_discount.rb
radiant-shop_discounts-extension-0.0.7 app/models/shop_discount.rb
radiant-shop_discounts-extension-0.0.6 app/models/shop_discount.rb
radiant-shop_discounts-extension-0.0.4 app/models/shop_discount.rb