radiant-shop_discounts-extension 0.0.8 → 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.8
1
+ 0.0.9
@@ -0,0 +1,21 @@
1
+ class CategoryShopDiscountable < ShopDiscount
2
+
3
+ before_validation :create_shop_products
4
+ before_destroy :destroy_shop_products
5
+
6
+ # Adds discount to a category's products
7
+ def create_shop_products
8
+ discounted.products.each do |product|
9
+ # Attach discount to the child product
10
+ ShopDiscountable.create(:discount => discount, :discounted => product)
11
+ end
12
+ end
13
+
14
+ # Removes discount from a category's products
15
+ def destroy_shop_products
16
+ discounted.discountables.for('ShopProduct').each do |discountable|
17
+ discountable.destroy
18
+ end
19
+ end
20
+
21
+ end
@@ -0,0 +1,23 @@
1
+ class OrderShopDiscountable < ShopDiscountable
2
+
3
+ before_validation :create_shop_line_items
4
+ before_destroy :destroy_shop_line_items
5
+
6
+ # Adds discount to an order's line_items
7
+ def create_shop_line_items
8
+ discounted.line_items.each do |line_item|
9
+ if line_item.item.discounts.include?(discount)
10
+ # We're here if the item associated with the line item can be discounted
11
+ discount = ShopDiscountable.create(:discount_id => discount_id, :discounted => line_item)
12
+ end
13
+ end
14
+ end
15
+
16
+ # Removes discount from an order's line_items
17
+ def destroy_shop_line_items
18
+ discounted.discountables.for('ShopLineItem').each do |discountable|
19
+ discountable.destroy
20
+ end
21
+ end
22
+
23
+ end
@@ -12,55 +12,9 @@ class ShopDiscountable < ActiveRecord::Base
12
12
  validates_presence_of :discount, :discounted
13
13
  validates_uniqueness_of :discounted_id, :scope => [ :discount_id, :discounted_type ]
14
14
 
15
- before_validation :create_shop_products_if_shop_category
16
- before_destroy :destroy_shop_products_if_shop_category
17
-
18
- before_validation :create_shop_line_items_if_shop_order
19
- before_destroy :destroy_shop_line_items_if_shop_order
20
-
21
15
  # Returns discount of a class
22
16
  def self.for(type)
23
17
  all(:conditions => { :discounted_type => type.pluralize.classify })
24
18
  end
25
19
 
26
- # Adds discount to a category's products
27
- def create_shop_products_if_shop_category
28
- if discounted_type === 'ShopCategory'
29
- discounted.products.each do |product|
30
- # Attach discount to the child product
31
- ShopDiscountable.create(:discount => discount, :discounted => product)
32
- end
33
- end
34
- end
35
-
36
- # Removes discount from a category's products
37
- def destroy_shop_products_if_shop_category
38
- if discounted_type === 'ShopCategory'
39
- discount.discountables.for('ShopProduct').each do |discountable|
40
- discountable.destroy
41
- end
42
- end
43
- end
44
-
45
- # Adds discount to an order's line_items
46
- def create_shop_line_items_if_shop_order
47
- if discounted_type === 'ShopOrder'
48
- discounted.line_items.each do |line_item|
49
- if line_item.item.discounts.include?(discount)
50
- # We're here if the item associated with the line item can be discounted
51
- discount = ShopDiscountable.create(:discount_id => discount_id, :discounted => line_item)
52
- end
53
- end
54
- end
55
- end
56
-
57
- # Removes discount from an order's line_items
58
- def destroy_shop_line_items_if_shop_order
59
- if discounted_type === 'ShopOrder'
60
- discounted.discountables.for('ShopLineItem').each do |discountable|
61
- discountable.destroy
62
- end
63
- end
64
- end
65
-
66
20
  end
