Class: ShopDiscountable

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/shop_discountable.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.for(type) ⇒ Object

Returns discount of a class



22
23
24
# File 'app/models/shop_discountable.rb', line 22

def self.for(type)
  all(:conditions => { :discounted_type => type.pluralize.classify })
end

Instance Method Details

#create_shop_line_items_if_shop_orderObject

Adds discount to an order’s line_items



46
47
48
49
50
51
52
53
54
55
# File 'app/models/shop_discountable.rb', line 46

def create_shop_line_items_if_shop_order
  if discounted_type === 'ShopOrder'
    discounted.line_items.each do |line_item|
      if line_item.item.discounts.include?(discount)
        # We're here if the item associated with the line item can be discounted
        discount = ShopDiscountable.create(:discount_id => discount_id, :discounted => line_item)
      end
    end
  end
end

#create_shop_products_if_shop_categoryObject

Adds discount to a category’s products



27
28
29
30
31
32
33
34
# File 'app/models/shop_discountable.rb', line 27

def create_shop_products_if_shop_category
  if discounted_type === 'ShopCategory'
    discounted.products.each do |product|
      # Attach discount to the child product
      ShopDiscountable.create(:discount => discount, :discounted => product)
    end
  end
end

#destroy_shop_line_items_if_shop_orderObject

Removes discount from an order’s line_items



58
59
60
61
62
63
64
# File 'app/models/shop_discountable.rb', line 58

def destroy_shop_line_items_if_shop_order
  if discounted_type === 'ShopOrder'
    discounted.discountables.for('ShopLineItem').each do |discountable|
      discountable.destroy
    end
  end
end

#destroy_shop_products_if_shop_categoryObject

Removes discount from a category’s products



37
38
39
40
41
42
43
# File 'app/models/shop_discountable.rb', line 37

def destroy_shop_products_if_shop_category
  if discounted_type === 'ShopCategory'
    discount.discountables.for('ShopProduct').each do |discountable|
      discountable.destroy
    end
  end
end