Created
May 16, 2011 15:23
-
-
Save pepe/974636 to your computer and use it in GitHub Desktop.
Suitcasetype cart controller
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Helper methods defined here can be accessed in any controller or view in the application | |
SuitcasetypeCom.helpers do | |
#TODO cart must be modeled!!! | |
def initialize_cart | |
session[:cart] ||= {:styles => [], :fonts => [], :price => 0} | |
end | |
def add_to_cart(product) | |
initialize_cart | |
unless cart_already_has? product | |
case product.class | |
when Style | |
session[:cart][:styles] << product.id.to_s | |
when Font | |
session[:cart][:fonts] << product.id.to_s | |
end | |
add_to_price(product.price_with_license(current_currency, license_for(product))) | |
end | |
end | |
def remove_from_cart(product) | |
if cart_already_has? product | |
case product.class | |
when Style | |
session[:cart][:styles].delete(product.id.to_s) | |
when Font | |
session[:cart][:fonts].delete(product.id.to_s) | |
end | |
remove_from_price(product.price_with_license(current_currency, license_for(product))) | |
end | |
end | |
def add_to_price(price) | |
session[:cart][:price] += price | |
end | |
def remove_from_price(price) | |
session[:cart][:price] -= price | |
end | |
def current_currency | |
session[:current_currency] ||= :euro | |
end | |
def set_currency_to_symbol(symbol) | |
set_currency currencies.detect {|k,v| v==symbol}.first | |
end | |
def currencies | |
@currencies ||= {euro: 'EUR', dollar: 'USD', crown: 'CZK'} | |
end | |
def set_currency(currency) | |
session[:current_currency] = currency | |
end | |
def current_currency_symbol | |
currencies[current_currency] | |
end | |
def cart_link | |
if (session[:cart] && (session[:cart][:price] > 0)) | |
items = pluralize(session[:cart][:styles].size + session[:cart][:fonts].size, 'item') | |
sentence = "Cart: (#{items}) #{session[:cart][:price]} #{current_currency_symbol}" | |
link_to(sentence, '/cart', class:"cart") | |
else | |
"Shopping cart is empty" | |
end | |
end | |
def cart_totals | |
session[:cart][:price] | |
end | |
def thrash_cart | |
session[:cart] = nil | |
initialize_cart | |
end | |
def back_to_referer | |
referer = session[:referer] | |
if referer && !referer.empty? | |
referer | |
else | |
'/' | |
end | |
end | |
def save_referer | |
if request.env["HTTP_REFERER"] && !request.env["HTTP_REFERER"].match(/cart/) | |
session[:referer] = request.env["HTTP_REFERER"] | |
end | |
end | |
def get_things_from_cart | |
initialize_cart | |
[Style.find_by_ids(session[:cart][:styles]), | |
Font.find(session[:cart][:fonts])] | |
end | |
def save_order | |
@order.save | |
session[:order] = @order.id | |
end | |
def styles_in_cart | |
session[:cart][:styles] | |
end | |
def fonts_in_cart | |
session[:cart][:fonts] | |
end | |
def initialize_license_count | |
session[:license_count] ||= {style: Hash.new(5), font: Hash.new(5)} | |
end | |
def license_for(product) | |
initialize_license_count | |
session[:license_count][product.class.to_s.downcase.to_sym][product.id] | |
end | |
def set_license_for(product, count) | |
initialize_license_count | |
recompute_cart | |
session[:license_count][product.class.to_s.downcase.to_sym][product.id] = count | |
end | |
def license_sentence(product) | |
"#{content_tag(:span, license_for(product), :id => 'license_count')} CPUs at 1 location" | |
end | |
def license_count_select(count) | |
options = {'1-5' => 5, '6-10' => 10, '11-20' => 20, | |
'21-50' => 50, '51-100' => 100, | |
'101-200' => 200, '201-500' => 500, | |
'501-1000' => 500} | |
selected = options.select {|k, v| v >= count.to_i}.first | |
select_tag :count, options: options, selected: selected, class: 'license_count' | |
end | |
def cart_form(style, &block) | |
if cart_already_has? style | |
url = url(:cart, :remove_style) | |
dom_class = 'tools cart_remover' | |
else | |
url = url(:cart, :add_style) | |
dom_class = 'tools cart_adder' | |
end | |
form_tag url, class: dom_class, &block | |
end | |
def buy_font_form(font, &block) | |
if cart_already_has? font | |
url = url(:cart, :remove_font) | |
else | |
url = url(:cart, :add_font) | |
end | |
return form_tag(url, class: 'buy', &block) | |
end | |
def cart_already_has?(product) | |
initialize_cart | |
case product.class | |
when Style | |
return session[:cart][:styles].include? product.id.to_s | |
when Font | |
return session[:cart][:fonts].include? product.id.to_s | |
end | |
end | |
def cart_submit(style) | |
if cart_already_has? style | |
submit_tag :remove_from_cart, value:"Remove from cart", class:'cart' | |
else | |
submit_tag :add_to_cart, value:"Add to cart", class:'cart' | |
end | |
end | |
def buy_button(font) | |
if cart_already_has? font | |
value = 'Remove' | |
dom_class = 'in_cart' | |
else | |
if font.price == 0 | |
value = 'Download' | |
dom_class = 'download' | |
elsif font.styles.size > 1 | |
value = 'Buy family' | |
dom_class = '' | |
else | |
value = 'Buy font' | |
dom_class = 'only_one' | |
end | |
end | |
submit_tag(:buy_button, value: value, class: dom_class) | |
end | |
def recompute_cart | |
initialize_cart | |
session[:cart][:price] = 0 | |
Style.find_by_ids(session[:cart][:styles]).each do |style| | |
add_to_price(style.price_with_license(current_currency, license_for(style))) | |
end | |
session[:cart][:fonts].each do |f| | |
font = Font.find(f) | |
add_to_price(font.price_with_license(current_currency, license_for(font))) | |
end | |
end | |
def make_current(customer) | |
session[:customer] = customer.id | |
end | |
def current_customer | |
if session[:customer] | |
@current_customer ||= Customer.find(session[:customer]) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment