Commit f91560291e809c887dc2d8268a8f737924163a6f

Authored by Rodrigo Souto
1 parent 7843b795

Moving shopping_cart settings from profiles table to settings

plugins/shopping_cart/controllers/shopping_cart_plugin_myprofile_controller.rb
@@ -4,9 +4,16 @@ class ShoppingCartPluginMyprofileController < MyProfileController @@ -4,9 +4,16 @@ class ShoppingCartPluginMyprofileController < MyProfileController
4 append_view_path File.join(File.dirname(__FILE__) + '/../views') 4 append_view_path File.join(File.dirname(__FILE__) + '/../views')
5 5
6 def edit 6 def edit
  7 + if params[:settings]
  8 + params[:settings][:enabled] = params[:settings][:enabled] == '1'
  9 + params[:settings][:delivery] = params[:settings][:delivery] == '1'
  10 + params[:settings][:delivery_price] = params[:settings][:delivery_price].to_d
  11 + end
  12 +
  13 + @settings = Noosfero::Plugin::Settings.new(profile, ShoppingCartPlugin, params[:settings])
7 if request.post? 14 if request.post?
8 begin 15 begin
9 - profile.update_attributes!(params[:profile_attr]) 16 + @settings.save!
10 session[:notice] = _('Option updated successfully.') 17 session[:notice] = _('Option updated successfully.')
11 rescue Exception => exception 18 rescue Exception => exception
12 session[:notice] = _('Option wasn\'t updated successfully.') 19 session[:notice] = _('Option wasn\'t updated successfully.')
plugins/shopping_cart/db/migrate/20121022190819_move_fields_included_on_profiles_table_to_settings.rb 0 → 100644
@@ -0,0 +1,19 @@ @@ -0,0 +1,19 @@
  1 +class MoveFieldsIncludedOnProfilesTableToSettings < ActiveRecord::Migration
  2 + def self.up
  3 + Profile.find_each do |profile|
  4 + settings = Noosfero::Plugin::Settings.new(profile, ShoppingCartPlugin)
  5 + settings.enabled = profile.shopping_cart
  6 + settings.delivery = profile.shopping_cart_delivery
  7 + settings.delivery_price = profile.shopping_cart_delivery_price
  8 + settings.save!
  9 + end
  10 +
  11 + remove_column :profiles, :shopping_cart
  12 + remove_column :profiles, :shopping_cart_delivery
  13 + remove_column :profiles, :shopping_cart_delivery_price
  14 + end
  15 +
  16 + def self.down
  17 + say "This migration can not be reverted!"
  18 + end
  19 +end
plugins/shopping_cart/lib/shopping_cart_plugin.rb
@@ -11,8 +11,21 @@ class ShoppingCartPlugin &lt; Noosfero::Plugin @@ -11,8 +11,21 @@ class ShoppingCartPlugin &lt; Noosfero::Plugin
11 _("A shopping basket feature for enterprises") 11 _("A shopping basket feature for enterprises")
12 end 12 end
13 13
  14 + def self.enabled_default_setting
  15 + true
  16 + end
  17 +
  18 + def self.delivery_default_setting
  19 + false
  20 + end
  21 +
  22 + def self.delivery_price_default_setting
  23 + 0
  24 + end
  25 +
14 def add_to_cart_button(item, enterprise = context.profile) 26 def add_to_cart_button(item, enterprise = context.profile)
15 - if enterprise.shopping_cart && item.available 27 + settings = Noosfero::Plugin::Settings.new(enterprise, ShoppingCartPlugin)
  28 + if settings.enabled && item.available
