Refactor add to cart form into partials and helpers

This commit is contained in:
Rohan Mitchell
2013-05-27 11:25:09 +10:00
parent ff9200ebb8
commit 796be2ee6d
6 changed files with 53 additions and 26 deletions

View File

@@ -0,0 +1,19 @@
module AddToCartHelper
def product_out_of_stock
!@product.has_stock? && !Spree::Config[:allow_backorders]
end
def product_incompatible_with_current_order(order, product)
!order.nil? && !DistributorChangeValidator.new(order).can_change_distributor? && !Enterprise.distributing_product(product).include?(order.distributor)
end
def available_distributors_for(order, product)
distributors = Enterprise.distributing_product(product)
if order && order.distributor
distributors = DistributorChangeValidator.new(order).available_distributors(distributors)
end
distributors
end
end

View File

@@ -1,37 +1,26 @@
.add-to-cart
- order = current_order(false)
- if !@product.has_stock? && !Spree::Config[:allow_backorders]
- if product_out_of_stock
= content_tag('strong', t(:out_of_stock))
- elsif !order.nil? && !DistributorChangeValidator.new(order).can_change_distributor? && !@product.distributors.include?(order.distributor)
.error-distributor
Please complete your order at
= link_to current_distributor.name, root_path
before shopping with another distributor.
- elsif product_incompatible_with_current_order(order, @product)
= render 'add_to_cart_distributor_unavailable'
- else
%div(class = "columns alpha two")
%div Quantity
= number_field_tag (@product.has_variants? ? :quantity : "variants[#{@product.master.id}]"), 1, :class => 'title', :in => 1..@product.on_hand
- if @product.group_buy
%div(class = "columns alpha three")
%div Max quantity
= number_field_tag (@product.has_variants? ? :max_quantity : "variant_attributes[#{@product.master.id}][max_quantity]"), 1, :class => 'title max_quantity', :in => 1..@product.on_hand
= render 'add_to_cart_quantity_fields', product: @product
%div.cleared
%br
- if order.nil? || order.distributor.nil?
%div Distributor for your order:
= select_tag "distributor_id", options_from_collection_for_select([Enterprise.new]+@product.distributors, "id", "name", :include_blank => '')
- available_distributors = available_distributors_for(order, @product)
- if available_distributors.length > 1 || order.andand.distributor.nil?
= render 'add_to_cart_distributor_choice', distributor_collection: available_distributors
- else
- available_distributors = DistributorChangeValidator.new(order).available_distributors(@product.distributors)
- if available_distributors.length > 1
%div Distributor for your order:
= select_tag "distributor_id", options_from_collection_for_select(available_distributors, "id", "name", current_distributor.andand.id)
- elsif !@product.distributors.include? order.distributor
= hidden_field_tag "distributor_id", available_distributors.first.id
.distributor-fixed= "Your distributor for this order will be changed to #{available_distributors.first.name} if you add this product to your cart."
- else
= hidden_field_tag "distributor_id", order.distributor.id
.distributor-fixed= "Your distributor for this order is #{order.distributor.name}"
- distributor = available_distributors.first
- changing_distributor = distributor != order.andand.distributor
= render 'add_to_cart_distributor_fixed', distributor: distributor, changing_distributor: changing_distributor
%br
= button_tag :class => 'large primary', :id => 'add-to-cart-button', :type => :submit do
= t(:add_to_cart)

View File

@@ -0,0 +1,2 @@
%div Distributor for your order:
= select_tag "distributor_id", options_from_collection_for_select([Enterprise.new]+distributor_collection, "id", "name", current_distributor.andand.id)

View File

@@ -0,0 +1,6 @@
= hidden_field_tag "distributor_id", distributor.id
- if changing_distributor
.distributor-fixed= "Your distributor for this order will be changed to #{distributor.name} if you add this product to your cart."
- else
.distributor-fixed= "Your distributor for this order is #{distributor.name}"

View File

@@ -0,0 +1,4 @@
.error-distributor
Please complete your order at
= link_to current_distributor.name, root_path
before shopping with another distributor.

View File

@@ -0,0 +1,7 @@
%div(class = "columns alpha two")
%div Quantity
= number_field_tag (product.has_variants? ? :quantity : "variants[#{product.master.id}]"), 1, :class => 'title', :in => 1..product.on_hand
- if product.group_buy
%div(class = "columns alpha three")
%div Max quantity
= number_field_tag (product.has_variants? ? :max_quantity : "variant_attributes[#{product.master.id}][max_quantity]"), 1, :class => 'title max_quantity', :in => 1..product.on_hand