From 932d74f8b65c6cad2ed4db8f009d8237d604180c Mon Sep 17 00:00:00 2001 From: Daniela Soares Feitosa Date: Thu, 22 Jul 2010 10:36:59 -0300 Subject: [PATCH] Added structure to add inputs to a product --- app/controllers/my_profile/manage_products_controller.rb | 24 ++++++++++++++++++------ app/helpers/manage_products_helper.rb | 28 +++++++++++++++++----------- app/models/consumption.rb | 9 --------- app/models/input.rb | 7 +++++++ app/models/product.rb | 4 +++- app/models/product_category.rb | 3 +-- app/models/profile.rb | 3 --- app/views/manage_products/_categories_for_selection.rhtml | 2 +- app/views/manage_products/_display_category.rhtml | 2 +- app/views/manage_products/_display_image.rhtml | 2 +- app/views/manage_products/_display_info.rhtml | 4 ++-- app/views/manage_products/_display_inputs.rhtml | 2 ++ app/views/manage_products/_display_name.rhtml | 2 +- app/views/manage_products/add_input.rhtml | 33 +++++++++++++++++++++++++++++++++ app/views/manage_products/edit_category.rhtml | 11 +++++------ app/views/manage_products/new.rhtml | 4 ++-- app/views/manage_products/show.rhtml | 4 ++++ db/migrate/20100719142230_create_inputs.rb | 14 ++++++++++++++ db/migrate/20100719142737_move_consumptions_to_inputs_and_destroy_consumptions.rb | 19 +++++++++++++++++++ db/schema.rb | 13 +++++++------ features/manage_products.feature | 34 ++++++++++++++++++++++++++++++++++ public/stylesheets/application.css | 9 +++++++-- test/factories.rb | 8 ++++++++ test/fixtures/consumptions.yml | 5 ----- test/functional/manage_products_controller_test.rb | 55 +++++++++++++++++++++++++++++++++---------------------- test/unit/consumption_test.rb | 14 -------------- test/unit/input_test.rb | 30 ++++++++++++++++++++++++++++++ test/unit/manage_products_helper_test.rb | 50 ++++++++++++++++++++++++++++++++++++++++++++++---- test/unit/product_category_test.rb | 7 ------- test/unit/product_test.rb | 33 ++++++++++++++++++++++++++++++++- 30 files changed, 328 insertions(+), 107 deletions(-) delete mode 100644 app/models/consumption.rb create mode 100644 app/models/input.rb create mode 100644 app/views/manage_products/_display_inputs.rhtml create mode 100644 app/views/manage_products/add_input.rhtml create mode 100644 db/migrate/20100719142230_create_inputs.rb create mode 100644 db/migrate/20100719142737_move_consumptions_to_inputs_and_destroy_consumptions.rb delete mode 100644 test/fixtures/consumptions.yml delete mode 100644 test/unit/consumption_test.rb create mode 100644 test/unit/input_test.rb diff --git a/app/controllers/my_profile/manage_products_controller.rb b/app/controllers/my_profile/manage_products_controller.rb index 0fc0e83..9e3a954 100644 --- a/app/controllers/my_profile/manage_products_controller.rb +++ b/app/controllers/my_profile/manage_products_controller.rb @@ -18,7 +18,6 @@ class ManageProductsController < ApplicationController def index @products = @profile.products - @consumptions = @profile.consumptions end def show @@ -38,7 +37,7 @@ class ManageProductsController < ApplicationController end def new - @product = @profile.products.build(params[:product]) + @product = @profile.products.build(:product_category_id => params[:selected_category_id]) @category = @product.product_category @categories = ProductCategory.top_level_for(environment) @level = 0 @@ -75,12 +74,25 @@ class ManageProductsController < ApplicationController @edit = true @level = @category.level if request.post? - begin - @product.update_attributes!(params[:product]) + if @product.update_attributes(:product_category_id => params[:selected_category_id]) render :partial => 'shared/redirect_via_javascript', :locals => { :url => url_for(:controller => 'manage_products', :action => 'show', :id => @product) } - rescue Exception => e - flash[:notice] = _('Could not update the product') + else + render :partial => 'shared/dialog_error_messages', :locals => { :object_name => 'product' } + end + end + end + + def add_input + @product = @profile.products.find(params[:id]) + @input = @product.inputs.build + @categories = ProductCategory.top_level_for(environment) + @level = 0 + if request.post? + if @input.update_attributes(:product_category_id => params[:selected_category_id]) + render :partial => 'shared/redirect_via_javascript', + :locals => { :url => url_for(:controller => 'manage_products', :action => 'show', :id => @product) } + else render :partial => 'shared/dialog_error_messages', :locals => { :object_name => 'product' } end end diff --git a/app/helpers/manage_products_helper.rb b/app/helpers/manage_products_helper.rb index 682a1f3..1e2da83 100644 --- a/app/helpers/manage_products_helper.rb +++ b/app/helpers/manage_products_helper.rb @@ -89,8 +89,8 @@ module ManageProductsHelper ) end - def categories_container(field_id_html, categories_selection_html, hierarchy_html = '') - field_id_html + + def categories_container(categories_selection_html, hierarchy_html = '') + hidden_field_tag('selected_category_id') + content_tag('div', hierarchy_html, :id => 'hierarchy_navigation') + content_tag('div', categories_selection_html, :id => 'categories_container_wrapper') end @@ -108,7 +108,13 @@ module ManageProductsHelper end end - def edit_product_link(product, field, label, html_options = {}) + def edit_product_link(field, label, url, html_options = {}) + return '' unless (user && user.has_permission?('manage_products', profile)) + options = html_options.merge(:id => "link-edit-#{field}") + link_to(label, url, options) + end + + def edit_product_link_to_remote(product, field, label, html_options = {}) return '' unless (user && user.has_permission?('manage_products', profile)) options = html_options.merge(:id => 'link-edit-product-' + field) @@ -124,12 +130,18 @@ module ManageProductsHelper if html_options.has_key?(:class) the_class << ' ' << html_options[:class] end - edit_product_link(product, field, label, html_options.merge(:class => the_class)) + edit_product_link_to_remote(product, field, label, html_options.merge(:class => the_class)) end - def edit_product_ui_button(product, field, label, html_options = {}) + def edit_product_ui_button(field, label, url, html_options = {}) return '' unless (user && user.has_permission?('manage_products', profile)) options = html_options.merge(:id => 'edit-product-button-ui-' + field) + ui_button(label, url, options) + end + + def edit_product_ui_button_to_remote(product, field, label, html_options = {}) + return '' unless (user && user.has_permission?('manage_products', profile)) + options = html_options.merge(:id => 'edit-product-remote-button-ui-' + field) ui_button_to_remote(label, {:update => "product-#{field}", @@ -146,12 +158,6 @@ module ManageProductsHelper end end - def edit_product_category_link(product, html_options = {}) - return '' unless (user && user.has_permission?('manage_products', profile)) - options = html_options.merge(:id => 'link-edit-product-category') - link_to(_('Change category'), { :action => 'edit_category', :id => product.id}, options) - end - def float_to_currency(value) number_to_currency(value, :unit => environment.currency_unit, :separator => environment.currency_separator, :delimiter => environment.currency_delimiter, :format => "%u %n") end diff --git a/app/models/consumption.rb b/app/models/consumption.rb deleted file mode 100644 index 86a1e70..0000000 --- a/app/models/consumption.rb +++ /dev/null @@ -1,9 +0,0 @@ -class Consumption < ActiveRecord::Base - belongs_to :profile - belongs_to :product_category - - validates_uniqueness_of :product_category_id, :scope => :profile_id - - xss_terminate :only => [ :aditional_specifications ], :on => 'validation' - -end diff --git a/app/models/input.rb b/app/models/input.rb new file mode 100644 index 0000000..94d455e --- /dev/null +++ b/app/models/input.rb @@ -0,0 +1,7 @@ +class Input < ActiveRecord::Base + belongs_to :product + belongs_to :product_category + + validates_presence_of :product + validates_presence_of :product_category +end diff --git a/app/models/product.rb b/app/models/product.rb index ffcc59c..20436ca 100644 --- a/app/models/product.rb +++ b/app/models/product.rb @@ -4,9 +4,11 @@ class Product < ActiveRecord::Base has_many :product_categorizations has_many :product_qualifiers has_many :qualifiers, :through => :product_qualifiers + has_many :inputs, :dependent => :destroy validates_uniqueness_of :name, :scope => :enterprise_id, :allow_nil => true - validates_presence_of :product_category + validates_presence_of :product_category_id + validates_associated :product_category validates_numericality_of :price, :allow_nil => true validates_numericality_of :discount, :allow_nil => true diff --git a/app/models/product_category.rb b/app/models/product_category.rb index c3c9aa5..d37bf57 100644 --- a/app/models/product_category.rb +++ b/app/models/product_category.rb @@ -1,7 +1,6 @@ class ProductCategory < Category has_many :products - has_many :consumptions - has_many :consumers, :through => :consumptions, :source => :profile + has_many :inputs def all_products Product.find(:all, :conditions => { :product_category_id => (all_children << self).map(&:id) }) diff --git a/app/models/profile.rb b/app/models/profile.rb index d9871d0..2e22541 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -120,9 +120,6 @@ class Profile < ActiveRecord::Base acts_as_having_image - has_many :consumptions - has_many :consumed_product_categories, :through => :consumptions, :source => :product_category - has_many :tasks, :dependent => :destroy, :as => 'target' has_many :events, :source => 'articles', :class_name => 'Event', :order => 'name' diff --git a/app/views/manage_products/_categories_for_selection.rhtml b/app/views/manage_products/_categories_for_selection.rhtml index 08100e8..bd2a274 100644 --- a/app/views/manage_products/_categories_for_selection.rhtml +++ b/app/views/manage_products/_categories_for_selection.rhtml @@ -2,7 +2,7 @@ <% javascript_tag do %> jQuery('#categories_container_wrapper').scrollTo('100%', 1000) - $('product_product_category_id').value = <%= @category && @category.id %> + $('selected_category_id').value = <%= @category && @category.id %> $('hierarchy_navigation').update('<%= escape_javascript(hierarchy_category_navigation(@category, :make_links => true)) %>') toggleDisabled(<%= @category && @category.accept_products? ? 'true' : 'false' %>, $('save_and_continue')) <% end %> diff --git a/app/views/manage_products/_display_category.rhtml b/app/views/manage_products/_display_category.rhtml index 415b521..f29c169 100644 --- a/app/views/manage_products/_display_category.rhtml +++ b/app/views/manage_products/_display_category.rhtml @@ -1,4 +1,4 @@

<%= hierarchy_category_navigation(@product.product_category, :make_links => false)%>

- <%= edit_product_category_link(@product) %> + <%= edit_product_link('product-category', _('Change category'), { :action => 'edit_category', :id => @product.id}) %>
diff --git a/app/views/manage_products/_display_image.rhtml b/app/views/manage_products/_display_image.rhtml index f24c636..3ed4218 100644 --- a/app/views/manage_products/_display_image.rhtml +++ b/app/views/manage_products/_display_image.rhtml @@ -2,4 +2,4 @@ <%= image_tag (@product.reload.image ? @product.image.public_filename : @product.default_image('thumb')), :class => 'product-pic' %> -<%= edit_product_link(@product, 'image', _('Change image')) %> +<%= edit_product_link_to_remote(@product, 'image', _('Change image')) %> diff --git a/app/views/manage_products/_display_info.rhtml b/app/views/manage_products/_display_info.rhtml index f3746fe..ad1c430 100644 --- a/app/views/manage_products/_display_info.rhtml +++ b/app/views/manage_products/_display_info.rhtml @@ -9,6 +9,6 @@ <% if @product.has_basic_info? %> <%= edit_product_button(@product, 'info', _('Edit basic information')) %> <% else %> - <%= edit_product_ui_button(@product, 'info', _('Add description, price and other basic information'), :title => _('Click here to add description, price, discount and qualifiers/certifiers to make your product more attractive and detailed for the consumers'), 'data-primary-icon' => 'ui-icon-pencil', 'data-secondary-icon' => 'ui-icon-triangle-1-s') %> - <%= javascript_tag("render_jquery_ui_buttons('edit-product-button-ui-info')") %> + <%= edit_product_ui_button_to_remote(@product, 'info', _('Add description, price and other basic information'), :title => _('Click here to add description, price, discount and qualifiers/certifiers to make your product more attractive and detailed for the consumers'), 'data-primary-icon' => 'ui-icon-pencil', 'data-secondary-icon' => 'ui-icon-triangle-1-s') %> + <%= javascript_tag("render_jquery_ui_buttons('edit-product-remote-button-ui-info')") %> <% end%> diff --git a/app/views/manage_products/_display_inputs.rhtml b/app/views/manage_products/_display_inputs.rhtml new file mode 100644 index 0000000..38faf94 --- /dev/null +++ b/app/views/manage_products/_display_inputs.rhtml @@ -0,0 +1,2 @@ +<%= edit_product_ui_button('inputs', _('Add the inputs used by this product'), {:action => 'add_input', :id => @product.id}, 'data-primary-icon' => 'ui-icon-pencil', 'data-secondary-icon' => 'ui-icon-triangle-1-s') %> +<%= javascript_tag("render_jquery_ui_buttons('edit-product-button-ui-inputs')") %> diff --git a/app/views/manage_products/_display_name.rhtml b/app/views/manage_products/_display_name.rhtml index abfd282..419849e 100644 --- a/app/views/manage_products/_display_name.rhtml +++ b/app/views/manage_products/_display_name.rhtml @@ -1,5 +1,5 @@

<%= @product.reload.name %>

- <%= edit_product_link(@product, 'name', _('Edit name')) %> + <%= edit_product_link_to_remote(@product, 'name', _('Edit name')) %>
diff --git a/app/views/manage_products/add_input.rhtml b/app/views/manage_products/add_input.rhtml new file mode 100644 index 0000000..1761d99 --- /dev/null +++ b/app/views/manage_products/add_input.rhtml @@ -0,0 +1,33 @@ +
+ <%= hierarchy_category_navigation(@product.product_category, :make_links => false)%> +
+ +
+ +

<%= @product.name %>

+ + + + <% remote_form_for(@input, + :loading => "open_loading('#{ _('loading...') }')", + :update => "request_result_message", + :url => {:action => 'add_input', :id => @product, :field => 'category'}, + :html => {:method => 'post', :id => 'category_form'}) do |f| %> + +

<%= _('Choose an input to this product:') %>

+ + <%= categories_container(select_for_new_category) %> + +
+ <%= button(:back, _('Back to product'), :action => 'show', :id => @product) %> + + <%= submit_button(:save, _('Save and continue'), :id => 'save_and_continue') %> + + <%= ui_icon('ui-icon-alert') %> + <%= _('This category does not allow registration of products, select a more specific category') %> + + +
+ + <% end %> +
diff --git a/app/views/manage_products/edit_category.rhtml b/app/views/manage_products/edit_category.rhtml index 98b2655..1b12874 100644 --- a/app/views/manage_products/edit_category.rhtml +++ b/app/views/manage_products/edit_category.rhtml @@ -1,5 +1,5 @@
- <%= hierarchy_category_navigation(@product.product_category, :make_links => false)%> + <%= hierarchy_category_navigation(@category, :make_links => false)%>
@@ -8,16 +8,15 @@ - <% remote_form_for(:product, @product, + <% remote_form_for(@product, :loading => "open_loading('#{ _('loading...') }')", - :loaded => "close_loading()", :update => "request_result_message", - :url => {:action => 'edit_category', :id => @product, :field => 'category'}, - :html => {:method => 'post', :id => 'product_category_form'}) do |f| %> + :url => {:action => 'edit_category', :id => @product}, + :html => {:method => 'post', :id => 'category_form'}) do |f| %>

<%= _('Edit category of this product:') %>

- <%= categories_container(f.hidden_field('product_category_id'), selects_for_all_ancestors(@category), hierarchy_category_navigation(@category, :make_links => true)) %> + <%= categories_container(selects_for_all_ancestors(@category), hierarchy_category_navigation(@category, :make_links => true)) %>
<%= button(:back, _('Back to product'), :action => 'show', :id => @product) %> diff --git a/app/views/manage_products/new.rhtml b/app/views/manage_products/new.rhtml index ed7ff99..1a2fc6f 100644 --- a/app/views/manage_products/new.rhtml +++ b/app/views/manage_products/new.rhtml @@ -6,11 +6,11 @@ :loading => "open_loading('#{ _('loading...') }')", :update => "request_result_message", :url => {:action => 'new'}, - :html => {:method => 'post', :id => 'product_category_form'} do |f| %> + :html => {:method => 'post', :id => 'category_form'} do |f| %>

<%= _('Select the category of the new product or service') %>

- <%= categories_container(f.hidden_field('product_category_id'), select_for_new_category) %> + <%= categories_container(select_for_new_category) %>
<%= button :back, _('Back to the product listing'), :action => 'index' %> diff --git a/app/views/manage_products/show.rhtml b/app/views/manage_products/show.rhtml index f4effd9..c44f7fe 100644 --- a/app/views/manage_products/show.rhtml +++ b/app/views/manage_products/show.rhtml @@ -17,6 +17,10 @@ <%= render :partial => 'manage_products/display_info', :locals => {:product => @product} %>
+
+ <%= render :partial => 'manage_products/display_inputs', :locals => {:product => @product} %> +
+
diff --git a/db/migrate/20100719142230_create_inputs.rb b/db/migrate/20100719142230_create_inputs.rb new file mode 100644 index 0000000..2968e58 --- /dev/null +++ b/db/migrate/20100719142230_create_inputs.rb @@ -0,0 +1,14 @@ +class CreateInputs < ActiveRecord::Migration + def self.up + create_table :inputs do |t| + t.references :product, :null => false + t.references :product_category, :null => false + + t.timestamps + end + end + + def self.down + drop_table :inputs + end +end diff --git a/db/migrate/20100719142737_move_consumptions_to_inputs_and_destroy_consumptions.rb b/db/migrate/20100719142737_move_consumptions_to_inputs_and_destroy_consumptions.rb new file mode 100644 index 0000000..c7d6e32 --- /dev/null +++ b/db/migrate/20100719142737_move_consumptions_to_inputs_and_destroy_consumptions.rb @@ -0,0 +1,19 @@ +class MoveConsumptionsToInputsAndDestroyConsumptions < ActiveRecord::Migration + def self.up + select_all('SELECT product_category_id, profile_id FROM consumptions').each do |consumption| + Profile.find(consumption['profile_id']).products.each do |product| + Input.create(:product_category_id => consumption['product_category_id'], :product_id => product.id) + end + end + drop_table :consumptions + end + + def self.down + say 'Warning: This migration cant recover data from old cunsumptions table' + create_table :consumptions do |t| + t.column :product_category_id, :integer + t.column :profile_id, :integer + t.column :aditional_specifications, :text + end + end +end diff --git a/db/schema.rb b/db/schema.rb index e287b35..e1cacfa 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -160,12 +160,6 @@ ActiveRecord::Schema.define(:version => 20100722020357) do t.datetime "created_at" end - create_table "consumptions", :force => true do |t| - t.integer "product_category_id" - t.integer "profile_id" - t.text "aditional_specifications" - end - create_table "domains", :force => true do |t| t.string "name" t.string "owner_type" @@ -228,6 +222,13 @@ ActiveRecord::Schema.define(:version => 20100722020357) do t.integer "height" end + create_table "inputs", :force => true do |t| + t.integer "product_category_id", :null => false + t.integer "product_id", :null => false + t.datetime "created_at" + t.datetime "updated_at" + end + create_table "product_categorizations", :force => true do |t| t.integer "category_id" t.integer "product_id" diff --git a/features/manage_products.feature b/features/manage_products.feature index 806cecc..af8ff3c 100644 --- a/features/manage_products.feature +++ b/features/manage_products.feature @@ -351,3 +351,37 @@ Feature: manage products When I follow "Cancel" Then I should see "A new red bicycle" And I should be on Rede Moinho's page of product Bike + + @selenium + Scenario: add an input to a product + Given the following product_category + | name | + | Food | + And the following product_categories + | name | parent | + | Cake | food | + | Sugar | food | + And the following products + | owner | category | name | + | redemoinho | cake | Chocolate cake | + And I am logged in as "joaosilva" + When I go to Rede Moinho's page of product Chocolate cake + And I follow "Add the inputs used by this product" + And I select "Food ยป" + And I select "Sugar" + And I press "Save and continue" + Then I should see "Sugar" + + @selenium + Scenario: cancel addition of a product input + Given the following product_category + | name | + | Food | + And the following products + | owner | category | name | + | redemoinho | food | Cake | + And I am logged in as "joaosilva" + When I go to Rede Moinho's page of product Cake + And I follow "Add the inputs used by this product" + When I follow "Back to product" + Then I should see "Cake" diff --git a/public/stylesheets/application.css b/public/stylesheets/application.css index 2d676a2..6fad7ee 100644 --- a/public/stylesheets/application.css +++ b/public/stylesheets/application.css @@ -2613,7 +2613,7 @@ div#activation_enterprise div { list-style: none; } -#edit-product-button-ui-info { +#edit-product-remote-button-ui-info { margin-top: 40px; } @@ -2655,6 +2655,11 @@ div#activation_enterprise div { #product-qualifiers-list .product-certifier-title { margin-right: 20px; } + +#edit-product-button-ui-inputs { + margin-top: 20px; +} + /* ==> public/stylesheets/controller_cms.css <== */ .controller-cms #article_types li { @@ -3142,7 +3147,7 @@ h1#agenda-title { /* ==> public/stylesheets/controller_manage_products.css <== */ -#product_category_form { +#category_form { border: 1px solid #AABB88; -moz-border-radius: 5px; -webkit-border-radius: 5px; diff --git a/test/factories.rb b/test/factories.rb index 3e26bca..34679c7 100644 --- a/test/factories.rb +++ b/test/factories.rb @@ -330,4 +330,12 @@ module Noosfero::Factory { :name => 'Product ' + factory_num_seq.to_s } end + ############################################### + # Input + ############################################### + + def defaults_for_input + { } + end + end diff --git a/test/fixtures/consumptions.yml b/test/fixtures/consumptions.yml deleted file mode 100644 index b49c4eb..0000000 --- a/test/fixtures/consumptions.yml +++ /dev/null @@ -1,5 +0,0 @@ -# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html -one: - id: 1 -two: - id: 2 diff --git a/test/functional/manage_products_controller_test.rb b/test/functional/manage_products_controller_test.rb index 5760ce8..a0f9ed1 100644 --- a/test/functional/manage_products_controller_test.rb +++ b/test/functional/manage_products_controller_test.rb @@ -50,7 +50,7 @@ class ManageProductsControllerTest < Test::Unit::TestCase should "create new product" do assert_difference Product, :count do - post 'new', :profile => @enterprise.identifier, :product => {:name => 'test product', :product_category_id => @product_category.id} + post 'new', :profile => @enterprise.identifier, :product => {:name => 'test product'}, :selected_category_id => @product_category.id assert assigns(:product) assert !assigns(:product).new_record? end @@ -117,17 +117,16 @@ class ManageProductsControllerTest < Test::Unit::TestCase end should "not edit to invalid parameters" do - product = fast_create(Product, :name => 'test product', :enterprise_id => @enterprise.id, :product_category_id => @product_category.id) - post 'edit_category', :profile => @enterprise.identifier, :product => {:product_category => nil}, :id => product.id + product = fast_create(Product, :enterprise_id => @enterprise.id, :product_category_id => @product_category.id) + post 'edit_category', :profile => @enterprise.identifier, :selected_category_id => nil, :id => product.id assert_response :success - assert assigns(:product) - assert ! assigns(:product).valid? + assert_template 'shared/_dialog_error_messages' end should "destroy product" do - p = @enterprise.products.create!(:name => 'test product', :product_category => @product_category) + product = fast_create(Product, :name => 'test product', :enterprise_id => @enterprise.id, :product_category_id => @product_category.id) assert_difference Product, :count, -1 do - post 'destroy', :profile => @enterprise.identifier, :id => p.id + post 'destroy', :profile => @enterprise.identifier, :id => product.id assert_response :redirect assert_redirected_to :action => 'index' assert assigns(:product) @@ -136,10 +135,10 @@ class ManageProductsControllerTest < Test::Unit::TestCase end should "fail to destroy product" do - p = @enterprise.products.create!(:name => 'test product', :product_category => @product_category) + product = fast_create(Product, :name => 'test product', :enterprise_id => @enterprise.id, :product_category_id => @product_category.id) Product.any_instance.stubs(:destroy).returns(false) assert_no_difference Product, :count do - post 'destroy', :profile => @enterprise.identifier, :id => p.id + post 'destroy', :profile => @enterprise.identifier, :id => product.id assert_response :redirect assert_redirected_to :action => 'show' assert assigns(:product) @@ -159,20 +158,20 @@ class ManageProductsControllerTest < Test::Unit::TestCase category1 = fast_create(ProductCategory, :name => 'Category 1') category2 = fast_create(ProductCategory, :name => 'Category 2', :parent_id => category1) assert_difference Product, :count do - post 'new', :profile => @enterprise.identifier, :product => { :name => 'test product', :product_category_id => category2.id } + post 'new', :profile => @enterprise.identifier, :product => { :name => 'test product' }, :selected_category_id => category2.id assert_equal category2, assigns(:product).product_category end end should 'filter html from name of product' do category = fast_create(ProductCategory, :name => 'Category 1') - post 'new', :profile => @enterprise.identifier, :product => { :name => "name bold", :product_category_id => category.id } + post 'new', :profile => @enterprise.identifier, :product => { :name => "name bold" }, :selected_category_id => category.id assert_sanitized assigns(:product).name end - should 'filter html with whit list from description of product' do - category = fast_create(ProductCategory, :name => 'Category 1') - post 'new', :profile => @enterprise.identifier, :product => { :name => 'name', :description => "descr bold", :product_category_id => category.id } + should 'filter html with white list from description of product' do + product = fast_create(Product, :enterprise_id => @enterprise.id, :product_category_id => @product_category.id) + post 'edit', :profile => @enterprise.identifier, :id => product.id, :field => 'info', :product => { :name => 'name', :description => "descr bold" } assert_equal "descr bold", assigns(:product).description end @@ -235,7 +234,7 @@ class ManageProductsControllerTest < Test::Unit::TestCase should 'render redirect_via_javascript template after save' do assert_difference Product, :count do - post :new, :profile => @enterprise.identifier, :product => { :name => 'Invalid product', :product_category_id => @product_category.id } + post :new, :profile => @enterprise.identifier, :product => { :name => 'Invalid product' }, :selected_category_id => @product_category.id assert_template 'shared/_redirect_via_javascript' end end @@ -273,25 +272,37 @@ class ManageProductsControllerTest < Test::Unit::TestCase end should 'show product price when showing product if unit was informed' do - prod = @enterprise.products.create!(:name => 'Product test', :price => 50.00, :unit => 'unit', :product_category => @product_category) - get :show, :id => prod.id, :profile => @enterprise.identifier + product = fast_create(Product, :name => 'test product', :price => 50.00, :unit => 'unit', :enterprise_id => @enterprise.id, :product_category_id => @product_category.id) + get :show, :id => product.id, :profile => @enterprise.identifier assert_tag :tag => 'span', :attributes => { :class => 'field-name' }, :content => /Price:/ + assert_tag :tag => 'span', :attributes => { :class => 'field-value' }, :content => '$ 50.00' end should 'show product price when showing product if discount was informed' do - prod = @enterprise.products.create!(:name => 'Product test', :price => 50.00, :discount => 3.50, :unit => 'unit', :product_category => @product_category) - get :show, :id => prod.id, :profile => @enterprise.identifier + product = fast_create(Product, :name => 'test product', :price => 50.00, :unit => 'unit', :discount => 3.50, :enterprise_id => @enterprise.id, :product_category_id => @product_category.id) + get :show, :id => product.id, :profile => @enterprise.identifier assert_tag :tag => 'span', :attributes => { :class => 'field-name' }, :content => /List price:/ + assert_tag :tag => 'span', :attributes => { :class => 'field-value' }, :content => '$ 50.00' assert_tag :tag => 'span', :attributes => { :class => 'field-name' }, :content => /On sale:/ + assert_tag :tag => 'span', :attributes => { :class => 'field-value' }, :content => '$ 46.50' end should 'not show product price when showing product if unit not informed' do - prod = @enterprise.products.create!(:name => 'Product test', :price => 50.00, :product_category => @product_category) - get :show, :id => prod.id, :profile => @enterprise.identifier + product = fast_create(Product, :name => 'test product', :price => 50.00, :enterprise_id => @enterprise.id, :product_category_id => @product_category.id) + get :show, :id => product.id, :profile => @enterprise.identifier - assert_no_tag :tag => 'span', :attributes => { :class => 'product_price' }, :content => /Price:/ + assert_no_tag :tag => 'span', :attributes => { :class => 'field-name' }, :content => /Price:/ + assert_no_tag :tag => 'span', :attributes => { :class => 'field-value' }, :content => '$ 50.00' + end + + should 'display button to add input when product has no input' do + product = fast_create(Product, :name => 'test product', :enterprise_id => @enterprise.id, :product_category_id => @product_category.id) + get :show, :id => product.id, :profile => @enterprise.identifier + + assert_tag :tag => 'div', :attributes => { :id => 'product-inputs'}, + :descendant => {:tag => 'a', :attributes => { :id => 'edit-product-button-ui-inputs' }, :content => 'Add the inputs used by this product'} end end diff --git a/test/unit/consumption_test.rb b/test/unit/consumption_test.rb deleted file mode 100644 index 34b42e0..0000000 --- a/test/unit/consumption_test.rb +++ /dev/null @@ -1,14 +0,0 @@ -require File.dirname(__FILE__) + '/../test_helper' - -class ConsumptionTest < Test::Unit::TestCase - fixtures :consumptions - - should 'escape malformed html tags' do - consumption = Consumption.new - consumption.aditional_specifications = "

> html >< tag" - consumption.valid? - - assert_no_match /[<>]/, consumption.aditional_specifications - end - -end diff --git a/test/unit/input_test.rb b/test/unit/input_test.rb new file mode 100644 index 0000000..a8d2f91 --- /dev/null +++ b/test/unit/input_test.rb @@ -0,0 +1,30 @@ +require File.dirname(__FILE__) + '/../test_helper' + +class InputTest < Test::Unit::TestCase + + should 'require product_category' do + product_category = fast_create(ProductCategory, :name => 'Products') + + input = Input.new + input.valid? + assert input.errors.invalid?(:product_category) + + input.product_category = product_category + input.valid? + assert !input.errors.invalid?(:product_category) + end + + should 'require product' do + product_category = fast_create(ProductCategory, :name => 'Products') + product = fast_create(Product, :name => 'Computer', :product_category_id => product_category.id) + + input = Input.new + input.valid? + assert input.errors.invalid?(:product) + + input.product = product + input.valid? + assert !input.errors.invalid?(:product) + end + +end diff --git a/test/unit/manage_products_helper_test.rb b/test/unit/manage_products_helper_test.rb index 52e0cf1..0e184c2 100644 --- a/test/unit/manage_products_helper_test.rb +++ b/test/unit/manage_products_helper_test.rb @@ -55,7 +55,7 @@ class ManageProductsHelperTest < Test::Unit::TestCase @controller.expects(:profile).returns(mock) category = fast_create(ProductCategory, :name => 'Category 1', :environment_id => @environment.id) product = fast_create(Product, :product_category_id => category.id) - assert_equal '', edit_product_link(product, 'field', 'link to edit') + assert_equal '', edit_product_link_to_remote(product, 'field', 'link to edit') end should 'display link to edit product when user has permission' do @@ -69,7 +69,7 @@ class ManageProductsHelperTest < Test::Unit::TestCase expects(:link_to_remote).with('link to edit', {:update => "product-name", :url => {:controller => 'manage_products', :action => 'edit', :id => product.id, :field => 'name'}, :method => :get}, anything).returns('LINK') - assert_equal 'LINK', edit_product_link(product, 'name', 'link to edit') + assert_equal 'LINK', edit_product_link_to_remote(product, 'name', 'link to edit') end should 'not display link to edit product category when user does not have permission' do @@ -80,7 +80,7 @@ class ManageProductsHelperTest < Test::Unit::TestCase @controller.expects(:profile).returns(mock) category = fast_create(ProductCategory, :name => 'Category 1', :environment_id => @environment.id) product = fast_create(Product, :product_category_id => category.id) - assert_equal '', edit_product_category_link(product) + assert_equal '', edit_product_link('product-category', 'link to edit category', { :action => 'edit_category', :id => product.id }) end should 'display link to edit product category when user has permission' do @@ -92,7 +92,49 @@ class ManageProductsHelperTest < Test::Unit::TestCase category = fast_create(ProductCategory, :name => 'Category 1', :environment_id => @environment.id) product = fast_create(Product, :product_category_id => category.id) - assert_tag_in_string edit_product_category_link(product), {:tag => 'a', :content => 'Change category'} + expects(:link_to).with('link to edit category', { :action => 'edit_category', :id => product.id }, {:id => 'link-edit-product-category'} ).returns('LINK') + + assert_equal 'LINK', edit_product_link('product-category', 'link to edit category', { :action => 'edit_category', :id => product.id }) + end + + should 'not display ui_button to edit product when user does not have permission' do + user = mock + user.expects(:has_permission?).with(anything, anything).returns(false) + @controller = mock + @controller.expects(:user).returns(user).at_least_once + @controller.expects(:profile).returns(mock) + category = fast_create(ProductCategory, :name => 'Category 1', :environment_id => @environment.id) + product = fast_create(Product, :product_category_id => category.id) + assert_equal '', edit_product_ui_button(product, 'field', 'link to edit') + end + + should 'display ui_button_to_remote to edit product when user has permission' do + user = mock + user.expects(:has_permission?).with(anything, anything).returns(true) + @controller = mock + @controller.expects(:user).returns(user).at_least_once + @controller.expects(:profile).returns(mock) + category = fast_create(ProductCategory, :name => 'Category 1', :environment_id => @environment.id) + product = fast_create(Product, :product_category_id => category.id) + + expects(:ui_button_to_remote).with('link to edit', {:update => "product-info", :url => {:controller => 'manage_products', :action => 'edit', :id => product.id, :field => 'info'}, :complete => "$('edit-product-button-ui-info').hide()", :method => :get}, :id => 'edit-product-remote-button-ui-info').returns('LINK') + + assert_equal 'LINK', edit_product_ui_button_to_remote(product, 'info', 'link to edit') + end + + + should 'display ui_button to edit product when user has permission' do + user = mock + user.expects(:has_permission?).with(anything, anything).returns(true) + @controller = mock + @controller.expects(:user).returns(user).at_least_once + @controller.expects(:profile).returns(mock) + category = fast_create(ProductCategory, :name => 'Category 1', :environment_id => @environment.id) + product = fast_create(Product, :product_category_id => category.id) + + expects(:ui_button).with('link to edit', { :action => 'add_input', :id => product.id }, {:id => 'edit-product-button-ui-info'}).returns('LINK') + + assert_equal 'LINK', edit_product_ui_button('info', 'link to edit', {:action => 'add_input', :id => product.id}) end protected diff --git a/test/unit/product_category_test.rb b/test/unit/product_category_test.rb index 7dd1ad9..9461ff1 100644 --- a/test/unit/product_category_test.rb +++ b/test/unit/product_category_test.rb @@ -17,13 +17,6 @@ class ProductCategoryTest < Test::Unit::TestCase assert_equivalent [p1], c1.all_products end - should 'have consumers' do - c = ProductCategory.create!(:name => 'base_cat', :environment => Environment.default) - person = create_user('test_user').person - c.consumers << person - assert_includes c.consumers, person - end - should 'return top level product categories for environment when no parent product category specified' do env1 = Environment.create!(:name => 'test env 1') env2 = Environment.create!(:name => 'test env 2') diff --git a/test/unit/product_test.rb b/test/unit/product_test.rb index 56d9c9b..5dfdbb6 100644 --- a/test/unit/product_test.rb +++ b/test/unit/product_test.rb @@ -241,7 +241,7 @@ class ProductTest < Test::Unit::TestCase should 'not save without category' do product = Product.new(:name => 'A product without category') product.valid? - assert product.errors.invalid?(:product_category) + assert product.errors.invalid?(:product_category_id) end should 'format values to float with 2 decimals' do @@ -263,4 +263,35 @@ class ProductTest < Test::Unit::TestCase product = Product.new assert_equal '/images/icons-app/product-default-pic-thumb.png', product.default_image end + + should 'have inputs' do + product = Product.new + assert_respond_to product, :inputs + end + + should 'return empty array if has no input' do + product = Product.new + assert product.inputs.empty? + end + + should 'return product inputs' do + ent = fast_create(Enterprise) + product = fast_create(Product, :enterprise_id => ent.id) + input = fast_create(Input, :product_id => product.id, :product_category_id => @product_category.id) + + assert_equal [input], product.inputs + end + + should 'destroy inputs when product is removed' do + ent = fast_create(Enterprise) + product = fast_create(Product, :enterprise_id => ent.id) + input = fast_create(Input, :product_id => product.id, :product_category_id => @product_category.id) + + services_category = fast_create(ProductCategory, :name => 'Services') + input2 = fast_create(Input, :product_id => product.id, :product_category_id => services_category.id) + + assert_difference Input, :count, -2 do + product.destroy + end + end end -- libgit2 0.21.2