diff --git a/app/assets/javascripts/preorder.js.coffee b/app/assets/javascripts/preorder.js.coffee
index 1d63aa3..0d93b78 100644
--- a/app/assets/javascripts/preorder.js.coffee
+++ b/app/assets/javascripts/preorder.js.coffee
@@ -41,6 +41,7 @@ Selfstarter =
# the radio button selection should bring up the email field and button
$('.payment_options ol li').on "click", ->
+ return false if $(this).children(".payment_radio").attr("disabled") == "disabled"
$(".payment_radio").parents("ol>li").removeClass("checkout_option_selected")
$(this).addClass("checkout_option_selected")
$(this).children(".payment_radio").attr "checked", "checked"
diff --git a/app/assets/stylesheets/checkout.css.scss b/app/assets/stylesheets/checkout.css.scss
index bc05563..bfec159 100644
--- a/app/assets/stylesheets/checkout.css.scss
+++ b/app/assets/stylesheets/checkout.css.scss
@@ -150,6 +150,12 @@
line-height: 14px;
}
+ .sold-out
+ {
+ color: #ffffff;
+ background-color: black;
+ padding: 3px;
+ }
}
div.description
{
diff --git a/app/controllers/preorder_controller.rb b/app/controllers/preorder_controller.rb
index a4be320..4100467 100644
--- a/app/controllers/preorder_controller.rb
+++ b/app/controllers/preorder_controller.rb
@@ -11,13 +11,15 @@ class PreorderController < ApplicationController
@user = User.find_or_create_by_email!(params[:email])
if Settings.use_payment_options
- price = params['payment_option']
- raise Exception.new("No payment option was selected") if price.nil?
+ payment_option_id = params['payment_option']
+ raise Exception.new("No payment option was selected") if payment_option_id.nil?
+ payment_option = PaymentOption.find(payment_option_id)
+ price = payment_option.amount
else
price = Settings.price
end
- @order = Order.prefill!(:name => Settings.product_name, :price => price, :user_id => @user.id)
+ @order = Order.prefill!(:name => Settings.product_name, :price => price, :user_id => @user.id, :payment_option => payment_option)
# This is where all the magic happens. We create a multi-use token with Amazon, letting us charge the user's Amazon account
# Then, if they confirm the payment, Amazon POSTs us their shipping details and phone number
diff --git a/app/helpers/preorder_helper.rb b/app/helpers/preorder_helper.rb
index 6e9d53a..6a65d96 100644
--- a/app/helpers/preorder_helper.rb
+++ b/app/helpers/preorder_helper.rb
@@ -25,4 +25,12 @@ module PreorderHelper
def encoded_root_url
raw URI.encode "#{request.scheme}://#{request.host}/preorder"
end
+
+ def sold_out(payment_option)
+ payment_option.limit > -1 and order_count(payment_option) >= payment_option.limit
+ end
+
+ def order_count(payment_option)
+ Order.where(payment_option_id: payment_option).count(:token) # count of orders that have a token from amazon and are for this payment option
+ end
end
diff --git a/app/models/order.rb b/app/models/order.rb
index c9e007b..24385e9 100644
--- a/app/models/order.rb
+++ b/app/models/order.rb
@@ -1,17 +1,20 @@
class Order < ActiveRecord::Base
- attr_accessible :address_one, :address_two, :city, :country, :number, :state, :status, :token, :transaction_id, :zip, :shipping, :tracking_number, :name, :price, :phone, :expiration
+ attr_accessible :address_one, :address_two, :city, :country, :number, :state, :status, :token, :transaction_id, :zip,
+ :shipping, :tracking_number, :name, :price, :phone, :expiration, :payment_option
attr_readonly :uuid
before_validation :generate_uuid!, :on => :create
belongs_to :user
+ belongs_to :payment_option
self.primary_key = 'uuid'
# This is where we create our Caller Reference for Amazon Payments, and prefill some other information.
def self.prefill!(options = {})
- @order = Order.new
- @order.name = options[:name]
- @order.user_id = options[:user_id]
- @order.price = options[:price]
- @order.number = Order.next_order_number
+ @order = Order.new
+ @order.name = options[:name]
+ @order.user_id = options[:user_id]
+ @order.price = options[:price]
+ @order.number = Order.next_order_number
+ @order.payment_option = options[:payment_option] if !options[:payment_option].nil?
@order.save!
@order
diff --git a/app/models/payment_option.rb b/app/models/payment_option.rb
index 0d14133..7faf458 100644
--- a/app/models/payment_option.rb
+++ b/app/models/payment_option.rb
@@ -1,3 +1,3 @@
class PaymentOption < ActiveRecord::Base
- attr_accessible :amount, :amount_display, :delivery_desc, :description, :limit, :purchased_count, :shipping_desc
+ attr_accessible :amount, :amount_display, :delivery_desc, :description, :limit, :shipping_desc
end
diff --git a/app/views/preorder/_payment_options.html.erb b/app/views/preorder/_payment_options.html.erb
index d5b3892..ddd4391 100644
--- a/app/views/preorder/_payment_options.html.erb
+++ b/app/views/preorder/_payment_options.html.erb
@@ -4,13 +4,19 @@
<% PaymentOption.all.each_with_index do |p, index| %>
-
- <%= radio_button_tag "payment_option", p.amount, false, class: 'payment_radio' %>
+ <%= radio_button_tag "payment_option", p.id, false, class: 'payment_radio', disabled: sold_out(p) %>
<%= label_tag("payment_option_#{index}", p.amount_display) %>
+ <% if sold_out p %>
+ Sold Out
+ <% end %>
<%= p.shipping_desc %>
<%= raw p.description %>
+ <% if p.limit > -1 %>
+
Limited (<%= p.limit - order_count(p) %> of <%= p.limit %> remaining)
+ <% end %>
<%= p.delivery_desc %>
diff --git a/db/migrate/20130106183354_create_payment_options.rb b/db/migrate/20130106183354_create_payment_options.rb
index 2c72b1b..9bc02bd 100644
--- a/db/migrate/20130106183354_create_payment_options.rb
+++ b/db/migrate/20130106183354_create_payment_options.rb
@@ -7,7 +7,6 @@ class CreatePaymentOptions < ActiveRecord::Migration
t.string :shipping_desc
t.string :delivery_desc
t.integer :limit
- t.integer :purchased_count
t.timestamps
end
diff --git a/db/migrate/20130107010733_add_payment_option_id_to_orders.rb b/db/migrate/20130107010733_add_payment_option_id_to_orders.rb
new file mode 100644
index 0000000..9d758c5
--- /dev/null
+++ b/db/migrate/20130107010733_add_payment_option_id_to_orders.rb
@@ -0,0 +1,5 @@
+class AddPaymentOptionIdToOrders < ActiveRecord::Migration
+ def change
+ add_column :orders, :payment_option_id, :integer
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 079ad3a..122de31 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20130106183354) do
+ActiveRecord::Schema.define(:version => 20130107010733) do
create_table "orders", :id => false, :force => true do |t|
t.string "token"
@@ -32,8 +32,9 @@ ActiveRecord::Schema.define(:version => 20130106183354) do
t.string "phone"
t.string "name"
t.date "expiration"
- t.datetime "created_at", :null => false
- t.datetime "updated_at", :null => false
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ t.integer "payment_option_id"
end
create_table "payment_options", :force => true do |t|
@@ -43,7 +44,6 @@ ActiveRecord::Schema.define(:version => 20130106183354) do
t.string "shipping_desc"
t.string "delivery_desc"
t.integer "limit"
- t.integer "purchased_count"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
diff --git a/db/seeds.rb b/db/seeds.rb
index 46299ef..61e5f1d 100644
--- a/db/seeds.rb
+++ b/db/seeds.rb
@@ -13,8 +13,7 @@ PaymentOption.create(
description: 'Basic level: You receive a great big thankyou from us! You Rock',
shipping_desc: '',
delivery_desc: '',
- limit: -1,
- purchased_count: 0
+ limit: -1
},
{
amount: 100.00,
@@ -22,8 +21,7 @@ PaymentOption.create(
description: 'Package 1: You receive our print edition',
shipping_desc: 'add $3 to ship outside the US',
delivery_desc: 'Estimated delivery: Oct 2013',
- limit: 250,
- purchased_count: 0
+ limit: 250
},
{
amount: 125.00,
@@ -31,8 +29,7 @@ PaymentOption.create(
description: 'Package 2: You will receive both our print and digital edition',
shipping_desc: 'add $3 to ship outside the US',
delivery_desc: 'Estimated delivery: Oct 2013',
- limit: -1,
- purchased_count: 0
+ limit: -1
},
{
amount: 200.00,
@@ -40,8 +37,7 @@ PaymentOption.create(
description: 'Package 3: Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
shipping_desc: 'add $3 to ship outside the US',
delivery_desc: 'Estimated delivery: Oct 2013',
- limit: -1,
- purchased_count: 0
+ limit: -1
},
{
amount: 250.00,
@@ -49,8 +45,7 @@ PaymentOption.create(
description: 'Package 4: Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
shipping_desc: 'add $3 to ship outside the US',
delivery_desc: 'Estimated delivery: Oct 2013',
- limit: -1,
- purchased_count: 0
+ limit: -1
},
{
amount: 300.00,
@@ -58,8 +53,7 @@ PaymentOption.create(
description: 'Package 5: Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
shipping_desc: 'add $3 to ship outside the US',
delivery_desc: 'Estimated delivery: Oct 2013',
- limit: -1,
- purchased_count: 0
+ limit: -1
},
{
amount: 500.00,
@@ -67,8 +61,7 @@ PaymentOption.create(
description: 'Package 6: Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
shipping_desc: 'add $3 to ship outside the US',
delivery_desc: 'Estimated delivery: Oct 2013',
- limit: -1,
- purchased_count: 0
+ limit: -1
},
{
amount: 1000.00,
@@ -76,7 +69,6 @@ PaymentOption.create(
description: 'Package 7: Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
shipping_desc: 'add $3 to ship outside the US',
delivery_desc: 'Estimated delivery: Oct 2013',
- limit: -1,
- purchased_count: 0
+ limit: -1
}
])
\ No newline at end of file