Commit c6c0bf79b57d7eaff5156fe55371838961bbfefd

Authored by Victor Costa
1 parent 4eb56579

Redirect to url when save a content

Added a parameter to specify a url to redirect when save (new or edit) a content with success
app/controllers/my_profile/cms_controller.rb
@@ -64,6 +64,7 @@ class CmsController < MyProfileController @@ -64,6 +64,7 @@ class CmsController < MyProfileController
64 end 64 end
65 65
66 def edit 66 def edit
  67 + @success_back_to = params[:success_back_to]
67 @article = profile.articles.find(params[:id]) 68 @article = profile.articles.find(params[:id])
68 @parent_id = params[:parent_id] 69 @parent_id = params[:parent_id]
69 @type = params[:type] || @article.class.to_s 70 @type = params[:type] || @article.class.to_s
@@ -77,7 +78,7 @@ class CmsController < MyProfileController @@ -77,7 +78,7 @@ class CmsController < MyProfileController
77 if @article.update_attributes(params[:article]) 78 if @article.update_attributes(params[:article])
78 if !continue 79 if !continue
79 if @article.content_type.nil? || @article.image? 80 if @article.content_type.nil? || @article.image?
80 - redirect_to @article.view_url 81 + success_redirect
81 else 82 else
82 redirect_to :action => (@article.parent ? 'view' : 'index'), :id => @article.parent 83 redirect_to :action => (@article.parent ? 'view' : 'index'), :id => @article.parent
83 end 84 end
@@ -89,6 +90,7 @@ class CmsController < MyProfileController @@ -89,6 +90,7 @@ class CmsController < MyProfileController
89 def new 90 def new
90 # FIXME this method should share some logic wirh edit !!! 91 # FIXME this method should share some logic wirh edit !!!
91 92
  93 + @success_back_to = params[:success_back_to]
92 # user must choose an article type first 94 # user must choose an article type first
93 95
94 @parent = profile.articles.find(params[:parent_id]) if params && params[:parent_id] 96 @parent = profile.articles.find(params[:parent_id]) if params && params[:parent_id]
@@ -133,7 +135,7 @@ class CmsController < MyProfileController @@ -133,7 +135,7 @@ class CmsController < MyProfileController
133 if continue 135 if continue
134 redirect_to :action => 'edit', :id => @article 136 redirect_to :action => 'edit', :id => @article
135 else 137 else
136 - redirect_to @article.view_url 138 + success_redirect
137 end 139 end
138 return 140 return
139 end 141 end
@@ -385,5 +387,13 @@ class CmsController < MyProfileController @@ -385,5 +387,13 @@ class CmsController < MyProfileController
385 true 387 true
386 end 388 end
387 389
  390 + def success_redirect
  391 + if !@success_back_to.blank?
  392 + redirect_to @success_back_to
  393 + else
  394 + redirect_to @article.view_url
  395 + end
  396 + end
  397 +
388 end 398 end
389 399
app/views/cms/edit.rhtml
@@ -9,6 +9,8 @@ @@ -9,6 +9,8 @@
9 9
10 <%= hidden_field_tag('back_to', @back_to) %> 10 <%= hidden_field_tag('back_to', @back_to) %>
11 11
  12 + <%= hidden_field_tag('success_back_to', @success_back_to) %>
  13 +
12 <%= render :partial => partial_for_class(@article.class), :locals => { :f => f } %> 14 <%= render :partial => partial_for_class(@article.class), :locals => { :f => f } %>
13 15
14 <% if environment.is_portal_community?(profile) %> 16 <% if environment.is_portal_community?(profile) %>
test/functional/cms_controller_test.rb
@@ -1688,6 +1688,17 @@ class CmsControllerTest &lt; ActionController::TestCase @@ -1688,6 +1688,17 @@ class CmsControllerTest &lt; ActionController::TestCase
1688 :attributes => { :value => article.id.to_s }} 1688 :attributes => { :value => article.id.to_s }}
1689 end 1689 end
1690 1690
  1691 + should 'go back to specified url when saving with success' do
  1692 + post :new, :type => 'TinyMceArticle', :profile => profile.identifier, :article => { :name => 'changed by me', :body => 'content ...' }, :success_back_to => '/'
  1693 + assert_redirected_to '/'
  1694 + end
  1695 +
  1696 + should 'redirect back to specified url when edit with success' do
  1697 + article = @profile.articles.create!(:name => 'myarticle')
  1698 + post :edit, :profile => 'testinguser', :id => article.id, :success_back_to => '/'
  1699 + assert_redirected_to '/'
  1700 + end
  1701 +
1691 protected 1702 protected
1692 1703
1693 # FIXME this is to avoid adding an extra dependency for a proper JSON parser. 1704 # FIXME this is to avoid adding an extra dependency for a proper JSON parser.