piggybak 0.6.10 → 0.6.11

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.
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- piggybak (0.6.10)
4
+ piggybak (0.6.11)
5
5
  activemerchant
6
6
  countries
7
7
  devise
@@ -6,7 +6,6 @@ var shipping_field;
6
6
  $(function() {
7
7
  piggybak.prevent_double_click();
8
8
  shipping_field = $('#piggybak_order_line_items_attributes_0_shipment_attributes_shipping_method_id');
9
- shipping_els = $('#piggybak_order_shipping_address_attributes_state_id,#piggybak_order_shipping_address_attributes_country_id,#piggybak_order_shipping_address_attributes_zip');
10
9
  piggybak.initialize_listeners();
11
10
  piggybak.update_shipping_options($('#piggybak_order_shipping_address_attributes_state_id'), function() {
12
11
  $('#piggybak_order_shipments_attributes_0_shipping_method_id').val(previous_shipping);
@@ -15,6 +14,7 @@ $(function() {
15
14
  });
16
15
 
17
16
  var piggybak = {
17
+ shipping_els: $('#piggybak_order_shipping_address_attributes_state_id,#piggybak_order_shipping_address_attributes_country_id,#piggybak_order_shipping_address_attributes_zip'),
18
18
  prevent_double_click: function() {
19
19
  $('#new_piggybak_order').find('input:submit').removeAttr('disabled');
20
20
  $('#new_piggybak_order').submit(function() {
@@ -22,7 +22,7 @@ var piggybak = {
22
22
  });
23
23
  },
24
24
  initialize_listeners: function() {
25
- shipping_els.live('change', function() {
25
+ piggybak.shipping_els.live('change', function() {
26
26
  piggybak.update_shipping_options($(this));
27
27
  });
28
28
  $('#piggybak_order_billing_address_attributes_state_id').live('change', function() {
@@ -123,11 +123,12 @@ var piggybak = {
123
123
  shipping_total = $('#shipping select option:selected').data('rate');
124
124
  }
125
125
  $('#shipping_total').html('$' + shipping_total.toFixed(2));
126
- var order_total = subtotal + tax_total + shipping_total;
126
+ var order_total = parseFloat((subtotal + tax_total + shipping_total).toFixed(2));
127
127
  $.each($('.extra_totals'), function(i, el) {
128
128
  order_total += parseFloat($(el).html().replace(/\$/, ''));
129
- });
130
- $('#order_total').html('$' + order_total.toFixed(2));
129
+ });
130
+ $('#order_total').html('$' + order_total.toFixed(2));
131
+ return order_total;
131
132
  },
132
133
  retrieve_shipping_data: function() {
133
134
  var shipping_data = {};
@@ -4,7 +4,7 @@ module PiggybakHelper
4
4
  end
5
5
  def cart_link
6
6
  cart = Piggybak::Cart.new(request.cookies["cart"])
7
- nitems = cart.items.inject(0) { |nitems, item| nitems + item[:quantity] }
7
+ nitems = cart.sellables.inject(0) { |nitems, item| nitems + item[:quantity] }
8
8
  if nitems > 0 && !["piggybak/orders", "piggybak/cart"].include?(params[:controller])
9
9
  link_to "#{pluralize(nitems, 'item')}: #{number_to_currency(cart.total)}", piggybak.cart_url
10
10
  end
@@ -1,22 +1,23 @@
1
1
  module Piggybak
2
2
  class Cart
3
- attr_accessor :items
3
+ attr_accessor :sellables
4
4
  attr_accessor :total
5
5
  attr_accessor :errors
6
6
  attr_accessor :extra_data
7
7
  alias :subtotal :total
8
+ alias :items :sellables
8
9
 
9
10
  def initialize(cookie='')
10
- self.items = []
11
+ self.sellables = []
11
12
  self.errors = []
12
13
  cookie ||= ''
13
14
  cookie.split(';').each do |item|
14
15
  item_sellable = Piggybak::Sellable.find_by_id(item.split(':')[0])
15
16
  if item_sellable.present?
16
- self.items << { :sellable => item_sellable, :quantity => (item.split(':')[1]).to_i }
17
+ self.sellables << { :sellable => item_sellable, :quantity => (item.split(':')[1]).to_i }
17
18
  end
18
19
  end
19
- self.total = self.items.sum { |item| item[:quantity]*item[:sellable].price }
20
+ self.total = self.sellables.sum { |item| item[:quantity]*item[:sellable].price }
20
21
 
21
22
  self.extra_data = {}
22
23
  end
@@ -58,7 +59,7 @@ module Piggybak
58
59
 
59
60
  def to_cookie
60
61
  cookie = ''
61
- self.items.each do |item|
62
+ self.sellables.each do |item|
62
63
  cookie += "#{item[:sellable].id.to_s}:#{item[:quantity].to_s};" if item[:quantity].to_i > 0
63
64
  end
64
65
  cookie
@@ -66,22 +67,22 @@ module Piggybak
66
67
 
67
68
  def update_quantities
68
69
  self.errors = []
69
- new_items = []
70
- self.items.each do |item|
70
+ new_sellables = []
71
+ self.sellables.each do |item|
71
72
  if !item[:sellable].active
72
73
  self.errors << ["Sorry, #{item[:sellable].description} is no longer for sale"]
73
74
  elsif item[:sellable].unlimited_inventory || item[:sellable].quantity >= item[:quantity]
74
- new_items << item
75
+ new_sellables << item
75
76
  elsif item[:sellable].quantity == 0
76
77
  self.errors << ["Sorry, #{item[:sellable].description} is no longer available"]
77
78
  else
78
79
  self.errors << ["Sorry, only #{item[:sellable].quantity} available for #{item[:sellable].description}"]
79
80
  item[:quantity] = item[:sellable].quantity
80
- new_items << item if item[:quantity] > 0
81
+ new_sellables << item if item[:quantity] > 0
81
82
  end
82
83
  end
83
- self.items = new_items
84
- self.total = self.items.sum { |item| item[:quantity]*item[:sellable].price }
84
+ self.sellables = new_sellables
85
+ self.total = self.sellables.sum { |item| item[:quantity]*item[:sellable].price }
85
86
  end
86
87
 
87
88
  def set_extra_data(form_params)
@@ -46,10 +46,10 @@ module Piggybak
46
46
  end
47
47
 
48
48
  def number_payments
49
- number_payments = self.line_items.select { |li| li.new_record? && li.line_item_type == "payment" }.size
50
- if number_payments > 1
49
+ new_payments = self.line_items.payments.select { |li| li.new_record? }
50
+ if new_payments.size > 1
51
51
  self.errors.add(:base, "Only one payment may be created at a time.")
52
- self.line_items.select { |li| li.new_record? && li.line_item_type == "payment" }.each do |li|
52
+ new_payments.each do |li|
53
53
  li.errors.add(:line_item_type, "Only one payment may be created at a time.")
54
54
  end
55
55
  end
@@ -75,15 +75,15 @@ module Piggybak
75
75
  # If a tax line item doesn't, create
76
76
  # If tax is 0, destroy tax line item
77
77
  tax = TaxMethod.calculate_tax(self)
78
- tax_line_item = self.line_items.detect { |line_item| line_item.line_item_type == "tax" }
78
+ tax_line_item = self.line_items.taxes
79
79
  if tax > 0
80
- if tax_line_item
81
- tax_line_item.price = tax
80
+ if tax_line_item.any?
81
+ tax_line_item.first.price = tax
82
82
  else
83
83
  self.line_items << LineItem.new({ :line_item_type => "tax", :description => "Tax Charge", :price => tax })
84
84
  end
85
- elsif tax_line_item
86
- tax_line_item.mark_for_destruction
85
+ elsif tax_line_item.any?
86
+ tax_line_item.first.mark_for_destruction
87
87
  end
88
88
 
89
89
  # Postprocess everything but payments first
@@ -107,11 +107,10 @@ module Piggybak
107
107
  self.total_due = self.total
108
108
 
109
109
  # Postprocess payment last
110
- self.line_items.each do |line_item|
111
- next if line_item.line_item_type != "payment"
112
- method = "postprocess_#{line_item.line_item_type}"
113
- if line_item.respond_to?(method)
114
- if !line_item.send(method)
110
+ self.line_items.payments.each do |line_item|
111
+ method = "postprocess_payment"
112
+ if line_item.respond_to?("postprocess_payment")
113
+ if !line_item.postprocess_payment
115
114
  return false
116
115
  end
117
116
  end
@@ -155,7 +154,7 @@ module Piggybak
155
154
  def add_line_items(cart)
156
155
  cart.update_quantities
157
156
 
158
- cart.items.each do |item|
157
+ cart.sellables.each do |item|
159
158
  self.line_items << Piggybak::LineItem.new({ :sellable_id => item[:sellable].id,
160
159
  :unit_price => item[:sellable].price,
161
160
  :price => item[:sellable].price*item[:quantity],
@@ -172,9 +171,9 @@ module Piggybak
172
171
  else
173
172
  if self.to_be_cancelled
174
173
  self.status = "cancelled"
175
- elsif line_items.select { |li| li.line_item_type == "shipment" }.any? && line_items.select { |li| li.line_item_type == "shipment" }.all? { |s| s.shipment.status == "shipped" }
174
+ elsif line_items.shipments.any? && line_items.shipments.all? { |li| li.shipment.status == "shipped" }
176
175
  self.status = "shipped"
177
- elsif line_items.select { |li| li.line_item_type == "shipment" }.any? && line_items.select { |li| li.line_item_type == "shipment" }.all? { |s| s.shipment.status == "processing" }
176
+ elsif line_items.shipments.any? && line_items.shipments.all? { |li| li.shipment.status == "processing" }
178
177
  self.status = "processing"
179
178
  else
180
179
  self.status = "new"
@@ -19,15 +19,7 @@ module Piggybak
19
19
  end
20
20
 
21
21
  def self.rate(method, object)
22
- taxable_total = 0
23
-
24
- if object.is_a?(Cart)
25
- taxable_total = object.total
26
- else
27
- taxable_total = object.subtotal
28
- end
29
-
30
- (method.metadata.detect { |m| m.key == "rate" }.value.to_f * taxable_total).to_c
22
+ (method.metadata.detect { |m| m.key == "rate" }.value.to_f * object.subtotal).to_c
31
23
  end
32
24
  end
33
25
  end
@@ -1,4 +1,4 @@
1
- <% if @cart.items.any? -%>
1
+ <% if @cart.sellables.any? -%>
2
2
  <%= form_tag piggybak.cart_update_url do -%>
3
3
  <table cellpadding="5" cellspacing="0" width="100%">
4
4
  <tr>
@@ -10,7 +10,7 @@
10
10
  <th></th>
11
11
  <% end -%>
12
12
  </tr>
13
- <% @cart.items.each do |item| %>
13
+ <% @cart.sellables.each do |item| %>
14
14
  <tr>
15
15
  <td><%= item[:sellable].description %></td>
16
16
  <td><%= number_to_currency item[:sellable].price %></td>
@@ -9,7 +9,7 @@
9
9
 
10
10
  <%= render "items", :page => "cart" %>
11
11
 
12
- <% if @cart.items.any? -%>
12
+ <% if @cart.sellables.any? -%>
13
13
  <%= link_to "Proceed to Checkout", piggybak.orders_url, :id => "checkout" %>
14
14
  <% end -%>
15
15
  </div>
@@ -46,22 +46,24 @@ module Piggybak
46
46
  attr_accessible "#{k}_attributes".to_sym
47
47
  end
48
48
  end
49
- Piggybak::LineItem.class_eval do
50
- scope plural_k, where(:line_item_type => "#{k}" )
51
- end
52
49
  Piggybak::Order.class_eval do
53
50
  define_method "#{k}_charge" do
54
- charge = 0
55
- self.line_items.each do |li|
56
- next if li._destroy || li.line_item_type.to_sym != k
57
- charge += li.price
58
- end
59
- charge
51
+ self.line_items.send(plural_k).map(&:price).reduce(:+) || 0
60
52
  end
61
53
  end
62
54
  end
63
- # Define method subtotal on order, alias to sellable_charge
64
55
  Piggybak::Order.class_eval do
56
+ has_many :line_items, :inverse_of => :order do
57
+ Piggybak.config.line_item_types.each do |k, v|
58
+ # Define proxy association method for line items
59
+ # e.g. self.line_items.sellables
60
+ # e.g. self.line_items.taxes
61
+ define_method "#{k.to_s.pluralize}" do
62
+ proxy_association.proxy.select { |li| li.line_item_type == "#{k}" }
63
+ end
64
+ end
65
+ end
66
+ # Define method subtotal on order, alias to sellable_charge
65
67
  alias :subtotal :sellable_charge
66
68
  end
67
69
  end
@@ -1,3 +1,3 @@
1
1
  module Piggybak
2
- VERSION = "0.6.10"
2
+ VERSION = "0.6.11"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: piggybak
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.10
4
+ version: 0.6.11
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2012-10-27 00:00:00.000000000 Z
14
+ date: 2012-10-30 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: rails
@@ -212,7 +212,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
212
212
  version: '0'
213
213
  segments:
214
214
  - 0
215
- hash: 3715131893246281747
215
+ hash: 151945934985011992
216
216
  required_rubygems_version: !ruby/object:Gem::Requirement
217
217
  none: false
218
218
  requirements:
@@ -221,7 +221,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
221
221
  version: '0'
222
222
  segments:
223
223
  - 0
224
- hash: 3715131893246281747
224
+ hash: 151945934985011992
225
225
  requirements: []
226
226
  rubyforge_project:
227
227
  rubygems_version: 1.8.23