app/helpers/bodega/cart_helper.rb in bodega-0.3.0 vs app/helpers/bodega/cart_helper.rb in bodega-0.4.0
- old
+ new
@@ -1,39 +1,23 @@
module Bodega
module CartHelper
- def button_to_cart(product, label = 'Add to Cart', options = {})
+ def button_to_cart(product, label = 'Add to Cart', options = {}, &block)
unless options.key? :disabled
options[:disabled] = !product.in_stock?
end
- form_tag(bodega.add_path) do
- hidden_field_tag('product[type]', product.class) +
- hidden_field_tag('product[id]', product.id) +
- button_tag(label, options)
+ form_contents = hidden_field_tag('order_product[product_type]', product.class)
+ form_contents << hidden_field_tag('order_product[product_id]', product.id) +
+ if block_given?
+ form_contents << capture(&block)
end
+ form_contents << button_tag(label, options)
+ form_tag(bodega.add_to_order_path) { form_contents }
end
protected
def current_order
- @current_order ||= Bodega::Order.new.tap do |order|
- begin
- if Bodega.config.customer_method
- order.customer = send(Bodega.config.customer_method)
- end
- rescue NoMethodError
- raise "Please configure Bodega.config.customer_method to point to a valid method for accessing a customer record (default: current_user)"
- end
- order.order_products = current_products.map do |type, product|
- product = product.symbolize_keys
- OrderProduct.new do |order_product|
- order_product.product_type = product[:type]
- order_product.product_id = product[:id]
- order_product.quantity = product[:quantity]
- end
- end
+ @current_order ||= Bodega::Order.where(identifier: session[:bodega_order_id]).first || Bodega::Order.new.tap do |order|
+ order.customer = send(Bodega.config.customer_method) if Bodega.config.customer_method
end
- end
-
- def current_products
- session[:bodega_products] ||= {}
end
end
end