From 1909edbe2a817caebad86fee874d90b5cfb75f9e Mon Sep 17 00:00:00 2001 From: chris2tof Date: Tue, 19 Apr 2011 15:05:48 +0800 Subject: [PATCH] Simplification of the 'link_back' code --- app/controllers/admin/assets_controller.rb | 2 -- app/controllers/admin/homes_controller.rb | 2 -- app/controllers/admin/layouts_controller.rb | 2 -- app/controllers/admin/links_controller.rb | 2 -- app/controllers/admin/pages_controller.rb | 2 -- app/controllers/admin/snippets_controller.rb | 2 -- .../admin/translations_controller.rb | 1 - .../admin/user_info_models_controller.rb | 2 -- .../admin/user_role_models_controller.rb | 2 -- app/controllers/application_controller.rb | 22 ------------------ app/controllers/panel/users_controller.rb | 2 -- app/helpers/application_helper.rb | 23 ++++++++++++++++++- .../app/views/panel/blog/posts/show.html.erb | 3 +-- 13 files changed, 23 insertions(+), 44 deletions(-) diff --git a/app/controllers/admin/assets_controller.rb b/app/controllers/admin/assets_controller.rb index 698f6f39c..3dcd08240 100644 --- a/app/controllers/admin/assets_controller.rb +++ b/app/controllers/admin/assets_controller.rb @@ -14,12 +14,10 @@ class Admin::AssetsController < ApplicationController def edit @asset = Asset.find(params[:id]) - session[:last_page] = get_go_back || admin_assets_url end def new @asset = Asset.new - session[:last_page] = get_go_back || admin_assets_url end def create diff --git a/app/controllers/admin/homes_controller.rb b/app/controllers/admin/homes_controller.rb index 58af4434e..fdfc855fa 100644 --- a/app/controllers/admin/homes_controller.rb +++ b/app/controllers/admin/homes_controller.rb @@ -12,13 +12,11 @@ class Admin::HomesController < ApplicationController @home = Home.new @home.is_published = true @home.parent_id = nil - session[:last_page] = get_go_back || admin_items_url end def edit @home = Home.find(params[:id]) @i18n_variable = @home.i18n_variable - session[:last_page] = get_go_back || admin_items_url end def create diff --git a/app/controllers/admin/layouts_controller.rb b/app/controllers/admin/layouts_controller.rb index 1f00ded50..6661720c2 100644 --- a/app/controllers/admin/layouts_controller.rb +++ b/app/controllers/admin/layouts_controller.rb @@ -16,12 +16,10 @@ class Admin::LayoutsController < ApplicationController def new @layout = Layout.new - session[:last_page] = get_go_back || admin_layouts_url end def edit @layout = Layout.find(params[:id]) - session[:last_page] = get_go_back || admin_layouts_url end def create diff --git a/app/controllers/admin/links_controller.rb b/app/controllers/admin/links_controller.rb index e95b8d264..88ece1bb9 100644 --- a/app/controllers/admin/links_controller.rb +++ b/app/controllers/admin/links_controller.rb @@ -13,13 +13,11 @@ class Admin::LinksController < ApplicationController @link = Link.new @link.is_published = true @link.parent_id = @parent_item.id rescue nil - session[:last_page] = get_go_back || admin_links_url end def edit @link = Link.find(params[:id]) @i18n_variable = @link.i18n_variable - session[:last_page] = get_go_back || admin_links_url end def create diff --git a/app/controllers/admin/pages_controller.rb b/app/controllers/admin/pages_controller.rb index eacf9053b..5fa4cc66a 100644 --- a/app/controllers/admin/pages_controller.rb +++ b/app/controllers/admin/pages_controller.rb @@ -13,14 +13,12 @@ class Admin::PagesController < ApplicationController @page = Page.new @page.is_published = true @page.parent_id = @parent_item.id rescue nil - session[:last_page] = get_go_back || admin_items_url end def edit @page = Page.find(params[:id]) @page.content = parse_content(@page.content, {:locale => 'show'}) @i18n_variable = @page.i18n_variable - session[:last_page] = get_go_back || admin_items_url end def create diff --git a/app/controllers/admin/snippets_controller.rb b/app/controllers/admin/snippets_controller.rb index 29da19996..7e7e44135 100644 --- a/app/controllers/admin/snippets_controller.rb +++ b/app/controllers/admin/snippets_controller.rb @@ -12,12 +12,10 @@ class Admin::SnippetsController < ApplicationController def new @snippet = Snippet.new @snippet.parent_id = @parent_item.id - session[:last_page] = get_go_back || admin_items_url end def edit @snippet = Snippet.find(params[:id]) - session[:last_page] = get_go_back || admin_items_url end def create diff --git a/app/controllers/admin/translations_controller.rb b/app/controllers/admin/translations_controller.rb index 21ec22658..590ad5567 100644 --- a/app/controllers/admin/translations_controller.rb +++ b/app/controllers/admin/translations_controller.rb @@ -10,7 +10,6 @@ class Admin::TranslationsController < ApplicationController def edit set_variables - session[:last_page] = get_go_back || admin_translations_url end # Update either the i18n_variables or the languages diff --git a/app/controllers/admin/user_info_models_controller.rb b/app/controllers/admin/user_info_models_controller.rb index 6deafdd51..5c9c06132 100644 --- a/app/controllers/admin/user_info_models_controller.rb +++ b/app/controllers/admin/user_info_models_controller.rb @@ -16,14 +16,12 @@ class Admin::UserInfoModelsController < ApplicationController def new @user_attribute_model = UserInfoModel.new - session[:last_page] = get_go_back || admin_user_info_models_url render :template => 'admin/user_attribute_models/new' end def edit @user_attribute_model = UserInfoModel.find(params[:id]) @i18n_variable = @user_attribute_model.i18n_variable - session[:last_page] = get_go_back || admin_user_info_models_url render :template => 'admin/user_attribute_models/edit' end diff --git a/app/controllers/admin/user_role_models_controller.rb b/app/controllers/admin/user_role_models_controller.rb index f4f71cab3..90f2070db 100644 --- a/app/controllers/admin/user_role_models_controller.rb +++ b/app/controllers/admin/user_role_models_controller.rb @@ -16,14 +16,12 @@ class Admin::UserRoleModelsController < ApplicationController def new @user_attribute_model = UserRoleModel.new - session[:last_page] = get_go_back || admin_user_role_models_url render :template => 'admin/user_attribute_models/new' end def edit @user_attribute_model = UserRoleModel.find(params[:id]) @i18n_variable = @user_attribute_model.i18n_variable - session[:last_page] = get_go_back || admin_user_role_models_url render :template => 'admin/user_attribute_models/edit' end diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 9a32345fa..ee8568b58 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -11,28 +11,6 @@ class ApplicationController < ActionController::Base @parent_item = Item.first(:conditions => { :id => BSON::ObjectId(params[:parent_id]) }) rescue nil end - - # Clean the link back - # Remove the locale but keep all the other params - def get_go_back - begin - if env['REQUEST_URI'].include?('locale=') - session[:last_page] - else - target = env['HTTP_REFERER'].split('?') - vars = target[1].split('&') - vars.delete_if {|var| var.include? 'locale=' } - if vars.size > 0 - target[0].to_s + '?' + vars.join('&') - else - target[0].to_s - end - end - rescue - nil - end - end - # Check if the current_user is admin def is_admin? redirect_to root_url unless current_user.admin? diff --git a/app/controllers/panel/users_controller.rb b/app/controllers/panel/users_controller.rb index b7b9c6882..b74263a2d 100644 --- a/app/controllers/panel/users_controller.rb +++ b/app/controllers/panel/users_controller.rb @@ -16,7 +16,6 @@ class Panel::UsersController < ApplicationController @user = User.new @user_info_models = UserInfoModel.all.entries @user_role_models = UserRoleModel.all.entries - session[:last_page] = get_go_back || panel_users_url end def create @@ -33,7 +32,6 @@ class Panel::UsersController < ApplicationController @user = User.find(params[:id]) @user_info_models = UserInfoModel.all.entries @user_role_models = UserRoleModel.all.entries - session[:last_page] = get_go_back || panel_users_url end def update diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 2e6db4546..6b10d9421 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -17,7 +17,28 @@ module ApplicationHelper end def link_back - link_to t('back'), session[:last_page] + link_to t('back'), get_go_back + end + + # Clean the link back + # Remove the locale but keep all the other params + def get_go_back + begin + if request.request_uri.include?('locale=') + session[:last_page] + else + target = request.referer.split('?') + vars = target[1].split('&') rescue [] + vars.delete_if {|var| var.include? 'locale=' } + session[:last_page] = if vars.size > 0 + target[0].to_s + '?' + vars.join('&') + else + target[0].to_s + end + end + rescue + eval(params[:controller].split('/').join('_') << '_url') + end end def message_for_item(message, item = nil) diff --git a/vendor/built_in_modules/blog/app/views/panel/blog/posts/show.html.erb b/vendor/built_in_modules/blog/app/views/panel/blog/posts/show.html.erb index e41e62538..7fce5fd16 100644 --- a/vendor/built_in_modules/blog/app/views/panel/blog/posts/show.html.erb +++ b/vendor/built_in_modules/blog/app/views/panel/blog/posts/show.html.erb @@ -35,5 +35,4 @@ <%= link_to t('blog.edit'), edit_panel_blog_post_path(@post) %> | -<%= link_back %> -<%= session[:last_page].inspect %> \ No newline at end of file +<%= link_back %> \ No newline at end of file