diff options
author | Gareth Rees <gareth@mysociety.org> | 2014-11-18 13:54:15 +0000 |
---|---|---|
committer | Gareth Rees <gareth@mysociety.org> | 2014-11-18 13:54:15 +0000 |
commit | d5d0ec771afed58e91429cfa626a1ee96faa92fb (patch) | |
tree | 241597ee2ecb743764a4ceedfc90ee47c3780bdc | |
parent | 3a412f2ec2c8f8c0a0eb5000791165ab82e7a550 (diff) | |
parent | 401aef5cf0bad318ac753ee5fb547e23c22deeb4 (diff) |
Merge branch '1977-update-categories-redirect-to-edit' into rails-3-develop
4 files changed, 82 insertions, 10 deletions
diff --git a/app/controllers/admin_public_body_categories_controller.rb b/app/controllers/admin_public_body_categories_controller.rb index fda09fa4a..5e305dde3 100644 --- a/app/controllers/admin_public_body_categories_controller.rb +++ b/app/controllers/admin_public_body_categories_controller.rb @@ -22,7 +22,8 @@ class AdminPublicBodyCategoriesController < AdminController I18n.with_locale(I18n.default_locale) do if params[:public_body_category][:category_tag] && PublicBody.find_by_tag(@category.category_tag).count > 0 && @category.category_tag != params[:public_body_category][:category_tag] - flash[:notice] = 'There are authorities associated with this category, so the tag can\'t be renamed' + flash[:error] = "There are authorities associated with this category, so the tag can't be renamed" + render :action => 'edit' else if params[:headings] heading_ids = params[:headings].values @@ -48,10 +49,11 @@ class AdminPublicBodyCategoriesController < AdminController if @category.update_attributes(params[:public_body_category]) flash[:notice] = 'Category was successfully updated.' + redirect_to edit_admin_category_path(@category) + else + render :action => 'edit' end end - - render :action => 'edit' end end diff --git a/app/controllers/admin_public_body_headings_controller.rb b/app/controllers/admin_public_body_headings_controller.rb index c7c80e802..84e5cbf35 100644 --- a/app/controllers/admin_public_body_headings_controller.rb +++ b/app/controllers/admin_public_body_headings_controller.rb @@ -10,8 +10,10 @@ class AdminPublicBodyHeadingsController < AdminController @heading = PublicBodyHeading.find(params[:id]) if @heading.update_attributes(params[:public_body_heading]) flash[:notice] = 'Category heading was successfully updated.' + redirect_to edit_admin_heading_path(@heading) + else + render :action => 'edit' end - render :action => 'edit' end end diff --git a/spec/controllers/admin_public_body_categories_controller_spec.rb b/spec/controllers/admin_public_body_categories_controller_spec.rb index 35454990d..4fb5181be 100644 --- a/spec/controllers/admin_public_body_categories_controller_spec.rb +++ b/spec/controllers/admin_public_body_categories_controller_spec.rb @@ -6,6 +6,7 @@ describe AdminPublicBodyCategoriesController do it 'shows the index page' do get :index + expect(response).to be_success end end @@ -14,6 +15,12 @@ describe AdminPublicBodyCategoriesController do get :new assigns[:category].should be_a(PublicBodyCategory) end + + it 'renders the new template' do + get :new + expect(response).to render_template('new') + end + end context 'when creating a public body category' do @@ -44,7 +51,6 @@ describe AdminPublicBodyCategoriesController do category.public_body_headings.should == [heading] end - it 'creates a new public body category with multiple locales' do n = PublicBodyCategory.count post :create, { @@ -69,6 +75,12 @@ describe AdminPublicBodyCategoriesController do response.should redirect_to(admin_categories_path) end + + it "renders the form if creating the record was unsuccessful" do + post :create, :public_body_category => { :title => '' } + expect(response).to render_template('new') + end + end context 'when editing a public body category' do @@ -82,14 +94,21 @@ describe AdminPublicBodyCategoriesController do render_views - it "edits a public body category" do + it "finds the requested category" do + get :edit, :id => @category.id + expect(assigns[:category]).to eq(@category) + end + + it "renders the edit template" do get :edit, :id => @category.id + expect(assigns[:category]).to render_template('edit') end it "edits a public body in another locale" do - get :edit, {:id => @category.id, :locale => :en} + get :edit, { :id => @category.id, :locale => :en } - # When editing a body, the controller returns all available translations + # When editing a body, the controller returns all available + # translations assigns[:category].find_translation_by_locale("es").title.should == 'Los category' response.should render_template('edit') end @@ -161,7 +180,9 @@ describe AdminPublicBodyCategoriesController do body = FactoryGirl.create(:public_body, :tag_string => @tag) post :update, { :id => @category.id, :public_body_category => { :category_tag => "renamed" } } - request.flash[:notice].should include('can\'t') + + msg = "There are authorities associated with this category, so the tag can't be renamed" + request.flash[:error].should == msg pbc = PublicBodyCategory.find(@category.id) pbc.category_tag.should == @tag end @@ -175,6 +196,21 @@ describe AdminPublicBodyCategoriesController do pbc = PublicBodyCategory.find(category.id) pbc.category_tag.should == "renamed" end + + it "redirects to the edit page after a successful update" do + post :update, { :id => @category.id, + :public_body_category => { :title => "Renamed" } } + + expect(response).to redirect_to(edit_admin_category_path(@category)) + end + + it "re-renders the edit form after an unsuccessful update" do + post :update, { :id => @category.id, + :public_body_category => { :title => '' } } + + expect(response).to render_template('edit') + end + end context 'when destroying a public body category' do diff --git a/spec/controllers/admin_public_body_headings_controller_spec.rb b/spec/controllers/admin_public_body_headings_controller_spec.rb index 31517d238..60f563e57 100644 --- a/spec/controllers/admin_public_body_headings_controller_spec.rb +++ b/spec/controllers/admin_public_body_headings_controller_spec.rb @@ -7,6 +7,11 @@ describe AdminPublicBodyHeadingsController do get :new assigns[:heading].should be_a(PublicBodyHeading) end + + it 'renders the new template' do + get :new + expect(response).to render_template('new') + end end context 'when creating a public body heading' do @@ -45,6 +50,12 @@ describe AdminPublicBodyHeadingsController do response.should redirect_to(admin_categories_path) end + + it "renders the form if creating the record was unsuccessful" do + post :create, :public_body_heading => { :name => '' } + expect(response).to render_template('new') + end + end context 'when editing a public body heading' do @@ -54,8 +65,14 @@ describe AdminPublicBodyHeadingsController do render_views - it "edits a public body heading" do + it "finds the requested heading" do get :edit, :id => @heading.id + expect(assigns[:heading]).to eq(@heading) + end + + it "renders the edit template" do + get :edit, :id => @heading.id + expect(assigns[:heading]).to render_template('edit') end end @@ -96,6 +113,21 @@ describe AdminPublicBodyHeadingsController do heading.name.should == @name end end + + it "redirects to the edit page after a successful update" do + post :update, { :id => @heading.id, + :public_body_heading => { :name => "Renamed" } } + + expect(response).to redirect_to(edit_admin_heading_path(@heading)) + end + + it "re-renders the edit form after an unsuccessful update" do + post :update, { :id => @heading.id, + :public_body_heading => { :name => '' } } + + expect(response).to render_template('edit') + end + end context 'when destroying a public body heading' do |