16 lambda { 29 lambda {
17 link_to(_('Add to basket'), "add:#{item.name}", 30 link_to(_('Add to basket'), "add:#{item.name}",
18 :class => 'cart-add-item', 31 :class => 'cart-add-item',
@@ -39,11 +52,12 @@ class ShoppingCartPlugin &lt; Noosfero::Plugin @@ -39,11 +52,12 @@ class ShoppingCartPlugin &lt; Noosfero::Plugin
39 end 52 end
40 53
41 def control_panel_buttons 54 def control_panel_buttons
  55 + settings = Noosfero::Plugin::Settings.new(context.profile, ShoppingCartPlugin)
42 buttons = [] 56 buttons = []
43 if context.profile.enterprise? 57 if context.profile.enterprise?
44 buttons << { :title => _('Shopping basket'), :icon => 'shopping-cart-icon', :url => {:controller => 'shopping_cart_plugin_myprofile', :action => 'edit'} } 58 buttons << { :title => _('Shopping basket'), :icon => 'shopping-cart-icon', :url => {:controller => 'shopping_cart_plugin_myprofile', :action => 'edit'} }
45 end 59 end
46 - if context.profile.enterprise? && context.profile.shopping_cart 60 + if context.profile.enterprise? && settings.enabled
47 buttons << { :title => _('Purchase reports'), :icon => 'shopping-cart-purchase-report', :url => {:controller => 'shopping_cart_plugin_myprofile', :action => 'reports'} } 61 buttons << { :title => _('Purchase reports'), :icon => 'shopping-cart-purchase-report', :url => {:controller => 'shopping_cart_plugin_myprofile', :action => 'reports'} }
48 end 62 end
49 63
plugins/shopping_cart/lib/shopping_cart_plugin/cart_helper.rb
@@ -17,9 +17,11 @@ module ShoppingCartPlugin::CartHelper @@ -17,9 +17,11 @@ module ShoppingCartPlugin::CartHelper
17 17
18 def items_table(items, profile, by_mail = false) 18 def items_table(items, profile, by_mail = false)
19 environment = profile.environment 19 environment = profile.environment
  20 + settings = Noosfero::Plugin::Settings.new(profile, ShoppingCartPlugin)
20 items = items.to_a 21 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) 22 + if settings.delivery
  23 + delivery = Product.new(:name => _('Delivery'), :price => settings.delivery_price)
  24 + delivery.save(false)
23 items << [delivery.id, 1] 25 items << [delivery.id, 1]
24 end 26 end
25 27
@@ -47,7 +49,7 @@ module ShoppingCartPlugin::CartHelper @@ -47,7 +49,7 @@ module ShoppingCartPlugin::CartHelper
47 end.join("\n") 49 end.join("\n")
48 50
49 total = get_total(items, environment) 51 total = get_total(items, environment)
50 - delivery.destroy if profile.shopping_cart_delivery 52 + delivery.destroy if settings.delivery
51 53
52 table + 54 table +
53 content_tag('th', _('Total:'), :colspan => 2, :class => 'cart-table-total-label') + 55 content_tag('th', _('Total:'), :colspan => 2, :class => 'cart-table-total-label') +
plugins/shopping_cart/test/functional/shopping_cart_plugin_myprofile_controller_test.rb
@@ -13,46 +13,42 @@ class ShoppingCartPluginMyprofileControllerTest &lt; ActionController::TestCase @@ -13,46 +13,42 @@ class ShoppingCartPluginMyprofileControllerTest &lt; ActionController::TestCase
13 attr_reader :enterprise 13 attr_reader :enterprise
14 14
15 should 'be able to enable shopping cart' do 15 should 'be able to enable shopping cart' do
16 - enterprise.shopping_cart = false  
17 - enterprise.save  
18 - post :edit, :profile => enterprise.identifier, :profile_attr => {:shopping_cart => '1'}  
19 - enterprise.reload 16 + settings.enabled = false
  17 + settings.save!
  18 + post :edit, :profile => enterprise.identifier, :settings => {:enabled => '1'}
20 19
21 - assert enterprise.shopping_cart 20 + assert settings.enabled
22 end 21 end
23 22
24 should 'be able to disable shopping cart' do 23 should 'be able to disable shopping cart' do
25 - enterprise.shopping_cart = true  
26 - enterprise.save  
27 - post :edit, :profile => enterprise.identifier, :profile_attr => {:shopping_cart => '0'}  
28 - enterprise.reload 24 + settings.enabled = true
  25 + settings.save!
  26 + post :edit, :profile => enterprise.identifier, :settings => {:enabled => '0'}
29 27
30 - assert !enterprise.shopping_cart 28 + assert !settings.enabled
31 end 29 end
32 30
33 should 'be able to enable shopping cart delivery' do 31 should 'be able to enable shopping cart delivery' do
34 - enterprise.shopping_cart_delivery = false  
35 - enterprise.save  
36 - post :edit, :profile => enterprise.identifier, :profile_attr => {:shopping_cart_delivery => '1'}  
37 - enterprise.reload 32 + settings.delivery = false
  33 + settings.save!
  34 + post :edit, :profile => enterprise.identifier, :settings => {:delivery => '1'}
38 35
39 - assert enterprise.shopping_cart_delivery 36 + assert settings.delivery
40 end 37 end
41 38
42 should 'be able to disable shopping cart delivery' do 39 should 'be able to disable shopping cart delivery' do
43 - enterprise.shopping_cart_delivery = true  
44 - enterprise.save  
45 - post :edit, :profile => enterprise.identifier, :profile_attr => {:shopping_cart_delivery => '0'}  
46 - enterprise.reload 40 + settings.delivery = true
  41 + settings.save!
  42 + post :edit, :profile => enterprise.identifier, :settings => {:delivery => '0'}
47 43
48 - assert !enterprise.shopping_cart_delivery 44 + assert !settings.delivery
49 end 45 end
50 46
51 should 'be able to choose the delivery price' do 47 should 'be able to choose the delivery price' do
52 price = 4.35 48 price = 4.35
53 - post :edit, :profile => enterprise.identifier, :profile_attr => {:shopping_cart_delivery_price => price}  
54 - enterprise.reload  
55 - assert enterprise.shopping_cart_delivery_price == price 49 + post :edit, :profile => enterprise.identifier, :settings => {:delivery_price => price}
  50 +
  51 + assert settings.delivery_price == price
56 end 52 end
57 53
58 should 'filter the reports correctly' do 54 should 'filter the reports correctly' do
@@ -112,4 +108,11 @@ class ShoppingCartPluginMyprofileControllerTest &lt; ActionController::TestCase @@ -112,4 +108,11 @@ class ShoppingCartPluginMyprofileControllerTest &lt; ActionController::TestCase
112 po.reload 108 po.reload
113 assert_equal ShoppingCartPlugin::PurchaseOrder::Status::CONFIRMED, po.status 109 assert_equal ShoppingCartPlugin::PurchaseOrder::Status::CONFIRMED, po.status
114 end 110 end
  111 +
  112 + private
  113 +
  114 + def settings
  115 + @enterprise.reload
  116 + Noosfero::Plugin::Settings.new(@enterprise, ShoppingCartPlugin)
  117 + end
115 end 118 end
plugins/shopping_cart/views/shopping_cart_plugin_myprofile/edit.html.erb
1 <h1> <%= _('Basket options') %> </h1> 1 <h1> <%= _('Basket options') %> </h1>
2 2
3 -<% form_for(:profile_attr, profile, :url => {:action => 'edit'}, :html => {:method => 'post'}) do |f| %>  
4 - <%= labelled_form_field(_('Enabled?'), f.check_box(:shopping_cart)) %>  
5 - <%= labelled_form_field(_('Delivery?'), f.check_box(:shopping_cart_delivery)) %>  
6 - <%= labelled_form_field(_('Delivery price:'), f.text_field(:shopping_cart_delivery_price)) %> 3 +<% form_for(:settings, @settings, :url => {:action => 'edit'}, :html => {:method => 'post'}) do |f| %>
  4 + <%= labelled_form_field(_('Enabled?'), f.check_box(:enabled)) %>
  5 + <%= labelled_form_field(_('Delivery?'), f.check_box(:delivery)) %>
  6 + <%= labelled_form_field(_('Delivery price:'), f.text_field(:delivery_price)) %>
7 <br style='clear: both'/> 7 <br style='clear: both'/>
8 <br style='clear: both'/> 8 <br style='clear: both'/>
9 <div> 9 <div>