shop_bunny 0.8.4 → 0.8.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/app/models/coupon.rb CHANGED
@@ -1,17 +1,20 @@
1
1
  class Coupon < ActiveRecord::Base
2
+ InvalidEvent = Class.new(NoMethodError)
2
3
  has_many :coupon_uses, :dependent => :destroy
3
4
  has_many :carts, :through => :coupon_uses
5
+ belongs_to :bonus_article, :class_name => ShopBunny.item_model_class_name
4
6
 
5
7
  validates_presence_of :code
6
8
  validates_uniqueness_of :code
7
9
  validates_presence_of :title
8
10
 
11
+ after_initialize { self.state ||= 'inactive' }
9
12
  after_save :touch_cart
10
13
  after_destroy :touch_cart
11
14
 
12
15
  # TODO Add self destruction when coupon has expired?
13
16
 
14
- scope :valid, lambda {{:conditions => ['(coupons.valid_from IS NULL OR coupons.valid_from <= ?) AND (coupons.valid_until IS NULL OR coupons.valid_until >= ?) AND coupons.active = ?', Time.now, Time.now, true]}}
17
+ scope :valid, lambda {{:conditions => ['(coupons.valid_from IS NULL OR coupons.valid_from <= ?) AND (coupons.valid_until IS NULL OR coupons.valid_until >= ?) AND coupons.state = ?', Time.now, Time.now, 'active']}}
15
18
  scope :automatically_added_over, lambda {|value| {:conditions => ['value_of_automatic_add <= ?', value]}}
16
19
 
17
20
  def expired?
@@ -28,9 +31,19 @@ class Coupon < ActiveRecord::Base
28
31
  end
29
32
 
30
33
  def redeemable?
31
- !not_yet_valid? && !has_expired? && active?
34
+ !not_yet_valid? && !has_expired? && state == 'active'
32
35
  end
33
-
36
+
37
+ def activate!
38
+ raise InvalidEvent unless state == 'inactive'
39
+ self.state = 'active'
40
+ end
41
+
42
+ def redeem!
43
+ raise InvalidEvent unless redeemable?
44
+ self.state = 'redeemed'
45
+ end
46
+
34
47
  protected
35
48
  def touch_cart
36
49
  carts.each {|cart| cart.touch}
@@ -0,0 +1,11 @@
1
+ class AddStateAndRemoveActiveFromCoupons < ActiveRecord::Migration
2
+ def self.up
3
+ add_column :coupons, :state, :string
4
+ remove_column :coupons, :active
5
+ end
6
+
7
+ def self.down
8
+ remove_column :coupons, :state
9
+ add_column :coupons, :active, :boolean
10
+ end
11
+ end
@@ -15,7 +15,11 @@ module ShopBunny
15
15
  def items
16
16
  self.cart_items
17
17
  end
18
-
18
+
19
+ def bonus_items
20
+ coupons.map(&:bonus_article).compact
21
+ end
22
+
19
23
  def item_count
20
24
  self.cart_items.inject(0) {|sum,e| sum += e.quantity}
21
25
  end
@@ -110,7 +114,7 @@ module ShopBunny
110
114
 
111
115
  # Check if the cart is empty
112
116
  def empty?
113
- cart_items.empty?
117
+ cart_items.empty? && bonus_items.empty?
114
118
  end
115
119
 
116
120
  # Remove all items from the cart
metadata CHANGED
@@ -1,102 +1,79 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: shop_bunny
3
- version: !ruby/object:Gem::Version
4
- hash: 55
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.8.5
5
5
  prerelease:
6
- segments:
7
- - 0
8
- - 8
9
- - 4
10
- version: 0.8.4
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - kopfmaschine.com
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2011-11-24 00:00:00 +01:00
19
- default_executable:
12
+ date: 2012-02-01 00:00:00.000000000Z
20
13
  dependencies: []
21
-
22
14
  description: A simple shop gem that integrates a cart and coupons functionality
23
- email:
15
+ email:
24
16
  - jan@kopfmaschine.com
25
17
  executables: []
26
-
27
18
  extensions: []
28
-
29
19
  extra_rdoc_files: []
30
-
31
- files:
32
- - lib/shop_bunny.rb
33
- - lib/generators/shop_bunny/install_generator.rb
34
- - lib/generators/shop_bunny/templates/shopbunny_controller_template.rb
35
- - lib/generators/shop_bunny/templates/shopbunny_model_template.rb
36
- - lib/shop_bunny/cart_controller_module.rb
20
+ files:
37
21
  - lib/shop_bunny/cart_module.rb
