From 65d13e049072f5f88aadd9f98a3e4fdcc65e7693 Mon Sep 17 00:00:00 2001 From: Rob Harrington Date: Thu, 30 Oct 2014 12:29:14 +1100 Subject: [PATCH] Show error messages on enterprise index page --- .../admin/enterprises_controller.rb | 1 + app/models/model_set.rb | 5 +- app/views/admin/enterprises/index.html.haml | 2 + spec/features/admin/enterprises_spec.rb | 77 ++++++++++++++----- 4 files changed, 63 insertions(+), 22 deletions(-) diff --git a/app/controllers/admin/enterprises_controller.rb b/app/controllers/admin/enterprises_controller.rb index b3fad9dc5c..b8dd9b9bd5 100644 --- a/app/controllers/admin/enterprises_controller.rb +++ b/app/controllers/admin/enterprises_controller.rb @@ -47,6 +47,7 @@ module Admin flash[:success] = 'Enterprises updated successfully' redirect_to main_app.admin_enterprises_path else + flash[:error] = 'Update failed' render :index end end diff --git a/app/models/model_set.rb b/app/models/model_set.rb index 0452b0bafb..d4760884b5 100644 --- a/app/models/model_set.rb +++ b/app/models/model_set.rb @@ -26,7 +26,10 @@ class ModelSet end def errors - @collection.map { |ef| ef.errors.full_messages }.flatten + errors = ActiveModel::Errors.new self + full_messages = @collection.map { |ef| ef.errors.full_messages }.flatten + full_messages.each { |fm| errors.add(:base, fm) } + errors end def save diff --git a/app/views/admin/enterprises/index.html.haml b/app/views/admin/enterprises/index.html.haml index ee1bdde996..64b8423baa 100644 --- a/app/views/admin/enterprises/index.html.haml +++ b/app/views/admin/enterprises/index.html.haml @@ -8,6 +8,8 @@ = render 'admin/shared/enterprises_sub_menu' += render :partial => 'spree/shared/error_messages', :locals => { :target => @enterprise_set } + = form_for @enterprise_set, url: main_app.bulk_update_admin_enterprises_path do |f| %table#listing_enterprises.index %colgroup diff --git a/spec/features/admin/enterprises_spec.rb b/spec/features/admin/enterprises_spec.rb index 06b1fd31a6..ae93ac19ea 100644 --- a/spec/features/admin/enterprises_spec.rb +++ b/spec/features/admin/enterprises_spec.rb @@ -35,28 +35,63 @@ feature %q{ end end - scenario "editing enterprises in bulk" do - s = create(:supplier_enterprise) - d = create(:distributor_enterprise, sells: 'none') - d_manager = create_enterprise_user - d_manager.enterprise_roles.build(enterprise: d).save - expect(d.owner).to_not eq d_manager + context "editing enterprises in bulk" do + let!(:s){ create(:supplier_enterprise) } + let!(:d){ create(:distributor_enterprise, sells: 'none') } + let!(:d_manager) { create_enterprise_user(enterprise_limit: 1) } - login_to_admin_section - click_link 'Enterprises' - - within("tr.enterprise-#{d.id}") do - expect(page).to have_checked_field "enterprise_set_collection_attributes_0_visible" - uncheck "enterprise_set_collection_attributes_0_visible" - select 'any', from: "enterprise_set_collection_attributes_0_sells" - select d_manager.email, from: 'enterprise_set_collection_attributes_0_owner_id' + before do + d_manager.enterprise_roles.build(enterprise: d).save + expect(d.owner).to_not eq d_manager + end + + context "without violating rules" do + before do + login_to_admin_section + click_link 'Enterprises' + end + + it "updates the enterprises" do + within("tr.enterprise-#{d.id}") do + expect(page).to have_checked_field "enterprise_set_collection_attributes_0_visible" + uncheck "enterprise_set_collection_attributes_0_visible" + select 'any', from: "enterprise_set_collection_attributes_0_sells" + select d_manager.email, from: 'enterprise_set_collection_attributes_0_owner_id' + end + click_button "Update" + flash_message.should == 'Enterprises updated successfully' + distributor = Enterprise.find(d.id) + expect(distributor.visible).to eq false + expect(distributor.sells).to eq 'any' + expect(distributor.owner).to eq d_manager + end + end + + context "with data that violates rules" do + let!(:second_distributor) { create(:distributor_enterprise, sells: 'none') } + + before do + d_manager.enterprise_roles.build(enterprise: second_distributor).save + expect(d.owner).to_not eq d_manager + + login_to_admin_section + click_link 'Enterprises' + end + + it "does not update the enterprises and displays errors" do + within("tr.enterprise-#{d.id}") do + select d_manager.email, from: 'enterprise_set_collection_attributes_0_owner_id' + end + within("tr.enterprise-#{second_distributor.id}") do + select d_manager.email, from: 'enterprise_set_collection_attributes_1_owner_id' + end + click_button "Update" + flash_message.should == 'Update failed' + expect(page).to have_content "#{d_manager.email} is not permitted to own any more enterprises (limit is 1)." + second_distributor.reload + expect(second_distributor.owner).to_not eq d_manager + end end - click_button "Update" - flash_message.should == 'Enterprises updated successfully' - distributor = Enterprise.find(d.id) - expect(distributor.visible).to eq false - expect(distributor.sells).to eq 'any' - expect(distributor.owner).to eq d_manager end scenario "viewing an enterprise" do @@ -350,7 +385,7 @@ feature %q{ # Then it should show me an error expect(page).to_not have_content 'Enterprise "zzz" has been successfully created!' - expect(page).to have_content "You are not permitted to own own any more enterprises (limit is 1)." + expect(page).to have_content "#{enterprise_user.email} is not permitted to own any more enterprises (limit is 1)." end end end