diff options
-rw-r--r-- | app/controllers/admin_public_body_category_controller.rb | 23 | ||||
-rw-r--r-- | app/controllers/admin_public_body_heading_controller.rb | 21 | ||||
-rw-r--r-- | config/routes.rb | 2 | ||||
-rw-r--r-- | spec/controllers/admin_public_body_category_controller_spec.rb | 51 | ||||
-rw-r--r-- | spec/controllers/admin_public_body_heading_controller_spec.rb | 39 |
5 files changed, 135 insertions, 1 deletions
diff --git a/app/controllers/admin_public_body_category_controller.rb b/app/controllers/admin_public_body_category_controller.rb index e22c5c572..d138eb495 100644 --- a/app/controllers/admin_public_body_category_controller.rb +++ b/app/controllers/admin_public_body_category_controller.rb @@ -55,6 +55,29 @@ class AdminPublicBodyCategoryController < AdminController end end + def reorder + 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]]) + unless link + error = "Couldn't find PublicBodyCategoryLink for category #{category_id}, heading #{params[:heading_id]}" + raise ActiveRecord::Rollback + end + link.category_display_order = index + unless link.save + error = link.errors.full_messages.join(",") + raise ActiveRecord::Rollback + end + end + render :nothing => true, :status => :ok and return + end + render :text => error, :status => :unprocessable_entity + end + def create I18n.with_locale(I18n.default_locale) do @category = PublicBodyCategory.new(params[:public_body_category]) diff --git a/app/controllers/admin_public_body_heading_controller.rb b/app/controllers/admin_public_body_heading_controller.rb index 43d8e329c..cc9d586a7 100644 --- a/app/controllers/admin_public_body_heading_controller.rb +++ b/app/controllers/admin_public_body_heading_controller.rb @@ -18,6 +18,27 @@ class AdminPublicBodyHeadingController < AdminController end end + def reorder + error = nil + ActiveRecord::Base.transaction do + params[:headings].each_with_index do |heading_id, index| + begin + heading = PublicBodyHeading.find(heading_id) + rescue ActiveRecord::RecordNotFound => e + error = e.message + raise ActiveRecord::Rollback + end + heading.display_order = index + unless heading.save + error = heading.errors.full_messages.join(",") + raise ActiveRecord::Rollback + end + end + render :nothing => true, :status => :ok and return + end + render :text => error, :status => :unprocessable_entity + end + def new @heading = PublicBodyHeading.new render :formats => [:html] diff --git a/config/routes.rb b/config/routes.rb index a97631979..e87a2a98b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -180,6 +180,7 @@ Alaveteli::Application.routes.draw do #### AdminPublicBodyCategory controller match '/admin/category' => 'admin_public_body_category#index', :as => :admin_category_index + match '/admin/category/reorder/:heading_id' => 'admin_public_body_category#reorder', :as => :admin_category_reorder, :via => :post match '/admin/category/new' => 'admin_public_body_category#new', :as => :admin_category_new match '/admin/category/edit/:id' => 'admin_public_body_category#edit', :as => :admin_category_edit match '/admin/category/update/:id' => 'admin_public_body_category#update', :as => :admin_category_update @@ -189,6 +190,7 @@ Alaveteli::Application.routes.draw do #### AdminPublicBodyHeading controller match '/admin/category_heading' => 'admin_public_body_heading#index' + match '/admin/category_heading/reorder' => 'admin_public_body_heading#reorder', :as => :admin_heading_reorder, :via => :post match '/admin/category_heading/new' => 'admin_public_body_heading#new', :as => :admin_heading_new match '/admin/category_heading/edit/:id' => 'admin_public_body_heading#edit', :as => :admin_heading_edit match '/admin/category_heading/update/:id' => 'admin_public_body_heading#update', :as => :admin_heading_update diff --git a/spec/controllers/admin_public_body_category_controller_spec.rb b/spec/controllers/admin_public_body_category_controller_spec.rb index 4f921bb9b..47ae38176 100644 --- a/spec/controllers/admin_public_body_category_controller_spec.rb +++ b/spec/controllers/admin_public_body_category_controller_spec.rb @@ -171,4 +171,55 @@ describe AdminPublicBodyCategoryController do PublicBodyCategory.count.should == n - 1 end end + + context 'when reordering public body categories' do + + render_views + + before do + @silly_heading = FactoryGirl.create(:silly_heading) + @useless_category = @silly_heading.public_body_categories.detect do |category| + category.title == 'Useless ministries' + end + @lonely_category = @silly_heading.public_body_categories.detect do |category| + category.title == 'Lonely agencies' + end + @default_params = { :categories => [@lonely_category.id, @useless_category.id], + :heading_id => @silly_heading } + end + + def make_request(params=@default_params) + post :reorder, params + end + + context 'when handling valid input' do + + it 'should reorder categories for the heading according to their position \ + in the submitted params' do + old_order = [@useless_category, @lonely_category] + new_order = [@lonely_category, @useless_category] + @silly_heading.public_body_categories.should == old_order + make_request + @silly_heading.public_body_categories(reload=true).should == new_order + end + + it 'should return a success status' do + make_request + response.should be_success + end + end + + context 'when handling invalid input' do + + it 'should return an "unprocessable entity" status and an error message' do + @lonely_category.destroy + make_request + assert_response :unprocessable_entity + response.body.should match("Couldn't find PublicBodyCategoryLink") + end + + 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 2a8214703..58a3adbaa 100644 --- a/spec/controllers/admin_public_body_heading_controller_spec.rb +++ b/spec/controllers/admin_public_body_heading_controller_spec.rb @@ -117,11 +117,48 @@ describe AdminPublicBodyHeadingController do end it "destroys an empty public body heading" do - heading = PublicBodyHeading.create(:name => "Empty Heading") + heading = FactoryGirl.create(:heading_with_no_categories) n = PublicBodyHeading.count post :destroy, { :id => heading.id } response.should redirect_to(:controller=>'admin_public_body_category', :action=>'index') PublicBodyHeading.count.should == n - 1 end end + + context 'when reordering public body headings' do + + render_views + + before do + @silly_heading = FactoryGirl.create(:silly_heading) + @popular_heading = FactoryGirl.create(:popular_heading) + @default_params = { :headings => [@popular_heading.id, @silly_heading.id] } + end + + def make_request(params=@default_params) + post :reorder, params + end + + context 'when handling valid input' do + + it 'should reorder headings according to their position in the submitted params' do + make_request + PublicBodyHeading.find(@popular_heading.id).display_order.should == 0 + PublicBodyHeading.find(@silly_heading.id).display_order.should == 1 + end + + it 'should return a "success" status' do + make_request + response.should be_success + end + end + + it 'should return an "unprocessable entity" status and an error message' do + @popular_heading.destroy + make_request + assert_response :unprocessable_entity + response.body.should match("Couldn't find PublicBodyHeading with id") + end + + end end |