38
- - lib/shop_bunny/engine.rb
39
22
  - lib/shop_bunny/shipping_cost_calculator.rb
40
- - app/controllers/application_controller.rb
23
+ - lib/shop_bunny/cart_controller_module.rb
24
+ - lib/shop_bunny/engine.rb
25
+ - lib/generators/shop_bunny/templates/shopbunny_model_template.rb
26
+ - lib/generators/shop_bunny/templates/shopbunny_controller_template.rb
27
+ - lib/generators/shop_bunny/install_generator.rb
28
+ - lib/shop_bunny.rb
41
29
  - app/controllers/carts_controller.rb
42
- - app/views/carts/show.html.erb
43
- - app/models/item.rb
30
+ - app/controllers/application_controller.rb
31
+ - app/models/coupon_use.rb
44
32
  - app/models/cart_item.rb
45
- - app/models/cart.rb
46
33
  - app/models/coupon.rb
47
- - app/models/coupon_use.rb
48
- - db/migrate/20101125140137_add_value_of_automatic_add_to_coupons.rb
34
+ - app/models/item.rb
35
+ - app/models/cart.rb
36
+ - app/views/carts/show.html.erb
37
+ - db/migrate/20100915162029_remove_owner_id_from_carts.rb
49
38
  - db/migrate/20110821124655_add_max_uses_to_coupon.rb
50
- - db/migrate/20111123234953_add_active_flag_to_coupon.rb
51
- - db/migrate/20110202235446_add_raw_item_to_cart_items.rb
39
+ - db/migrate/20101125140137_add_value_of_automatic_add_to_coupons.rb
52
40
  - db/migrate/20110203005514_remove_item_id_from_cart_items.rb
53
- - db/migrate/20100902115757_create_cart_items.rb
54
- - db/migrate/20100915162029_remove_owner_id_from_carts.rb
41
+ - db/migrate/20111130123434_add_state_and_remove_active_from_coupons.rb
42
+ - db/migrate/20100915093821_create_coupon_uses.rb
43
+ - db/migrate/20101125151017_change_valid_from_and_until_to_datetime_on_coupons.rb
55
44
  - db/migrate/20100916194407_set_default_column_value_for_cart_items_quantity.rb
56
45
  - db/migrate/20100915091059_create_coupons.rb
57
- - db/migrate/20101125151017_change_valid_from_and_until_to_datetime_on_coupons.rb
58
- - db/migrate/20100902115627_create_carts.rb
59
- - db/migrate/20100915093821_create_coupon_uses.rb
46
+ - db/migrate/20100902115757_create_cart_items.rb
47
+ - db/migrate/20111123234953_add_active_flag_to_coupon.rb
60
48
  - db/migrate/20100915073016_create_items.rb
61
- - config/initializers/machinist.rb
49
+ - db/migrate/20100902115627_create_carts.rb
50
+ - db/migrate/20110202235446_add_raw_item_to_cart_items.rb
62
51
  - config/initializers/shop_bunny.rb
63
52
  - config/initializers/add_cart_finder_to_controllers.rb
53
+ - config/initializers/machinist.rb
64
54
  - config/routes.rb
65
- has_rdoc: true
66
55
  homepage: http://kopfmaschine.com
67
56
  licenses: []
68
-
69
57
  post_install_message:
70
58
  rdoc_options: []
71
-
72
- require_paths:
59
+ require_paths:
73
60
  - lib
74
- required_ruby_version: !ruby/object:Gem::Requirement
61
+ required_ruby_version: !ruby/object:Gem::Requirement
75
62
  none: false
76
- requirements:
77
- - - ">="
78
- - !ruby/object:Gem::Version
79
- hash: 3
80
- segments:
81
- - 0
82
- version: "0"
83
- required_rubygems_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ! '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
68
  none: false
85
- requirements:
86
- - - ">="
87
- - !ruby/object:Gem::Version
88
- hash: 23
89
- segments:
90
- - 1
91
- - 3
92
- - 6
69
+ requirements:
70
+ - - ! '>='
71
+ - !ruby/object:Gem::Version
93
72
  version: 1.3.6
94
73
  requirements: []
95
-
96
74
  rubyforge_project:
97
- rubygems_version: 1.6.2
75
+ rubygems_version: 1.8.10
98
76
  signing_key:
99
77
  specification_version: 3
100
78
  summary: A shop template for your needs
101
79
  test_files: []
102
-