diff --git a/app/assets/javascripts/admin/bulk_product_update.js b/app/assets/javascripts/admin/bulk_product_update.js new file mode 100644 index 0000000000..64c9992486 --- /dev/null +++ b/app/assets/javascripts/admin/bulk_product_update.js @@ -0,0 +1,10 @@ +function AdminProductsBulkCtrl($scope, $http) { + $scope.refreshData = function(){ + $http({ method: 'GET', url:'/admin/products/bulk_index.json' }).success(function(data) { + $scope.products = data; + }); + } + $scope.refreshData(); +} + +var productsApp = angular.module('bulk_product_update', []) \ No newline at end of file diff --git a/app/controllers/spree/admin/products_controller_decorator.rb b/app/controllers/spree/admin/products_controller_decorator.rb new file mode 100644 index 0000000000..6df7ac1914 --- /dev/null +++ b/app/controllers/spree/admin/products_controller_decorator.rb @@ -0,0 +1,26 @@ +Spree::Admin::ProductsController.class_eval do + before_filter :load_product_set, :only => :bulk_index + + alias_method :location_after_save_original, :location_after_save + + def bulk_index + respond_to do |format| + format.html + format.json + end + end + + protected + def location_after_save + if URI(request.referer).path == '/admin/products/bulk_index' + bulk_index_admin_products_url + else + location_after_save_original + end + end + + private + def load_product_set + @product_set = Spree::ProductSet.new :collection => collection + end +end \ No newline at end of file diff --git a/app/models/spree/product_set.rb b/app/models/spree/product_set.rb new file mode 100644 index 0000000000..81041ffb66 --- /dev/null +++ b/app/models/spree/product_set.rb @@ -0,0 +1,7 @@ +class Spree::ProductSet < ModelSet + def initialize(attributes={}) + super(Spree::Product, Spree::Product.all, + proc { |attrs| attrs[:product_id].blank? }, + attributes) + end +end \ No newline at end of file diff --git a/app/overrides/add_bulk_index_tab_to_products_admin_sub_menu.rb b/app/overrides/add_bulk_index_tab_to_products_admin_sub_menu.rb new file mode 100644 index 0000000000..e6b4f0a3d6 --- /dev/null +++ b/app/overrides/add_bulk_index_tab_to_products_admin_sub_menu.rb @@ -0,0 +1,4 @@ +Deface::Override.new(:virtual_path => "spree/admin/shared/_product_sub_menu", + :name => "add_bulk_index_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 diff --git a/app/views/spree/admin/products/bulk_index.html.haml b/app/views/spree/admin/products/bulk_index.html.haml new file mode 100644 index 0000000000..7dfe90a618 --- /dev/null +++ b/app/views/spree/admin/products/bulk_index.html.haml @@ -0,0 +1,22 @@ +- content_for :page_title do + = "Bulk Edit Products" + +- content_for :page_actions do + %div{ :class => "toolbar", 'data-hook' => "toolbar" } + %ul{ :class => "actions header-action-links inline-menu" } + %li#new_product_link + = button_link_to t(:new_product), new_object_url, { :remote => true, :icon => 'icon-plus', :id => 'admin_new_product' } + += render :partial => 'spree/admin/shared/product_sub_menu' + +%div#new_product(data-hook) + +%div{ 'ng-app' => 'bulk_product_update', 'ng-controller' => 'AdminProductsBulkCtrl' } + %table.index#listing_products + %thead + %tr + %th Name + %tbody{ 'ng-repeat' => 'product in products | filter:query' } + %tr + %td + %input{ 'ng-model' => "product.name", :type => 'text' } \ No newline at end of file diff --git a/app/views/spree/admin/products/bulk_index.rep b/app/views/spree/admin/products/bulk_index.rep new file mode 100644 index 0000000000..4a68e7f379 --- /dev/null +++ b/app/views/spree/admin/products/bulk_index.rep @@ -0,0 +1,4 @@ +r.list_of :products, @collection do + r.element :id + r.element :name +end \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 11ab6cb5a3..004dd7aa87 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -32,6 +32,12 @@ Spree::Core::Engine.routes.prepend do 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] + namespace :admin do + resources :products do + get :bulk_index, :on => :collection, :as => :bulk_index + end + end + resources :orders do get :select_distributor, :on => :member get :deselect_distributor, :on => :collection diff --git a/spec/features/admin/bulk_product_update_spec.rb b/spec/features/admin/bulk_product_update_spec.rb new file mode 100644 index 0000000000..47432514d4 --- /dev/null +++ b/spec/features/admin/bulk_product_update_spec.rb @@ -0,0 +1,67 @@ +require 'spec_helper' + +feature %q{ + As an Administrator + I want to be able to manage products in bulk +} , js: true do + + include AuthenticationWorkflow + include WebHelper + + before :all do + @default_wait_time = Capybara.default_wait_time + Capybara.default_wait_time = 5 + end + + after :all do + Capybara.default_wait_time = @default_wait_time + end + + scenario "listing products" do + p = FactoryGirl.create(:product) + + login_to_admin_section + click_link 'Products' + click_link 'Bulk Product Edit' + + page.first('input').value.should == p.name + end + + scenario "create a new product" do + s = FactoryGirl.create(:supplier_enterprise) + d = FactoryGirl.create(:distributor_enterprise) + + login_to_admin_section + click_link 'Products' + click_link 'Bulk Product Edit' + + click_link 'New Product' + + page.should have_content 'NEW PRODUCT' + + fill_in 'product_name', :with => 'Big Bag Of Apples' + select(s.name, :from => 'product_supplier_id') + choose('product_group_buy_0') + fill_in 'product_price', :with => '10.00' + fill_in 'product_available_on', :with => Date.today.strftime("%Y/%m/%d") + check('product_product_distributions_attributes_0__destroy') + click_button 'Create' + + URI.parse(current_url).path.should == '/admin/products/bulk_index' + flash_message.should == 'Product "Big Bag Of Apples" has been successfully created!' + page.should have_content 'Big Bag Of Apples' + end + + scenario "updating a product" do + p = FactoryGirl.create(:product) + s1 = FactoryGirl.create(:supplier_enterprise) + s2 = FactoryGirl.create(:supplier_enterprise) + p.supplier = s1 + + login_to_admin_section + click_link 'Products' + click_link 'Bulk Product Edit' + + page.first(:css, "td select", :text => s1.name).select(s2.name) + end +end \ No newline at end of file