@@ -0,0 +1,14 @@
1
+ module ShopDiscounts
2
+ module Models
3
+ module CategoryDiscountable
4
+
5
+ def self.included(base)
6
+ base.class_eval do
7
+ has_many :discountables, :class_name => 'CategoryShopDiscountable', :foreign_key => :discounted_id, :dependent => :destroy
8
+ has_many :discounts, :class_name => 'ShopDiscount', :through => :discountables
9
+ end
10
+ end
11
+
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ module ShopDiscounts
2
+ module Models
3
+ module OrderDiscountable
4
+
5
+ def self.included(base)
6
+ base.class_eval do
7
+ has_many :discountables, :class_name => 'OrderShopDiscountable', :foreign_key => :discounted_id, :dependent => :destroy
8
+ has_many :discounts, :class_name => 'ShopDiscount', :through => :discountables
9
+ end
10
+ end
11
+
12
+ end
13
+ end
14
+ end
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{radiant-shop_discounts-extension}
8
- s.version = "0.0.8"
8
+ s.version = "0.0.9"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Dirk Kelly"]
@@ -22,7 +22,9 @@ Gem::Specification.new do |s|
22
22
  "VERSION",
23
23
  "app/controllers/admin/shop/discounts/discountables_controller.rb",
24
24
  "app/controllers/admin/shop/discounts_controller.rb",
25
+ "app/models/category_shop_discountable.rb",
25
26
  "app/models/form_discount.rb",
27
+ "app/models/order_shop_discountable.rb",
26
28
  "app/models/shop_discount.rb",
27
29
  "app/models/shop_discountable.rb",
28
30
  "app/views/admin/shop/discounts/edit.html.haml",
@@ -64,8 +66,10 @@ Gem::Specification.new do |s|
64
66
  "features/support/env.rb",
65
67
  "features/support/paths.rb",
66
68
  "lib/radiant-shop_discounts-extension.rb",
69
+ "lib/shop_discounts/models/category_discountable.rb",
67
70
  "lib/shop_discounts/models/discountable.rb",
68
71
  "lib/shop_discounts/models/form_line_item.rb",
72
+ "lib/shop_discounts/models/order_discountable.rb",
69
73
  "lib/shop_discounts/models/purchaseable.rb",
70
74
  "lib/shop_discounts/models/shop_order.rb",
71
75
  "lib/shop_discounts/models/shop_product.rb",
@@ -19,8 +19,8 @@ class ShopDiscountsExtension < Radiant::Extension
19
19
  ShopLineItem.send :include, ShopDiscounts::Models::Discountable, ShopDiscounts::Models::Purchaseable
20
20
 
21
21
  ShopProduct.send :include, ShopDiscounts::Models::Discountable, ShopDiscounts::Models::ShopProduct
22
- ShopOrder.send :include, ShopDiscounts::Models::Discountable, ShopDiscounts::Models::ShopOrder
23
- ShopCategory.send :include, ShopDiscounts::Models::Discountable
22
+ ShopOrder.send :include, ShopDiscounts::Models::OrderDiscountable, ShopDiscounts::Models::ShopOrder
23
+ ShopCategory.send :include, ShopDiscounts::Models::CategoryDiscountable
24
24
  User.send :include, ShopDiscounts::Models::Discountable
25
25
 
26
26
  Page.send :include, ShopDiscounts::Tags::Cart, ShopDiscounts::Tags::Item
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: radiant-shop_discounts-extension
3
3
  version: !ruby/object:Gem::Version
4
- hash: 15
4
+ hash: 13
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 8
10
- version: 0.0.8
9
+ - 9
10
+ version: 0.0.9
11
11
  platform: ruby
12
12
  authors:
13
13
  - Dirk Kelly
@@ -49,7 +49,9 @@ files:
49
49
  - VERSION
50
50
  - app/controllers/admin/shop/discounts/discountables_controller.rb
51
51
  - app/controllers/admin/shop/discounts_controller.rb
52
+ - app/models/category_shop_discountable.rb
52
53
  - app/models/form_discount.rb
54
+ - app/models/order_shop_discountable.rb
53
55
  - app/models/shop_discount.rb
54
56
  - app/models/shop_discountable.rb
55
57
  - app/views/admin/shop/discounts/edit.html.haml
@@ -91,8 +93,10 @@ files:
91
93
  - features/support/env.rb
92
94
  - features/support/paths.rb
93
95
  - lib/radiant-shop_discounts-extension.rb
96
+ - lib/shop_discounts/models/category_discountable.rb
94
97
  - lib/shop_discounts/models/discountable.rb
95
98
  - lib/shop_discounts/models/form_line_item.rb
99
+ - lib/shop_discounts/models/order_discountable.rb
96
100
  - lib/shop_discounts/models/purchaseable.rb
97
101
  - lib/shop_discounts/models/shop_order.rb
98
102
  - lib/shop_discounts/models/shop_product.rb