diff options
3 files changed, 38 insertions, 16 deletions
diff --git a/app/controllers/admin_public_body_category_controller.rb b/app/controllers/admin_public_body_category_controller.rb index b0dde3219..9380aa21b 100644 --- a/app/controllers/admin_public_body_category_controller.rb +++ b/app/controllers/admin_public_body_category_controller.rb @@ -59,10 +59,9 @@ class AdminPublicBodyCategoryController < AdminController error = nil ActiveRecord::Base.transaction do params[:categories].each_with_index do |category_id, index| - link = PublicBodyCategoryLink.find(:first, - :conditions => ['public_body_category_id = ? - AND public_body_heading_id = ?', - category_id, params[:heading_id]]) + conditions = { :public_body_category_id => category_id, + :public_body_heading_id => params[:heading_id] } + link = PublicBodyCategoryLink.where(conditions).first unless link error = "Couldn't find PublicBodyCategoryLink for category #{category_id}, heading #{params[:heading_id]}" raise ActiveRecord::Rollback diff --git a/spec/controllers/admin_public_body_category_controller_spec.rb b/spec/controllers/admin_public_body_category_controller_spec.rb index 5037d7671..6c14c5d93 100644 --- a/spec/controllers/admin_public_body_category_controller_spec.rb +++ b/spec/controllers/admin_public_body_category_controller_spec.rb @@ -209,6 +209,8 @@ describe AdminPublicBodyCategoryController do :category_display_order => 1) @default_params = { :categories => [@second_category.id, @first_category.id], :heading_id => @heading } + @old_order = [@first_category, @second_category] + @new_order = [@second_category, @first_category] end def make_request(params=@default_params) @@ -219,11 +221,10 @@ describe AdminPublicBodyCategoryController do it 'should reorder categories for the heading according to their position \ in the submitted params' do - old_order = [@first_category, @second_category] - new_order = [@second_category, @first_category] - @heading.public_body_categories.should == old_order + + @heading.public_body_categories.should == @old_order make_request - @heading.public_body_categories(reload=true).should == new_order + @heading.public_body_categories(reload=true).should == @new_order end it 'should return a success status' do @@ -234,13 +235,23 @@ describe AdminPublicBodyCategoryController do context 'when handling invalid input' do + before do + @new_category = FactoryGirl.create(:public_body_category) + @params = @default_params.merge(:categories => [@second_category.id, + @first_category.id, + @new_category.id]) + end + it 'should return an "unprocessable entity" status and an error message' do - @first_category.destroy - make_request + make_request(@params) assert_response :unprocessable_entity response.body.should match("Couldn't find PublicBodyCategoryLink") end + it 'should not reorder the categories for the heading' do + make_request(@params) + @heading.public_body_categories(reload=true).should == @old_order + end end end diff --git a/spec/controllers/admin_public_body_heading_controller_spec.rb b/spec/controllers/admin_public_body_heading_controller_spec.rb index fc93a22b4..d41b09807 100644 --- a/spec/controllers/admin_public_body_heading_controller_spec.rb +++ b/spec/controllers/admin_public_body_heading_controller_spec.rb @@ -160,12 +160,24 @@ describe AdminPublicBodyHeadingController do end end - it 'should return an "unprocessable entity" status and an error message' do - @first.destroy - make_request - assert_response :unprocessable_entity - response.body.should match("Couldn't find PublicBodyHeading with id") - end + context 'when handling invalid input' do + + before do + @params = { :headings => [@second.id, @first.id, @second.id + 1]} + end + + it 'should return an "unprocessable entity" status and an error message' do + make_request(@params) + assert_response :unprocessable_entity + response.body.should match("Couldn't find PublicBodyHeading with id") + end + + it 'should not reorder headings' do + make_request(@params) + PublicBodyHeading.find(@first.id).display_order.should == 0 + PublicBodyHeading.find(@second.id).display_order.should == 1 + end + end end end |