diff options
-rw-r--r-- | app/assets/javascripts/admin/category-order.js | 9 | ||||
-rw-r--r-- | app/controllers/admin_public_body_categories_controller.rb (renamed from app/controllers/admin_public_body_category_controller.rb) | 28 | ||||
-rw-r--r-- | app/controllers/admin_public_body_headings_controller.rb (renamed from app/controllers/admin_public_body_heading_controller.rb) | 30 | ||||
-rw-r--r-- | app/views/admin_general/_admin_navbar.html.erb | 2 | ||||
-rw-r--r-- | app/views/admin_public_body_categories/_form.html.erb (renamed from app/views/admin_public_body_category/_form.html.erb) | 0 | ||||
-rw-r--r-- | app/views/admin_public_body_categories/_one.html.erb (renamed from app/views/admin_public_body_category/_one.html.erb) | 2 | ||||
-rw-r--r-- | app/views/admin_public_body_categories/_one_list.html.erb (renamed from app/views/admin_public_body_category/_one_list.html.erb) | 9 | ||||
-rw-r--r-- | app/views/admin_public_body_categories/edit.html.erb (renamed from app/views/admin_public_body_category/edit.html.erb) | 8 | ||||
-rw-r--r-- | app/views/admin_public_body_categories/index.html.erb (renamed from app/views/admin_public_body_category/index.html.erb) | 6 | ||||
-rw-r--r-- | app/views/admin_public_body_categories/new.html.erb (renamed from app/views/admin_public_body_category/new.html.erb) | 4 | ||||
-rw-r--r-- | app/views/admin_public_body_headings/_form.html.erb (renamed from app/views/admin_public_body_heading/_form.html.erb) | 0 | ||||
-rw-r--r-- | app/views/admin_public_body_headings/edit.html.erb (renamed from app/views/admin_public_body_heading/edit.html.erb) | 8 | ||||
-rw-r--r-- | app/views/admin_public_body_headings/new.html.erb (renamed from app/views/admin_public_body_heading/new.html.erb) | 4 | ||||
-rw-r--r-- | config/routes.rb | 25 | ||||
-rw-r--r-- | spec/controllers/admin_public_body_categories_controller_spec.rb (renamed from spec/controllers/admin_public_body_category_controller_spec.rb) | 72 | ||||
-rw-r--r-- | spec/controllers/admin_public_body_headings_controller_spec.rb (renamed from spec/controllers/admin_public_body_heading_controller_spec.rb) | 75 |
16 files changed, 135 insertions, 147 deletions
diff --git a/app/assets/javascripts/admin/category-order.js b/app/assets/javascripts/admin/category-order.js index 6e210083c..3be82e46f 100644 --- a/app/assets/javascripts/admin/category-order.js +++ b/app/assets/javascripts/admin/category-order.js @@ -1,14 +1,12 @@ $(function() { - var endpoints = { 'heading' : '/admin/category_heading/reorder', - 'category' : '/admin/category/reorder' } $('.save-order').each(function(index){ // identify the elements that will work together var save_button = $(this); var save_notice = save_button.next(); var save_panel = save_button.parent(); - var list_type = save_button.data('list-type'); var list_element = $(save_button.data('list-id')); + var endpoint = save_button.data('endpoint'); // on the first list change, show that there are unsaved changes list_element.sortable({ @@ -24,11 +22,6 @@ $(function() { save_button.click(function(){ if (!save_button.is('.disabled')){ var data = list_element.sortable('serialize', {'attribute': 'data-id'}); - var endpoint = endpoints[list_type]; - if (list_type == 'category'){ - heading_id = save_button.data('heading-id'); - endpoint = endpoint + "/" + heading_id; - } var update_call = $.ajax({ data: data, type: 'POST', url: endpoint }); // on success, disable the save button again, and show success notice diff --git a/app/controllers/admin_public_body_category_controller.rb b/app/controllers/admin_public_body_categories_controller.rb index 99a4a7f3c..e9ca57434 100644 --- a/app/controllers/admin_public_body_category_controller.rb +++ b/app/controllers/admin_public_body_categories_controller.rb @@ -1,4 +1,4 @@ -class AdminPublicBodyCategoryController < AdminController +class AdminPublicBodyCategoriesController < AdminController def index @locale = self.locale_from_params @category_headings = PublicBodyHeading.all @@ -55,28 +55,6 @@ class AdminPublicBodyCategoryController < AdminController end end - def reorder - error = nil - ActiveRecord::Base.transaction do - params[:categories].each_with_index do |category_id, index| - 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 - 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]) @@ -87,7 +65,7 @@ class AdminPublicBodyCategoryController < AdminController end end flash[:notice] = 'Category was successfully created.' - redirect_to admin_category_index_url + redirect_to categories_path else render :action => 'new' end @@ -100,7 +78,7 @@ class AdminPublicBodyCategoryController < AdminController category = PublicBodyCategory.find(params[:id]) category.destroy flash[:notice] = "Category was successfully destroyed." - redirect_to admin_category_index_url + redirect_to categories_path end end end diff --git a/app/controllers/admin_public_body_heading_controller.rb b/app/controllers/admin_public_body_headings_controller.rb index 0fba350cc..6eec4b65e 100644 --- a/app/controllers/admin_public_body_heading_controller.rb +++ b/app/controllers/admin_public_body_headings_controller.rb @@ -1,4 +1,4 @@ -class AdminPublicBodyHeadingController < AdminController +class AdminPublicBodyHeadingsController < AdminController def edit @heading = PublicBodyHeading.find(params[:id]) @@ -36,6 +36,28 @@ class AdminPublicBodyHeadingController < AdminController render :text => error, :status => :unprocessable_entity end + def reorder_categories + error = nil + ActiveRecord::Base.transaction do + params[:categories].each_with_index do |category_id, index| + conditions = { :public_body_category_id => category_id, + :public_body_heading_id => params[:id] } + link = PublicBodyCategoryLink.where(conditions).first + unless link + error = "Couldn't find PublicBodyCategoryLink for category #{category_id}, heading #{params[: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 new @heading = PublicBodyHeading.new render :formats => [:html] @@ -46,7 +68,7 @@ class AdminPublicBodyHeadingController < AdminController @heading = PublicBodyHeading.new(params[:public_body_heading]) if @heading.save flash[:notice] = 'Category heading was successfully created.' - redirect_to admin_category_index_url + redirect_to categories_url else render :action => 'new' end @@ -60,13 +82,13 @@ class AdminPublicBodyHeadingController < AdminController if heading.public_body_categories.count > 0 flash[:notice] = "There are categories associated with this heading, so can't destroy it" - redirect_to admin_heading_edit_url(heading) + redirect_to edit_heading_url(heading) return end heading.destroy flash[:notice] = "Category heading was successfully destroyed." - redirect_to admin_category_index_url + redirect_to categories_url end end end diff --git a/app/views/admin_general/_admin_navbar.html.erb b/app/views/admin_general/_admin_navbar.html.erb index dcf55dc50..e19a0ac5c 100644 --- a/app/views/admin_general/_admin_navbar.html.erb +++ b/app/views/admin_general/_admin_navbar.html.erb @@ -10,7 +10,7 @@ <li><%= link_to 'Stats', admin_stats_path %></li> <li><%= link_to 'Debug', admin_debug_path %></li> <li><%= link_to 'Authorities', admin_body_list_path %></li> - <li><%= link_to 'Categories', admin_category_index_path %></li> + <li><%= link_to 'Categories', categories_path %></li> <li><%= link_to 'Requests', admin_request_list_path %></li> <li><%= link_to 'Users', admin_user_list_path %></li> <li><%= link_to 'Tracks', admin_track_list_path %></li> diff --git a/app/views/admin_public_body_category/_form.html.erb b/app/views/admin_public_body_categories/_form.html.erb index b0778d371..b0778d371 100644 --- a/app/views/admin_public_body_category/_form.html.erb +++ b/app/views/admin_public_body_categories/_form.html.erb diff --git a/app/views/admin_public_body_category/_one.html.erb b/app/views/admin_public_body_categories/_one.html.erb index 6b881a669..46f4f855c 100644 --- a/app/views/admin_public_body_category/_one.html.erb +++ b/app/views/admin_public_body_categories/_one.html.erb @@ -1,5 +1,5 @@ <div class="category-list-item" <% if heading %> data-id="categories_<%= category.id %>"<% end %>> - <%= link_to(category.title, admin_category_edit_path(category), :title => "view full details") %> + <%= link_to(category.title, edit_category_path(category), :title => "view full details") %> </div> diff --git a/app/views/admin_public_body_category/_one_list.html.erb b/app/views/admin_public_body_categories/_one_list.html.erb index 1c7856c10..1849f4b22 100644 --- a/app/views/admin_public_body_category/_one_list.html.erb +++ b/app/views/admin_public_body_categories/_one_list.html.erb @@ -1,12 +1,10 @@ - - <div class="accordion" id="category_list"> <% for heading in category_headings %> <div class="accordion-group" data-id="headings_<%=heading.id%>"> <div class="accordion-heading accordion-toggle row"> <span class="item-title span6"> <a href="#heading_<%=heading.id%>_categories" data-toggle="collapse" data-parent="#categories" ><%= chevron_right %></a> - <strong><%= link_to(heading.name, admin_heading_edit_path(heading), :title => "view full details") %></strong> + <strong><%= link_to(heading.name, edit_heading_path(heading), :title => "view full details") %></strong> </span> </div> @@ -19,13 +17,10 @@ </div> <div class="form-actions save-panel"> - <%= link_to "Save", '#', :class => "btn btn-primary disabled save-order", "data-heading-id" => heading.id, "data-list-id" => "#heading_#{heading.id}_category_list", 'data-list-type' => 'category' %><p class="save-notice" data-unsaved-text="There are unsaved changes to the order of categories." data-success-text="Changes saved." data-error-text="There was an error saving your changes: ">Drag and drop to change the order of categories.</p> + <%= link_to "Save", '#', :class => "btn btn-primary disabled save-order", "data-heading-id" => heading.id, "data-list-id" => "#heading_#{heading.id}_category_list", 'data-endpoint' => reorder_categories_heading_path(heading) %><p class="save-notice" data-unsaved-text="There are unsaved changes to the order of categories." data-success-text="Changes saved." data-error-text="There was an error saving your changes: ">Drag and drop to change the order of categories.</p> </div> </div> </div> - - </div> <% end %> - </div> diff --git a/app/views/admin_public_body_category/edit.html.erb b/app/views/admin_public_body_categories/edit.html.erb index c1e522cac..206e610fc 100644 --- a/app/views/admin_public_body_category/edit.html.erb +++ b/app/views/admin_public_body_categories/edit.html.erb @@ -3,7 +3,7 @@ <div class="row"> <div class="span8"> <div id="public_body_category_form"> - <%= form_for @category, :url => admin_category_update_path(@category), :html => { :class => "form form-horizontal" } do |f| %> + <%= form_for @category, :url => category_path(@category), :html => { :class => "form form-horizontal" } do |f| %> <%= render :partial => 'form', :locals => {:f => f} %> <div class="form-actions"> <%= f.submit 'Save', :accesskey => 's', :class => "btn btn-success" %></p> @@ -14,17 +14,17 @@ <div class="row"> <div class="span8 well"> - <%= link_to 'List all', admin_category_index_path, :class => "btn" %> + <%= link_to 'List all', categories_path, :class => "btn" %> </div> </div> <% if @tagged_public_bodies.empty? %> <div class="row"> <div class="span8"> - <%= form_tag(admin_category_destroy_path(@category), :class => "form form-inline") do %> + <%= form_tag(category_path(@category), :method => 'delete', :class => "form form-inline") do %> <%= hidden_field_tag(:public_body_id, { :value => @category.id } ) %> <%= submit_tag "Destroy #{@category.title}", :title => @category.title, :class => "btn btn-danger" %> (this is permanent!) <% end %> </div> </div> -<% end %>
\ No newline at end of file +<% end %> diff --git a/app/views/admin_public_body_category/index.html.erb b/app/views/admin_public_body_categories/index.html.erb index 85f2282ab..061ad0080 100644 --- a/app/views/admin_public_body_category/index.html.erb +++ b/app/views/admin_public_body_categories/index.html.erb @@ -4,10 +4,10 @@ <div class="btn-toolbar"> <div class="btn-group"> - <%= link_to 'New category', admin_category_new_path, :class => "btn btn-primary" %> + <%= link_to 'New category', new_category_path, :class => "btn btn-primary" %> </div> <div class="btn-group"> - <%= link_to 'New category heading', admin_heading_new_path, :class => "btn" %> + <%= link_to 'New category heading', new_heading_path, :class => "btn" %> </div> </div> @@ -24,5 +24,5 @@ <% end %> <% end %> <div class="form-actions save-panel"> -<%= link_to "Save", '#', :class => "btn btn-primary disabled save-order", "data-list-id" => '#category_list', 'data-list-type' => 'heading' %><p class="save-notice" data-unsaved-text="There are unsaved changes to the order of category headings." data-success-text="Changes saved." data-error-text="There was an error saving your changes: ">Drag and drop to change the order of category headings.</p> +<%= link_to "Save", '#', :class => "btn btn-primary disabled save-order", "data-list-id" => '#category_list', 'data-endpoint' => reorder_headings_path %><p class="save-notice" data-unsaved-text="There are unsaved changes to the order of category headings." data-success-text="Changes saved." data-error-text="There was an error saving your changes: ">Drag and drop to change the order of category headings.</p> </div> diff --git a/app/views/admin_public_body_category/new.html.erb b/app/views/admin_public_body_categories/new.html.erb index 0a016182f..f91f5597c 100644 --- a/app/views/admin_public_body_category/new.html.erb +++ b/app/views/admin_public_body_categories/new.html.erb @@ -4,7 +4,7 @@ <div class="row"> <div class="span8"> <div id="public_category_form"> - <%= form_for @category, :as => :public_body_category, :url => admin_category_create_path, :html => {:class => "form form-horizontal"} do |f| %> + <%= form_for @category, :url => categories_path, :html => {:class => "form form-horizontal"} do |f| %> <%= render :partial => 'form', :locals => {:f => f} %> <div class="form-actions"> @@ -13,7 +13,7 @@ <% end %> <div class="row"> <div class="span8 well"> - <%= link_to 'List all', admin_category_index_path, :class => "btn" %> + <%= link_to 'List all', categories_path, :class => "btn" %> </div> </div> </div> diff --git a/app/views/admin_public_body_heading/_form.html.erb b/app/views/admin_public_body_headings/_form.html.erb index d4e914ca1..d4e914ca1 100644 --- a/app/views/admin_public_body_heading/_form.html.erb +++ b/app/views/admin_public_body_headings/_form.html.erb diff --git a/app/views/admin_public_body_heading/edit.html.erb b/app/views/admin_public_body_headings/edit.html.erb index df5ae7d0e..1c8416df3 100644 --- a/app/views/admin_public_body_heading/edit.html.erb +++ b/app/views/admin_public_body_headings/edit.html.erb @@ -3,7 +3,7 @@ <div class="row"> <div class="span8"> <div id="public_body_heading_form"> - <%= form_for @heading, :url => admin_heading_update_path(@heading), :html => { :class => "form form-horizontal" } do |f| %> + <%= form_for @heading, :url => heading_path(@heading), :html => { :class => "form form-horizontal" } do |f| %> <%= render :partial => 'form', :locals => {:f => f} %> <div class="form-actions"> <%= f.submit 'Save', :accesskey => 's', :class => "btn btn-success" %></p> @@ -14,17 +14,17 @@ <div class="row"> <div class="span8 well"> - <%= link_to 'List all', admin_category_index_path, :class => "btn" %> + <%= link_to 'List all', categories_path, :class => "btn" %> </div> </div> <% if @heading.public_body_categories.empty? %> <div class="row"> <div class="span8"> - <%= form_tag(admin_heading_destroy_path(@heading), :class => "form form-inline") do %> + <%= form_tag(heading_path(@heading), :method => 'delete', :class => "form form-inline") do %> <%= hidden_field_tag(:public_body_heading_id, { :value => @heading.id } ) %> <%= submit_tag "Destroy #{@heading.name}", :name => @heading.name, :class => "btn btn-danger" %> (this is permanent!) <% end %> </div> </div> -<% end %>
\ No newline at end of file +<% end %> diff --git a/app/views/admin_public_body_heading/new.html.erb b/app/views/admin_public_body_headings/new.html.erb index 22cb88901..b0066481f 100644 --- a/app/views/admin_public_body_heading/new.html.erb +++ b/app/views/admin_public_body_headings/new.html.erb @@ -4,7 +4,7 @@ <div class="row"> <div class="span8"> <div id="public_heading_form"> - <%= form_for @heading, :as => :public_body_heading, :url => admin_heading_create_path, :html => {:class => "form form-horizontal"} do |f| %> + <%= form_for @heading, :url => headings_path, :html => {:class => "form form-horizontal"} do |f| %> <%= render :partial => 'form', :locals => {:f => f} %> <div class="form-actions"> @@ -13,7 +13,7 @@ <% end %> <div class="row"> <div class="span8 well"> - <%= link_to 'List all', admin_category_index_path, :class => "btn" %> + <%= link_to 'List all', categories_path, :class => "btn" %> </div> </div> </div> diff --git a/config/routes.rb b/config/routes.rb index 3aff84c4b..90fab52e8 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -179,22 +179,21 @@ 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 - match '/admin/category/create' => 'admin_public_body_category#create', :as => :admin_category_create - match '/admin/category/destroy/:id' => 'admin_public_body_category#destroy', :as => :admin_category_destroy + scope '/admin' do + resources :categories, + :controller => 'admin_public_body_categories' + end #### #### AdminPublicBodyHeading controller - 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 - match '/admin/category_heading/create' => 'admin_public_body_heading#create', :as => :admin_heading_create - match '/admin/category_heading/destroy/:id' => 'admin_public_body_heading#destroy', :as => :admin_heading_destroy + scope '/admin' do + resources :headings, + :controller => 'admin_public_body_headings', + :except => [:index] do + post 'reorder', :on => :collection + post 'reorder_categories', :on => :member + end + end #### #### AdminPublicBodyChangeRequest controller diff --git a/spec/controllers/admin_public_body_category_controller_spec.rb b/spec/controllers/admin_public_body_categories_controller_spec.rb index 649331884..25bdd64ad 100644 --- a/spec/controllers/admin_public_body_category_controller_spec.rb +++ b/spec/controllers/admin_public_body_categories_controller_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe AdminPublicBodyCategoryController do +describe AdminPublicBodyCategoriesController do context 'when showing the index of categories and headings' do render_views @@ -29,7 +29,7 @@ describe AdminPublicBodyCategoryController do PublicBodyCategory.count.should == n + 1 category = PublicBodyCategory.find_by_title("New Category") - response.should redirect_to(:controller=>'admin_public_body_category', :action=>'index') + response.should redirect_to(categories_path) end it "saves the public body category's heading associations" do @@ -67,7 +67,7 @@ describe AdminPublicBodyCategoryController do category.title.should == "Mi Nuevo Category" end - response.should redirect_to(:controller=>'admin_public_body_category', :action=>'index') + response.should redirect_to(categories_path) end end @@ -183,74 +183,10 @@ describe AdminPublicBodyCategoryController do pbc = PublicBodyCategory.create(:title => "Empty Category", :category_tag => "empty", :description => "-") n = PublicBodyCategory.count post :destroy, { :id => pbc.id } - response.should redirect_to(:controller=>'admin_public_body_category', :action=>'index') + response.should redirect_to(categories_path) PublicBodyCategory.count.should == n - 1 end end - context 'when reordering public body categories' do - - render_views - - before do - @heading = FactoryGirl.create(:public_body_heading) - @first_category = FactoryGirl.create(:public_body_category) - @first_link = FactoryGirl.create(:public_body_category_link, - :public_body_category => @first_category, - :public_body_heading => @heading, - :category_display_order => 0) - @second_category = FactoryGirl.create(:public_body_category) - @second_link = FactoryGirl.create(:public_body_category_link, - :public_body_category => @second_category, - :public_body_heading => @heading, - :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) - 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 - - @heading.public_body_categories.should == @old_order - make_request - @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 - - 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 - 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 end diff --git a/spec/controllers/admin_public_body_heading_controller_spec.rb b/spec/controllers/admin_public_body_headings_controller_spec.rb index 8d9833fe9..2191c916d 100644 --- a/spec/controllers/admin_public_body_heading_controller_spec.rb +++ b/spec/controllers/admin_public_body_headings_controller_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe AdminPublicBodyHeadingController do +describe AdminPublicBodyHeadingsController do context 'when showing the form for a new public body category' do it 'should assign a new public body heading to the view' do @@ -20,7 +20,7 @@ describe AdminPublicBodyHeadingController do PublicBodyHeading.count.should == n + 1 heading = PublicBodyHeading.find_by_name("New Heading") - response.should redirect_to(:controller=>'admin_public_body_category', :action=>'index') + response.should redirect_to(categories_path) end it 'creates a new public body heading with multiple locales' do @@ -43,7 +43,7 @@ describe AdminPublicBodyHeadingController do heading.name.should == "Mi Nuevo Heading" end - response.should redirect_to(:controller=>'admin_public_body_category', :action=>'index') + response.should redirect_to(categories_path) end end @@ -112,14 +112,14 @@ describe AdminPublicBodyHeadingController do :category_display_order => 0) n = PublicBodyHeading.count post :destroy, { :id => @heading.id } - response.should redirect_to(:controller=>'admin_public_body_heading', :action=>'edit', :id => @heading.id) + response.should redirect_to(edit_heading_path(@heading)) PublicBodyHeading.count.should == n end it "destroys an empty public body heading" do n = PublicBodyHeading.count post :destroy, { :id => @heading.id } - response.should redirect_to(:controller=>'admin_public_body_category', :action=>'index') + response.should redirect_to(categories_path) PublicBodyHeading.count.should == n - 1 end end @@ -172,4 +172,69 @@ describe AdminPublicBodyHeadingController do end end + + context 'when reordering public body categories' do + + render_views + + before do + @heading = FactoryGirl.create(:public_body_heading) + @first_category = FactoryGirl.create(:public_body_category) + @first_link = FactoryGirl.create(:public_body_category_link, + :public_body_category => @first_category, + :public_body_heading => @heading, + :category_display_order => 0) + @second_category = FactoryGirl.create(:public_body_category) + @second_link = FactoryGirl.create(:public_body_category_link, + :public_body_category => @second_category, + :public_body_heading => @heading, + :category_display_order => 1) + @default_params = { :categories => [@second_category.id, @first_category.id], + :id => @heading } + @old_order = [@first_category, @second_category] + @new_order = [@second_category, @first_category] + end + + def make_request(params=@default_params) + post :reorder_categories, params + end + + context 'when handling valid input' do + + it 'should reorder categories for the heading according to their position \ + in the submitted params' do + + @heading.public_body_categories.should == @old_order + make_request + @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 + + 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 + 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 end |