Commit 3f3550d3fd6947eba4cd4e14bd165331b15c10bd

Authored by Rodrigo Souto
1 parent b99c27c5

[shopping-cart plugin] Delivery and some fixes

plugins/shopping_cart/controllers/shopping_cart_plugin_myprofile_controller.rb
... ... @@ -4,8 +4,7 @@ class ShoppingCartPluginMyprofileController < MyProfileController
4 4 def edit
5 5 if request.post?
6 6 begin
7   - profile.shopping_cart = params[:shopping_cart] == '1' ? true : false
8   - profile.save!
  7 + profile.update_attributes!(params[:profile_attr])
9 8 session[:notice] = _('Option updated successfully.')
10 9 rescue Exception => exception
11 10 session[:notice] = _('Option wasn\'t updated successfully.')
... ...
plugins/shopping_cart/db/migrate/20110513112133_add_shopping_cart_delivery_to_profile.rb 0 → 100644
... ... @@ -0,0 +1,12 @@
  1 +class AddShoppingCartDeliveryToProfile < ActiveRecord::Migration
  2 +
  3 + def self.up
  4 + add_column :profiles, :shopping_cart_delivery, :boolean, :default => false
  5 + add_column :profiles, :shopping_cart_delivery_price, :decimal, :default => 0
  6 + end
  7 +
  8 + def self.down
  9 + remove_column :profiles, :shopping_cart_delivery
  10 + remove_column :profiles, :shopping_cart_delivery_price
  11 + end
  12 +end
... ...
plugins/shopping_cart/lib/shopping_cart_plugin/cart_helper.rb
... ... @@ -15,8 +15,20 @@ module ShoppingCartPlugin::CartHelper
15 15 float_to_currency_cart(items.map { |id, quantity| sell_price(Product.find(id)) * quantity}.sum, environment)
16 16 end
17 17  
18   - def items_table(items, environment, by_mail = false)
19   - '<table id="cart-items-table" cellpadding="2" cellspacing="0"
  18 + def items_table(items, profile, by_mail = false)
  19 + environment = profile.environment
  20 + items = items.to_a
  21 + if profile.shopping_cart_delivery
  22 + delivery = Product.create!(:name => _('Delivery'), :price => profile.shopping_cart_delivery_price, :product_category => ProductCategory.last)
  23 + items << [delivery.id, 1]
  24 + end
  25 +
  26 + quantity_opts = { :class => 'cart-table-quantity' }
  27 + quantity_opts.merge!({:align => 'center'}) if by_mail
  28 + price_opts = {:class => 'cart-table-price'}
  29 + price_opts.merge!({:align => 'right'}) if by_mail
  30 +
  31 + table = '<table id="cart-items-table" cellpadding="2" cellspacing="0"
20 32 border="'+(by_mail ? '1' : '0')+'"
21 33 style="'+(by_mail ? 'border-collapse:collapse' : '')+'">' +
22 34 content_tag('tr',
... ... @@ -26,18 +38,19 @@ module ShoppingCartPlugin::CartHelper
26 38 ) +
27 39 items.map do |id, quantity|
28 40 product = Product.find(id)
29   - quantity_opts = { :class => 'cart-table-quantity' }
30   - quantity_opts.merge!({:align => 'center'}) if by_mail
31   - price_opts = {:class => 'cart-table-price'}
32   - price_opts.merge!({:align => 'right'}) if by_mail
33 41 content_tag('tr',
34 42 content_tag('td', product.name) +
35 43 content_tag('td', quantity, quantity_opts ) +
36 44 content_tag('td', get_price(product, environment), price_opts )
37 45 )
38   - end.join("\n") +
  46 + end.join("\n")
  47 +
  48 + total = get_total(items, environment)
  49 + delivery.destroy if profile.shopping_cart_delivery
  50 +
  51 + table +
39 52 content_tag('th', _('Total:'), :colspan => 2, :class => 'cart-table-total-label') +
40   - content_tag('th', get_total(items, environment), :class => 'cart-table-total-value') +
  53 + content_tag('th', total, :class => 'cart-table-total-value') +
41 54 '</table>'
42 55 end
43 56  
... ...
plugins/shopping_cart/test/functional/shopping_cart_plugin_myprofile_controller_test.rb
... ... @@ -20,7 +20,7 @@ class ShoppingCartPluginMyprofileControllerTest &lt; Test::Unit::TestCase
20 20 should 'be able to enable shopping cart' do
21 21 enterprise.shopping_cart = false
22 22 enterprise.save
23   - post :edit, :profile => enterprise.identifier, :shopping_cart => '1'
  23 + post :edit, :profile => enterprise.identifier, :profile_attr => {:shopping_cart => '1'}
24 24 enterprise.reload
25 25  
26 26 assert enterprise.shopping_cart
... ... @@ -29,9 +29,34 @@ class ShoppingCartPluginMyprofileControllerTest &lt; Test::Unit::TestCase
29 29 should 'be able to disable shopping cart' do
30 30 enterprise.shopping_cart = true
31 31 enterprise.save
32   - post :edit, :profile => enterprise.identifier, :shopping_cart => '0'
  32 + post :edit, :profile => enterprise.identifier, :profile_attr => {:shopping_cart => '0'}
