From e14a246ff4e626033a5ce24a8add88f0f33dfadf Mon Sep 17 00:00:00 2001 From: AntonioTerceiro Date: Wed, 30 Jul 2008 20:53:29 +0000 Subject: [PATCH] ActionItem593: going back to public view when editing from there --- app/controllers/my_profile/cms_controller.rb | 13 ++++++++++++- app/views/cms/edit.rhtml | 6 +++++- test/functional/cms_controller_test.rb | 24 ++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/app/controllers/my_profile/cms_controller.rb b/app/controllers/my_profile/cms_controller.rb index c9e567a..be83a72 100644 --- a/app/controllers/my_profile/cms_controller.rb +++ b/app/controllers/my_profile/cms_controller.rb @@ -41,6 +41,7 @@ class CmsController < MyProfileController @article = profile.articles.find(params[:id]) @parent_id = params[:parent_id] @type = params[:type] + record_coming_from_public_view if request.post? @article.last_changed_by = user if @article.update_attributes(params[:article]) @@ -129,12 +130,22 @@ class CmsController < MyProfileController protected def redirect_back - if @article.parent + if params[:back_to] == 'public_view' + redirect_to @article.url + elsif @article.parent redirect_to :action => 'view', :id => @article.parent else redirect_to :action => 'index' end end + def record_coming_from_public_view + referer = request.referer + if (referer == url_for(@article.url)) || (@article == @profile.home_page && referer == url_for(@profile.url)) + @back_to = 'public_view' + @back_url = @article.url + end + end + end diff --git a/app/views/cms/edit.rhtml b/app/views/cms/edit.rhtml index b7bb419..ba0aa72 100644 --- a/app/views/cms/edit.rhtml +++ b/app/views/cms/edit.rhtml @@ -6,6 +6,8 @@ <%= hidden_field_tag('parent_id', @parent_id) if @parent_id %> + <%= hidden_field_tag('back_to', @back_to) %> + <%= render :partial => partial_for_class(@article.class), :locals => { :f => f } %> <% button_bar do %> @@ -31,7 +33,9 @@ <% button_bar do %> <%= submit_button :save, _('Save') %> - <% if @parent_id || @article.parent %> + <% if @back_url %> + <%= button :cancel, _('Cancel'), @back_url %> + <% elsif @parent_id || @article.parent %> <%= button :cancel, _('Cancel'), :action => 'view', :id => @parent_id || @article.parent %> <% else %> <%= button :cancel, _('Cancel'), :action => 'index' %> diff --git a/test/functional/cms_controller_test.rb b/test/functional/cms_controller_test.rb index 8eee82f..97297d9 100644 --- a/test/functional/cms_controller_test.rb +++ b/test/functional/cms_controller_test.rb @@ -505,4 +505,28 @@ class CmsControllerTest < Test::Unit::TestCase assert_equal [c1, c2], assigns(:categories) end + should 'record when coming from public view on edit' do + article = @profile.articles.create!(:name => 'myarticle') + + @request.expects(:referer).returns('http://colivre.net/testinguser/myarticle') + + get :edit, :profile => 'testinguser', :id => article.id + assert_tag :tag => 'input', :attributes => { :type => 'hidden', :name => 'back_to', :value => 'public_view' } + assert_tag :tag => 'a', :descendant => { :content => 'Cancel' }, :attributes => { :href => 'http://colivre.net/testinguser/myarticle' } + end + + should 'detect when comming from home page' do + @request.expects(:referer).returns('http://colivre.net/testinguser') + get :edit, :profile => 'testinguser', :id => @profile.home_page.id + assert_tag :tag => 'input', :attributes => { :type => 'hidden', :name => 'back_to', :value => 'public_view' } + assert_tag :tag => 'a', :descendant => { :content => 'Cancel' }, :attributes => { :href => 'http://colivre.net/testinguser/testingusers-home-page' } + end + + should 'go back to public view when saving coming from there' do + article = @profile.articles.create!(:name => 'myarticle') + + post :edit, :profile => 'testinguser', :id => article.id, :back_to => 'public_view' + assert_redirected_to article.url + end + end -- libgit2 0.21.2