diff --git a/plugins/shopping_cart/controllers/shopping_cart_plugin_myprofile_controller.rb b/plugins/shopping_cart/controllers/shopping_cart_plugin_myprofile_controller.rb index c3c29a9..de7ce05 100644 --- a/plugins/shopping_cart/controllers/shopping_cart_plugin_myprofile_controller.rb +++ b/plugins/shopping_cart/controllers/shopping_cart_plugin_myprofile_controller.rb @@ -4,9 +4,16 @@ class ShoppingCartPluginMyprofileController < MyProfileController append_view_path File.join(File.dirname(__FILE__) + '/../views') def edit + if params[:settings] + params[:settings][:enabled] = params[:settings][:enabled] == '1' + params[:settings][:delivery] = params[:settings][:delivery] == '1' + params[:settings][:delivery_price] = params[:settings][:delivery_price].to_d + end + + @settings = Noosfero::Plugin::Settings.new(profile, ShoppingCartPlugin, params[:settings]) if request.post? begin - profile.update_attributes!(params[:profile_attr]) + @settings.save! session[:notice] = _('Option updated successfully.') rescue Exception => exception session[:notice] = _('Option wasn\'t updated successfully.') diff --git a/plugins/shopping_cart/db/migrate/20121022190819_move_fields_included_on_profiles_table_to_settings.rb b/plugins/shopping_cart/db/migrate/20121022190819_move_fields_included_on_profiles_table_to_settings.rb new file mode 100644 index 0000000..b071aec --- /dev/null +++ b/plugins/shopping_cart/db/migrate/20121022190819_move_fields_included_on_profiles_table_to_settings.rb @@ -0,0 +1,19 @@ +class MoveFieldsIncludedOnProfilesTableToSettings < ActiveRecord::Migration + def self.up + Profile.find_each do |profile| + settings = Noosfero::Plugin::Settings.new(profile, ShoppingCartPlugin) + settings.enabled = profile.shopping_cart + settings.delivery = profile.shopping_cart_delivery + settings.delivery_price = profile.shopping_cart_delivery_price + settings.save! + end + + remove_column :profiles, :shopping_cart + remove_column :profiles, :shopping_cart_delivery + remove_column :profiles, :shopping_cart_delivery_price + end + + def self.down + say "This migration can not be reverted!" + end +end diff --git a/plugins/shopping_cart/lib/shopping_cart_plugin.rb b/plugins/shopping_cart/lib/shopping_cart_plugin.rb index e335f32..2ddda29 100644 --- a/plugins/shopping_cart/lib/shopping_cart_plugin.rb +++ b/plugins/shopping_cart/lib/shopping_cart_plugin.rb @@ -11,8 +11,21 @@ class ShoppingCartPlugin < Noosfero::Plugin _("A shopping basket feature for enterprises") end + def self.enabled_default_setting + true + end + + def self.delivery_default_setting + false + end + + def self.delivery_price_default_setting + 0 + end + def add_to_cart_button(item, enterprise = context.profile) - if enterprise.shopping_cart && item.available + settings = Noosfero::Plugin::Settings.new(enterprise, ShoppingCartPlugin) + if settings.enabled && item.available lambda { link_to(_('Add to basket'), "add:#{item.name}", :class => 'cart-add-item', @@ -39,11 +52,12 @@ class ShoppingCartPlugin < Noosfero::Plugin end def control_panel_buttons + settings = Noosfero::Plugin::Settings.new(context.profile, ShoppingCartPlugin) buttons = [] if context.profile.enterprise? buttons << { :title => _('Shopping basket'), :icon => 'shopping-cart-icon', :url => {:controller => 'shopping_cart_plugin_myprofile', :action => 'edit'} } end - if context.profile.enterprise? && context.profile.shopping_cart + if context.profile.enterprise? && settings.enabled buttons << { :title => _('Purchase reports'), :icon => 'shopping-cart-purchase-report', :url => {:controller => 'shopping_cart_plugin_myprofile', :action => 'reports'} } end diff --git a/plugins/shopping_cart/lib/shopping_cart_plugin/cart_helper.rb b/plugins/shopping_cart/lib/shopping_cart_plugin/cart_helper.rb index e0a3c86..d847b25 100644 --- a/plugins/shopping_cart/lib/shopping_cart_plugin/cart_helper.rb +++ b/plugins/shopping_cart/lib/shopping_cart_plugin/cart_helper.rb @@ -17,9 +17,11 @@ module ShoppingCartPlugin::CartHelper def items_table(items, profile, by_mail = false) environment = profile.environment + settings = Noosfero::Plugin::Settings.new(profile, ShoppingCartPlugin) items = items.to_a - if profile.shopping_cart_delivery - delivery = Product.create!(:name => _('Delivery'), :price => profile.shopping_cart_delivery_price, :product_category => ProductCategory.last) + if settings.delivery + delivery = Product.new(:name => _('Delivery'), :price => settings.delivery_price) + delivery.save(false) items << [delivery.id, 1] end @@ -47,7 +49,7 @@ module ShoppingCartPlugin::CartHelper end.join("\n") total = get_total(items, environment) - delivery.destroy if profile.shopping_cart_delivery + delivery.destroy if settings.delivery table + content_tag('th', _('Total:'), :colspan => 2, :class => 'cart-table-total-label') + diff --git a/plugins/shopping_cart/test/functional/shopping_cart_plugin_myprofile_controller_test.rb b/plugins/shopping_cart/test/functional/shopping_cart_plugin_myprofile_controller_test.rb index 5a29613..306a7f8 100644 --- a/plugins/shopping_cart/test/functional/shopping_cart_plugin_myprofile_controller_test.rb +++ b/plugins/shopping_cart/test/functional/shopping_cart_plugin_myprofile_controller_test.rb @@ -13,46 +13,42 @@ class ShoppingCartPluginMyprofileControllerTest < ActionController::TestCase attr_reader :enterprise should 'be able to enable shopping cart' do - enterprise.shopping_cart = false - enterprise.save - post :edit, :profile => enterprise.identifier, :profile_attr => {:shopping_cart => '1'} - enterprise.reload + settings.enabled = false + settings.save! + post :edit, :profile => enterprise.identifier, :settings => {:enabled => '1'} - assert enterprise.shopping_cart + assert settings.enabled end should 'be able to disable shopping cart' do - enterprise.shopping_cart = true - enterprise.save - post :edit, :profile => enterprise.identifier, :profile_attr => {:shopping_cart => '0'} - enterprise.reload + settings.enabled = true + settings.save! + post :edit, :profile => enterprise.identifier, :settings => {:enabled => '0'} - assert !enterprise.shopping_cart + assert !settings.enabled end should 'be able to enable shopping cart delivery' do - enterprise.shopping_cart_delivery = false - enterprise.save - post :edit, :profile => enterprise.identifier, :profile_attr => {:shopping_cart_delivery => '1'} - enterprise.reload + settings.delivery = false + settings.save! + post :edit, :profile => enterprise.identifier, :settings => {:delivery => '1'} - assert enterprise.shopping_cart_delivery + assert settings.delivery end should 'be able to disable shopping cart delivery' do - enterprise.shopping_cart_delivery = true - enterprise.save - post :edit, :profile => enterprise.identifier, :profile_attr => {:shopping_cart_delivery => '0'} - enterprise.reload + settings.delivery = true + settings.save! + post :edit, :profile => enterprise.identifier, :settings => {:delivery => '0'} - assert !enterprise.shopping_cart_delivery + assert !settings.delivery end should 'be able to choose the delivery price' do price = 4.35 - post :edit, :profile => enterprise.identifier, :profile_attr => {:shopping_cart_delivery_price => price} - enterprise.reload - assert enterprise.shopping_cart_delivery_price == price + post :edit, :profile => enterprise.identifier, :settings => {:delivery_price => price} + + assert settings.delivery_price == price end should 'filter the reports correctly' do @@ -112,4 +108,11 @@ class ShoppingCartPluginMyprofileControllerTest < ActionController::TestCase po.reload assert_equal ShoppingCartPlugin::PurchaseOrder::Status::CONFIRMED, po.status end + + private + + def settings + @enterprise.reload + Noosfero::Plugin::Settings.new(@enterprise, ShoppingCartPlugin) + end end diff --git a/plugins/shopping_cart/views/shopping_cart_plugin_myprofile/edit.html.erb b/plugins/shopping_cart/views/shopping_cart_plugin_myprofile/edit.html.erb index f7d88e6..34194b9 100644 --- a/plugins/shopping_cart/views/shopping_cart_plugin_myprofile/edit.html.erb +++ b/plugins/shopping_cart/views/shopping_cart_plugin_myprofile/edit.html.erb @@ -1,9 +1,9 @@