33 33 enterprise.reload
34 34  
35 35 assert !enterprise.shopping_cart
36 36 end
  37 +
  38 + should 'be able to enable shopping cart delivery' do
  39 + enterprise.shopping_cart_delivery = false
  40 + enterprise.save
  41 + post :edit, :profile => enterprise.identifier, :profile_attr => {:shopping_cart_delivery => '1'}
  42 + enterprise.reload
  43 +
  44 + assert enterprise.shopping_cart_delivery
  45 + end
  46 +
  47 + should 'be able to disable shopping cart delivery' do
  48 + enterprise.shopping_cart_delivery = true
  49 + enterprise.save
  50 + post :edit, :profile => enterprise.identifier, :profile_attr => {:shopping_cart_delivery => '0'}
  51 + enterprise.reload
  52 +
  53 + assert !enterprise.shopping_cart_delivery
  54 + end
  55 +
  56 + should 'be able to choose the delivery price' do
  57 + price = 4.35
  58 + post :edit, :profile => enterprise.identifier, :profile_attr => {:shopping_cart_delivery_price => price}
  59 + enterprise.reload
  60 + assert enterprise.shopping_cart_delivery_price == price
  61 + end
37 62 end
... ...
plugins/shopping_cart/views/shopping_cart_plugin/mailer/customer_notification.html.erb
... ... @@ -21,7 +21,7 @@
21 21 </ul>
22 22  
23 23 <p><%=_('Here are the products you bought:')%></p>
24   - <%= items_table(@items, @environment, true) %>
  24 + <%= items_table(@items, @supplier, true) %>
25 25  
26 26 <p>
27 27 --<br/>
... ...
plugins/shopping_cart/views/shopping_cart_plugin/mailer/supplier_notification.html.erb
... ... @@ -21,7 +21,7 @@
21 21 </ul>
22 22  
23 23 <p><%=_('And here are the items bought by this customer:')%></p>
24   - <%= items_table(@items, @environment, true) %>
  24 + <%= items_table(@items, @supplier, true) %>
25 25  
26 26 <p>
27 27 --<br/>
... ...
plugins/shopping_cart/views/shopping_cart_plugin_myprofile/edit.html.erb
1 1 <h1> <%= _('Cart options') %> </h1>
2 2  
3   -<%# form_for :profile, profile, :url => {:action => 'edit'} do %>
4   -<% form_tag :action => 'edit' do %>
5   - <%= labelled_check_box(_('Enabled?'), :shopping_cart, '1', profile.shopping_cart) %>
  3 +<% form_for(:profile_attr, profile, :url => {:action => 'edit'}, :html => {:method => 'post'}) do |f| %>
  4 +<%# form_tag :action => 'edit' do %>
  5 + <%= labelled_form_field(_('Enabled?'), f.check_box(:shopping_cart)) %>
  6 + <%= labelled_form_field(_('Delivery?'), f.check_box(:shopping_cart_delivery)) %>
  7 + <%= labelled_form_field(_('Delivery price:'), f.text_field(:shopping_cart_delivery_price)) %>
6 8 <br style='clear: both'/>
7 9 <br style='clear: both'/>
8 10 <div>
... ...
plugins/shopping_cart/views/shopping_cart_plugin_profile/buy.html.erb
... ... @@ -3,22 +3,21 @@
3 3 <% form_for(:customer, person, :url => {:action => 'send_request'},
4 4 :html => {:onsubmit => "return Cart.send_request(this)", :id => 'cart-request-form' }) do |f| %>
5 5 <div id="cart-form-main">
6   - <%= labelled_form_field('* ' + _("Full name"), f.text_field(:name, :class => 'required') ) %>
  6 + <%= labelled_form_field('* ' + _("Name"), f.text_field(:name, :class => 'required') ) %>
7 7 <%= labelled_form_field('* ' + _("Email"), f.text_field(:email, :class => 'required email') ) %>
8   - <%= labelled_form_field('* ' + _("Phone number"), f.text_field(:contact_phone, :class => 'required') ) %>
  8 + <%= labelled_form_field('* ' + _("Contact phone"), f.text_field(:contact_phone, :class => 'required') ) %>
9 9 </div>
10 10 <fieldset><legend><%=_('Delivery Address')%></legend>
11 11 <%= labelled_form_field(_('Address (street and number)'), f.text_field(:address)) %>
12 12 <%= labelled_form_field( _("City"), f.text_field(:city)) %>
13 13 <%= labelled_form_field( _("State"), f.text_field(:state)) %>
14   - <%= select_country(_('Country'), :customer, :country, {:class => 'type-select'}, {:selected => person.country}) %>
15 14 <%= labelled_form_field(_('ZIP code'), f.text_field(:zip_code)) %>
16 15 </fieldset>
17 16 <div id="cart-form-actions">
18 17 <%= submit_button(:send, _('Send buy request')) %>
19 18 </div>
20 19 <% end %>
21   - <%= items_table(session[:cart][:items], @environment) %>
  20 + <%= items_table(session[:cart][:items], profile) %>
22 21 </div>
23 22  
24 23 <script type="text/javascript">
... ...