diff --git a/app/controllers/shop_controller.rb b/app/controllers/shop_controller.rb index 0f07c2125..d59a6428a 100644 --- a/app/controllers/shop_controller.rb +++ b/app/controllers/shop_controller.rb @@ -2,6 +2,22 @@ class ShopController < ApplicationController def index @products = Product.all @order_item = OrderItem.new + + # this is (hopefully) part of a short-term hack to prevent people from + # ordering multiple subscriptions, which would be very confusing to deal + # with. We check whether they have an order already in progress, and if + # so, point that out to them and encourage them to checkout, rather than + # letting them add more stuff to their order. + + @order = nil + @most_recent_item = nil + if current_member + @order = current_member.current_order + if @order + @most_recent_item = @order.order_items.first + end + end + respond_to do |format| format.html # index.html.haml end diff --git a/app/models/product.rb b/app/models/product.rb index 5ad15a2e8..88ee157b0 100644 --- a/app/models/product.rb +++ b/app/models/product.rb @@ -2,4 +2,7 @@ class Product < ActiveRecord::Base attr_accessible :description, :min_price, :name has_and_belongs_to_many :orders + def to_s + name + end end diff --git a/app/views/shop/index.html.haml b/app/views/shop/index.html.haml index 6c45b33f3..52a280571 100644 --- a/app/views/shop/index.html.haml +++ b/app/views/shop/index.html.haml @@ -13,36 +13,50 @@ you want to pay. Remember, your subscription supports an open source platform promoting sustainable lifestyles! -- @products.each do |p| - %h2= p.name +- if @order and @order.order_items.count > 0 + %h2 Your current order - %div - :markdown - #{ strip_tags p.description } + %p + You currently have the following item in your cart: + %strong + = @most_recent_item.product + @ + = price_with_currency(@most_recent_item.price) %p - Pay what you want, starting at - =succeed "." do - =price_with_currency(p.min_price) + = link_to "View order and checkout", order_path(@order) - %div - - if can? :create, Order +- else + - @products.each do |p| + %h2= p.name - = form_for @order_item do |f| - .field - = f.text_field :price, :value => price_in_dollars(p.min_price) - .field - = f.hidden_field :product_id, :value => p.id - .field - = f.hidden_field :quantity, :value => 1 - .actions - = f.submit 'Buy', :class => 'btn btn-primary' + %div + :markdown + #{ strip_tags p.description } - - else - Please - =link_to "sign in", new_member_session_path - or - =link_to "sign up", new_member_registration_path - to purchase. + %p + Pay what you want, starting at + =succeed "." do + =price_with_currency(p.min_price) + + %div + - if can? :create, Order + + = form_for @order_item do |f| + .field + = f.text_field :price, :value => price_in_dollars(p.min_price) + .field + = f.hidden_field :product_id, :value => p.id + .field + = f.hidden_field :quantity, :value => 1 + .actions + = f.submit 'Buy', :class => 'btn btn-primary' + + - else + Please + =link_to "sign in", new_member_session_path + or + =link_to "sign up", new_member_registration_path + to purchase. diff --git a/spec/controllers/shop_controller_spec.rb b/spec/controllers/shop_controller_spec.rb index fad178cc6..2aaaa58ff 100644 --- a/spec/controllers/shop_controller_spec.rb +++ b/spec/controllers/shop_controller_spec.rb @@ -12,6 +12,25 @@ describe ShopController do get :index, {} assigns(:products).should eq([@product1, @product2]) end + + it "assigns a new @order_item to build forms" do + get :index, {} + assigns(:order_item).should be_an_instance_of OrderItem + end + + it "assigns @order as nil if the user doesn't have one" do + get :index, {} + assigns(:order).should be_nil + end + + it "assigns @order as current_order if there is one" do + @member = FactoryGirl.create(:member) + sign_in @member + @order = FactoryGirl.create(:order, :member => @member) + get :index, {} + assigns(:order).should eq @order + end + end end diff --git a/spec/models/product_spec.rb b/spec/models/product_spec.rb index 88c9c9938..d8fc496a8 100644 --- a/spec/models/product_spec.rb +++ b/spec/models/product_spec.rb @@ -1,4 +1,9 @@ require 'spec_helper' describe Product do + + it "stringifies using the name" do + @product = FactoryGirl.create(:product) + @product.to_s.should eq @product.name + end end