From 4ae5085192cd105c514f03f05d2a1f714fa66c9c Mon Sep 17 00:00:00 2001 From: Rob H Date: Thu, 20 Jun 2013 22:40:29 +0530 Subject: [PATCH] BPUR: Remove unnecessary collections of products on bulk edit page --- .../admin/products_controller_decorator.rb | 13 ++--- app/models/spree/product_set.rb | 14 ++--- ...lk_edit_tab_to_products_admin_sub_menu.rb} | 4 +- ...lk_index.html.haml => bulk_edit.html.haml} | 0 config/routes.rb | 1 + .../admin/bulk_product_update_spec.rb | 52 +++++++++---------- 6 files changed, 42 insertions(+), 42 deletions(-) rename app/overrides/{add_bulk_index_tab_to_products_admin_sub_menu.rb => add_bulk_edit_tab_to_products_admin_sub_menu.rb} (65%) rename app/views/spree/admin/products/{bulk_index.html.haml => bulk_edit.html.haml} (100%) diff --git a/app/controllers/spree/admin/products_controller_decorator.rb b/app/controllers/spree/admin/products_controller_decorator.rb index 3f5e092773..089199711e 100644 --- a/app/controllers/spree/admin/products_controller_decorator.rb +++ b/app/controllers/spree/admin/products_controller_decorator.rb @@ -8,17 +8,14 @@ Spree::Admin::ProductsController.class_eval do #respond_override :clone => { :json => {:success => lambda { redirect_to bulk_index_admin_products_url+"?q[id_eq]=#{@new.id}" } } } def bulk_index - respond_to do |format| - format.html - format.json - end + respond_to :json end def bulk_update collection_hash = Hash[params[:_json].each_with_index.map { |p,i| [i,p] }] - product_set = Spree::ProductSet.new() + product_set = Spree::ProductSet.new({:collection_attributes => collection_hash}) - if product_set.save(collection_hash) + if product_set.save redirect_to bulk_index_admin_products_url :format => :json else render :nothing => true @@ -27,8 +24,8 @@ Spree::Admin::ProductsController.class_eval do protected def location_after_save - if URI(request.referer).path == '/admin/products/bulk_index' - bulk_index_admin_products_url + if URI(request.referer).path == '/admin/products/bulk_edit' + bulk_edit_admin_products_url else location_after_save_original end diff --git a/app/models/spree/product_set.rb b/app/models/spree/product_set.rb index b1bb50870e..ce316641bb 100644 --- a/app/models/spree/product_set.rb +++ b/app/models/spree/product_set.rb @@ -1,9 +1,6 @@ class Spree::ProductSet < ModelSet def initialize(attributes={}) - product_ids = attributes[:collection_attributes].each_value.map{ |p| p[:id] } if attributes[:collection_attributes] - super(Spree::Product, (product_ids ? Spree::Product.where( :id => product_ids ) : Spree::Product.all ), - proc { |attrs| attrs[:product_id].blank? }, - attributes) + super(Spree::Product, [], proc { |attrs| attrs[:product_id].blank? }, attributes) end # A separate method of updating products was required due to an issue with the way Rails' assign_attributes and updates_attributes behave when delegated attributes of a nested @@ -29,8 +26,13 @@ class Spree::ProductSet < ModelSet end end - def save(collection_hash) - collection_hash.each_value.all? do |product_attributes| + def collection_attributes=(attributes) + @collection = Spree::Product.where( :id => attributes.each_value.map{ |p| p[:id] } ) + @collection_hash = attributes + end + + def save + @collection_hash.each_value.all? do |product_attributes| update_attributes(product_attributes) end end diff --git a/app/overrides/add_bulk_index_tab_to_products_admin_sub_menu.rb b/app/overrides/add_bulk_edit_tab_to_products_admin_sub_menu.rb similarity index 65% rename from app/overrides/add_bulk_index_tab_to_products_admin_sub_menu.rb rename to app/overrides/add_bulk_edit_tab_to_products_admin_sub_menu.rb index e6b4f0a3d6..27980a2262 100644 --- a/app/overrides/add_bulk_index_tab_to_products_admin_sub_menu.rb +++ b/app/overrides/add_bulk_edit_tab_to_products_admin_sub_menu.rb @@ -1,4 +1,4 @@ Deface::Override.new(:virtual_path => "spree/admin/shared/_product_sub_menu", - :name => "add_bulk_index_tab_to_products_admin_sub_menu", + :name => "add_bulk_edit_tab_to_products_admin_sub_menu", :insert_bottom => "[data-hook='admin_product_sub_tabs']", - :text => "<%= tab('Bulk Product Edit', :url => bulk_index_admin_products_path) %>") \ No newline at end of file + :text => "<%= tab('Bulk Product Edit', :url => bulk_edit_admin_products_path) %>") \ No newline at end of file diff --git a/app/views/spree/admin/products/bulk_index.html.haml b/app/views/spree/admin/products/bulk_edit.html.haml similarity index 100% rename from app/views/spree/admin/products/bulk_index.html.haml rename to app/views/spree/admin/products/bulk_edit.html.haml diff --git a/config/routes.rb b/config/routes.rb index bf8690725f..9445e01489 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -31,6 +31,7 @@ Spree::Core::Engine.routes.prepend do match '/admin/reports/bulk_coop' => 'admin/reports#bulk_coop', :as => "bulk_coop_admin_reports", :via => [:get, :post] match '/admin/reports/payments' => 'admin/reports#payments', :as => "payments_admin_reports", :via => [:get, :post] match '/admin/reports/order_cycles' => 'admin/reports#order_cycles', :as => "order_cycles_admin_reports", :via => [:get, :post] + match '/admin/products/bulk_edit' => 'admin/products#bulk_edit', :as => "bulk_edit_admin_products" namespace :admin do resources :products do diff --git a/spec/features/admin/bulk_product_update_spec.rb b/spec/features/admin/bulk_product_update_spec.rb index 0898f57a07..032b18e79c 100644 --- a/spec/features/admin/bulk_product_update_spec.rb +++ b/spec/features/admin/bulk_product_update_spec.rb @@ -25,7 +25,7 @@ feature %q{ p1 = FactoryGirl.create(:product) p2 = FactoryGirl.create(:product) - visit '/admin/products/bulk_index' + visit '/admin/products/bulk_edit' page.should have_field "product_name", with: p1.name page.should have_field "product_name", with: p2.name @@ -38,7 +38,7 @@ feature %q{ p1 = FactoryGirl.create(:product, supplier: s2) p2 = FactoryGirl.create(:product, supplier: s3) - visit '/admin/products/bulk_index' + visit '/admin/products/bulk_edit' page.should have_select "supplier_id", with_options: [s1.name,s2.name,s3.name], selected: s2.name page.should have_select "supplier_id", with_options: [s1.name,s2.name,s3.name], selected: s3.name @@ -48,7 +48,7 @@ feature %q{ p1 = FactoryGirl.create(:product, available_on: Date.today) p2 = FactoryGirl.create(:product, available_on: Date.today-1) - visit '/admin/products/bulk_index' + visit '/admin/products/bulk_edit' page.should have_field "available_on", with: p1.available_on.strftime("%F %T") page.should have_field "available_on", with: p2.available_on.strftime("%F %T") @@ -62,7 +62,7 @@ feature %q{ p1.save! p2.save! - visit '/admin/products/bulk_index' + visit '/admin/products/bulk_edit' page.should have_field "price", with: "22.0" page.should have_field "price", with: "44.0" @@ -76,7 +76,7 @@ feature %q{ p1.save! p2.save! - visit '/admin/products/bulk_index' + visit '/admin/products/bulk_edit' page.should_not have_selector "span[name='on_hand']", text: "0" page.should have_field "on_hand", with: "15" @@ -92,7 +92,7 @@ feature %q{ p1.save! p2.save! - visit '/admin/products/bulk_index' + visit '/admin/products/bulk_edit' page.should_not have_field "on_hand", with: "15" page.should have_selector "span[name='on_hand']", text: "4" @@ -109,7 +109,7 @@ feature %q{ v1 = FactoryGirl.create(:variant) v2 = FactoryGirl.create(:variant) - visit '/admin/products/bulk_index' + visit '/admin/products/bulk_edit' page.should have_selector "a.view-variants" first("a.view-variants").click @@ -124,7 +124,7 @@ feature %q{ v1 = FactoryGirl.create(:variant, product: p1, is_master: false, on_hand: 15) v2 = FactoryGirl.create(:variant, product: p1, is_master: false, on_hand: 6) - visit '/admin/products/bulk_index' + visit '/admin/products/bulk_edit' page.should have_selector "span[name='on_hand']", text: "21" page.should have_field "variant_on_hand", with: "15" @@ -137,7 +137,7 @@ feature %q{ v1 = FactoryGirl.create(:variant, product: p1, is_master: false, price: 12.75) v2 = FactoryGirl.create(:variant, product: p1, is_master: false, price: 2.50) - visit '/admin/products/bulk_index' + visit '/admin/products/bulk_edit' page.should have_field "price", with: "2.0" page.should have_field "variant_price", with: "12.75" @@ -151,7 +151,7 @@ feature %q{ login_to_admin_section - visit '/admin/products/bulk_index' + visit '/admin/products/bulk_edit' click_link 'New Product' @@ -165,7 +165,7 @@ feature %q{ check('product_product_distributions_attributes_0__destroy') click_button 'Create' - URI.parse(current_url).path.should == '/admin/products/bulk_index' + URI.parse(current_url).path.should == '/admin/products/bulk_edit' flash_message.should == 'Product "Big Bag Of Apples" has been successfully created!' page.should have_field "product_name", with: 'Big Bag Of Apples' end @@ -180,7 +180,7 @@ feature %q{ login_to_admin_section - visit '/admin/products/bulk_index' + visit '/admin/products/bulk_edit' page.should have_field "product_name", with: p.name page.should have_select "supplier_id", selected: s1.name @@ -197,7 +197,7 @@ feature %q{ click_button 'Update' page.find("span#update-status-message").should have_content "Update complete" - visit '/admin/products/bulk_index' + visit '/admin/products/bulk_edit' page.should have_field "product_name", with: "Big Bag Of Potatoes" page.should have_select "supplier_id", selected: s2.name @@ -214,7 +214,7 @@ feature %q{ login_to_admin_section - visit '/admin/products/bulk_index' + visit '/admin/products/bulk_edit' page.should have_selector "a.view-variants" first("a.view-variants").click @@ -230,7 +230,7 @@ feature %q{ click_button 'Update' page.find("span#update-status-message").should have_content "Update complete" - visit '/admin/products/bulk_index' + visit '/admin/products/bulk_edit' page.should have_selector "a.view-variants" first("a.view-variants").click @@ -244,7 +244,7 @@ feature %q{ login_to_admin_section - visit '/admin/products/bulk_index' + visit '/admin/products/bulk_edit' page.should have_selector "a.view-variants" first("a.view-variants").click @@ -255,7 +255,7 @@ feature %q{ click_button 'Update' page.find("span#update-status-message").should have_content "Update complete" - visit '/admin/products/bulk_index' + visit '/admin/products/bulk_edit' page.should have_selector "a.view-variants" first("a.view-variants").click @@ -266,7 +266,7 @@ feature %q{ p = FactoryGirl.create(:product, name: 'original name') login_to_admin_section - visit '/admin/products/bulk_index' + visit '/admin/products/bulk_edit' page.should have_field "product_name", with: "original name" @@ -294,7 +294,7 @@ feature %q{ p3 = FactoryGirl.create(:product) login_to_admin_section - visit '/admin/products/bulk_index' + visit '/admin/products/bulk_edit' page.should have_selector "a.delete-product", :count => 3 @@ -305,7 +305,7 @@ feature %q{ page.should have_selector "a.delete-product", :count => 2 #page.should have_selector "div.flash.notice", text: "Product has been deleted." - visit '/admin/products/bulk_index' + visit '/admin/products/bulk_edit' page.should have_selector "a.delete-product", :count => 2 end @@ -316,7 +316,7 @@ feature %q{ v3 = FactoryGirl.create(:variant) login_to_admin_section - visit '/admin/products/bulk_index' + visit '/admin/products/bulk_edit' page.should have_selector "a.view-variants" all("a.view-variants").each{ |e| e.click } @@ -328,7 +328,7 @@ feature %q{ page.should have_selector "a.delete-variant", :count => 2 #page.should have_selector "div.flash.notice", text: "Product has been deleted." - visit '/admin/products/bulk_index' + visit '/admin/products/bulk_edit' page.should have_selector "a.view-variants" all("a.view-variants").select{ |e| e.visible? }.each{ |e| e.click } @@ -343,7 +343,7 @@ feature %q{ p3 = FactoryGirl.create(:product) login_to_admin_section - visit '/admin/products/bulk_index' + visit '/admin/products/bulk_edit' page.should have_selector "a.edit-product", :count => 3 @@ -358,7 +358,7 @@ feature %q{ v3 = FactoryGirl.create(:variant) login_to_admin_section - visit '/admin/products/bulk_index' + visit '/admin/products/bulk_edit' page.should have_selector "a.view-variants" first("a.view-variants").click @@ -377,7 +377,7 @@ feature %q{ p3 = FactoryGirl.create(:product, :name => "P3") login_to_admin_section - visit '/admin/products/bulk_index' + visit '/admin/products/bulk_edit' page.should have_selector "a.clone-product", :count => 3 @@ -386,7 +386,7 @@ feature %q{ page.should have_selector "a.clone-product", :count => 4 page.should have_field "product_name", with: "COPY OF #{p1.name}" - visit '/admin/products/bulk_index' + visit '/admin/products/bulk_edit' page.should have_selector "a.clone-product", :count => 4 page.should have_field "product_name", with: "COPY OF #{p1.name}"