From 6540bb8efc84e62e32e7a2b5290274d3b1fdd5a0 Mon Sep 17 00:00:00 2001 From: Rob H Date: Sun, 7 Sep 2014 19:51:14 +1000 Subject: [PATCH] Adding select field for enterprise type to index when super admin --- .../admin/enterprises_controller.rb | 14 ++++-- app/views/admin/enterprises/index.html.haml | 8 +-- .../admin/enterprises_controller_spec.rb | 37 +++++++++++++- spec/features/admin/enterprises_spec.rb | 50 ++++++++++++------- 4 files changed, 81 insertions(+), 28 deletions(-) diff --git a/app/controllers/admin/enterprises_controller.rb b/app/controllers/admin/enterprises_controller.rb index 594ccc751c..ceeb26abfc 100644 --- a/app/controllers/admin/enterprises_controller.rb +++ b/app/controllers/admin/enterprises_controller.rb @@ -4,7 +4,8 @@ module Admin before_filter :load_countries, :except => :index before_filter :load_methods_and_fees, :only => [:new, :edit, :update, :create] create.after :grant_management - before_filter :override_type, only: :update + before_filter :check_type, only: :update + before_filter :check_bulk_type, only: :bulk_update helper 'spree/products' include OrderCyclesHelper @@ -68,8 +69,15 @@ module Admin @enterprise_fees = EnterpriseFee.managed_by(spree_current_user).for_enterprise(@enterprise).order(:fee_type, :name).all end - def override_type - # TODO this should be done using CanCan, but our current version does not allow this level of fine grained control + def check_bulk_type + unless spree_current_user.admin? + params[:enterprise_set][:collection_attributes].each do |i, enterprise_params| + enterprise_params.delete :type + end + end + end + + def check_type params[:enterprise].delete :type unless spree_current_user.admin? end diff --git a/app/views/admin/enterprises/index.html.haml b/app/views/admin/enterprises/index.html.haml index 7ab6ea4d0e..30dfc7a8f2 100644 --- a/app/views/admin/enterprises/index.html.haml +++ b/app/views/admin/enterprises/index.html.haml @@ -10,17 +10,17 @@ = form_for @enterprise_set, :url => main_app.bulk_update_admin_enterprises_path do |f| %table#listing_enterprises.index %colgroup - %col{style: "width: 20%;"}/ + %col{style: "width: 25%;"}/ %col{style: "width: 10%;"}/ %col{style: "width: 5%;"}/ - %col/ + %col{style: "width: 10%;"}/ %col{style: "width: 20%;"}/ %thead %tr{"data-hook" => "enterprises_header"} %th Name %th Role %th Visible? - %th Description + %th Type %th %tbody = f.fields_for :collection do |enterprise_form| @@ -37,7 +37,7 @@ - else %h1.icon-exclamation-sign.with-tip{"data-powertip" => "This enterprise does not have any roles", style: "text-align: center;color: #DA5354"} %td= enterprise_form.check_box :visible - %td= enterprise.description + %td= enterprise_form.select :type, Enterprise::TYPES, {}, class: 'select2 fullwidth' %td{"data-hook" => "admin_users_index_row_actions"} = render 'actions', enterprise: enterprise - if @enterprises.empty? diff --git a/spec/controllers/admin/enterprises_controller_spec.rb b/spec/controllers/admin/enterprises_controller_spec.rb index e52f09a5a4..14faf26287 100644 --- a/spec/controllers/admin/enterprises_controller_spec.rb +++ b/spec/controllers/admin/enterprises_controller_spec.rb @@ -42,7 +42,6 @@ module Admin context "as manager" do it "does not allow 'type' to be changed" do - # TODO should be implemented and tested using cancan abilities, but can't do this using our current version profile_enterprise.enterprise_roles.build(user: user).save controller.stub spree_current_user: user enterprise_params = { id: profile_enterprise.id, enterprise: { type: 'full' } } @@ -55,7 +54,6 @@ module Admin context "as super admin" do it "allows 'type' to be changed" do - # TODO should be implemented and tested using cancan abilities, but can't do this using our current version controller.stub spree_current_user: admin_user enterprise_params = { id: profile_enterprise.id, enterprise: { type: 'full' } } @@ -65,5 +63,40 @@ module Admin end end end + + describe "bulk updating enterprises" do + let(:profile_enterprise1) { create(:enterprise, type: 'profile') } + let(:profile_enterprise2) { create(:enterprise, type: 'profile') } + + context "as manager" do + it "does not allow 'type' to be changed" do + profile_enterprise1.enterprise_roles.build(user: user).save + profile_enterprise2.enterprise_roles.build(user: user).save + controller.stub spree_current_user: user + bulk_enterprise_params = { enterprise_set: { collection_attributes: { '0' => { id: profile_enterprise1.id, type: 'full' }, '1' => { id: profile_enterprise2.id, type: 'full' } } } } + + spree_put :bulk_update, bulk_enterprise_params + profile_enterprise1.reload + profile_enterprise2.reload + expect(profile_enterprise1.type).to eq 'profile' + expect(profile_enterprise2.type).to eq 'profile' + end + end + + context "as super admin" do + it "allows 'type' to be changed" do + profile_enterprise1.enterprise_roles.build(user: user).save + profile_enterprise2.enterprise_roles.build(user: user).save + controller.stub spree_current_user: admin_user + bulk_enterprise_params = { enterprise_set: { collection_attributes: { '0' => { id: profile_enterprise1.id, type: 'full' }, '1' => { id: profile_enterprise2.id, type: 'full' } } } } + + spree_put :bulk_update, bulk_enterprise_params + profile_enterprise1.reload + profile_enterprise2.reload + expect(profile_enterprise1.type).to eq 'full' + expect(profile_enterprise2.type).to eq 'full' + end + end + end end end diff --git a/spec/features/admin/enterprises_spec.rb b/spec/features/admin/enterprises_spec.rb index 08bae3aec5..00c15160f0 100644 --- a/spec/features/admin/enterprises_spec.rb +++ b/spec/features/admin/enterprises_spec.rb @@ -15,39 +15,43 @@ feature %q{ click_link 'Enterprises' within("tr.enterprise-#{s.id}") do - page.should have_content s.name - page.should have_content "Edit Profile" - page.should have_content "Delete" - page.should_not have_content "Payment Methods" - page.should_not have_content "Shipping Methods" - page.should have_content "Enterprise Fees" + expect(page).to have_content s.name + expect(page).to have_select "enterprise_set_collection_attributes_1_type" + expect(page).to have_content "Edit Profile" + expect(page).to have_content "Delete" + expect(page).to_not have_content "Payment Methods" + expect(page).to_not have_content "Shipping Methods" + expect(page).to have_content "Enterprise Fees" end within("tr.enterprise-#{d.id}") do - page.should have_content d.name - page.should have_content "Edit Profile" - page.should have_content "Delete" - page.should have_content "Payment Methods" - page.should have_content "Shipping Methods" - page.should have_content "Enterprise Fees" + expect(page).to have_content d.name + expect(page).to have_select "enterprise_set_collection_attributes_0_type" + expect(page).to have_content "Edit Profile" + expect(page).to have_content "Delete" + expect(page).to have_content "Payment Methods" + expect(page).to have_content "Shipping Methods" + expect(page).to have_content "Enterprise Fees" end end scenario "editing enterprises in bulk" do s = create(:supplier_enterprise) - d = create(:distributor_enterprise) + d = create(:distributor_enterprise, type: 'profile') login_to_admin_section click_link 'Enterprises' within("tr.enterprise-#{d.id}") do - page.should have_checked_field "enterprise_set_collection_attributes_0_visible" + expect(page).to have_checked_field "enterprise_set_collection_attributes_0_visible" uncheck "enterprise_set_collection_attributes_0_visible" + select 'full', from: "enterprise_set_collection_attributes_0_type" end click_button "Update" flash_message.should == 'Enterprises updated successfully' distributor = Enterprise.find(d.id) - distributor.visible.should == false + expect(distributor.visible).to eq false + expect(distributor.type).to eq 'full' end scenario "viewing an enterprise" do @@ -259,10 +263,18 @@ feature %q{ click_link "Enterprises" - page.should have_content supplier1.name - page.should have_content distributor1.name - page.should_not have_content supplier2.name - page.should_not have_content distributor2.name + within("tr.enterprise-#{supplier1.id}") do + expect(page).to have_content supplier1.name + expect(page).to have_select "enterprise_set_collection_attributes_1_type" + end + + within("tr.enterprise-#{distributor1.id}") do + expect(page).to have_content distributor1.name + expect(page).to have_select "enterprise_set_collection_attributes_0_type" + end + + expect(page).to_not have_content "supplier2.name" + expect(page).to_not have_content "distributor2.name" end scenario "creating an enterprise" do