Simplification of the 'link_back' code

This commit is contained in:
chris2tof 2011-04-19 15:05:48 +08:00
parent d88cae437d
commit 1909edbe2a
13 changed files with 23 additions and 44 deletions

View File

@ -14,12 +14,10 @@ class Admin::AssetsController < ApplicationController
def edit def edit
@asset = Asset.find(params[:id]) @asset = Asset.find(params[:id])
session[:last_page] = get_go_back || admin_assets_url
end end
def new def new
@asset = Asset.new @asset = Asset.new
session[:last_page] = get_go_back || admin_assets_url
end end
def create def create

View File

@ -12,13 +12,11 @@ class Admin::HomesController < ApplicationController
@home = Home.new @home = Home.new
@home.is_published = true @home.is_published = true
@home.parent_id = nil @home.parent_id = nil
session[:last_page] = get_go_back || admin_items_url
end end
def edit def edit
@home = Home.find(params[:id]) @home = Home.find(params[:id])
@i18n_variable = @home.i18n_variable @i18n_variable = @home.i18n_variable
session[:last_page] = get_go_back || admin_items_url
end end
def create def create

View File

@ -16,12 +16,10 @@ class Admin::LayoutsController < ApplicationController
def new def new
@layout = Layout.new @layout = Layout.new
session[:last_page] = get_go_back || admin_layouts_url
end end
def edit def edit
@layout = Layout.find(params[:id]) @layout = Layout.find(params[:id])
session[:last_page] = get_go_back || admin_layouts_url
end end
def create def create

View File

@ -13,13 +13,11 @@ class Admin::LinksController < ApplicationController
@link = Link.new @link = Link.new
@link.is_published = true @link.is_published = true
@link.parent_id = @parent_item.id rescue nil @link.parent_id = @parent_item.id rescue nil
session[:last_page] = get_go_back || admin_links_url
end end
def edit def edit
@link = Link.find(params[:id]) @link = Link.find(params[:id])
@i18n_variable = @link.i18n_variable @i18n_variable = @link.i18n_variable
session[:last_page] = get_go_back || admin_links_url
end end
def create def create

View File

@ -13,14 +13,12 @@ class Admin::PagesController < ApplicationController
@page = Page.new @page = Page.new
@page.is_published = true @page.is_published = true
@page.parent_id = @parent_item.id rescue nil @page.parent_id = @parent_item.id rescue nil
session[:last_page] = get_go_back || admin_items_url
end end
def edit def edit
@page = Page.find(params[:id]) @page = Page.find(params[:id])
@page.content = parse_content(@page.content, {:locale => 'show'}) @page.content = parse_content(@page.content, {:locale => 'show'})
@i18n_variable = @page.i18n_variable @i18n_variable = @page.i18n_variable
session[:last_page] = get_go_back || admin_items_url
end end
def create def create

View File

@ -12,12 +12,10 @@ class Admin::SnippetsController < ApplicationController
def new def new
@snippet = Snippet.new @snippet = Snippet.new
@snippet.parent_id = @parent_item.id @snippet.parent_id = @parent_item.id
session[:last_page] = get_go_back || admin_items_url
end end
def edit def edit
@snippet = Snippet.find(params[:id]) @snippet = Snippet.find(params[:id])
session[:last_page] = get_go_back || admin_items_url
end end
def create def create

View File

@ -10,7 +10,6 @@ class Admin::TranslationsController < ApplicationController
def edit def edit
set_variables set_variables
session[:last_page] = get_go_back || admin_translations_url
end end
# Update either the i18n_variables or the languages # Update either the i18n_variables or the languages

View File

@ -16,14 +16,12 @@ class Admin::UserInfoModelsController < ApplicationController
def new def new
@user_attribute_model = UserInfoModel.new @user_attribute_model = UserInfoModel.new
session[:last_page] = get_go_back || admin_user_info_models_url
render :template => 'admin/user_attribute_models/new' render :template => 'admin/user_attribute_models/new'
end end
def edit def edit
@user_attribute_model = UserInfoModel.find(params[:id]) @user_attribute_model = UserInfoModel.find(params[:id])
@i18n_variable = @user_attribute_model.i18n_variable @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' render :template => 'admin/user_attribute_models/edit'
end end

View File

@ -16,14 +16,12 @@ class Admin::UserRoleModelsController < ApplicationController
def new def new
@user_attribute_model = UserRoleModel.new @user_attribute_model = UserRoleModel.new
session[:last_page] = get_go_back || admin_user_role_models_url
render :template => 'admin/user_attribute_models/new' render :template => 'admin/user_attribute_models/new'
end end
def edit def edit
@user_attribute_model = UserRoleModel.find(params[:id]) @user_attribute_model = UserRoleModel.find(params[:id])
@i18n_variable = @user_attribute_model.i18n_variable @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' render :template => 'admin/user_attribute_models/edit'
end end

View File

@ -11,28 +11,6 @@ class ApplicationController < ActionController::Base
@parent_item = Item.first(:conditions => { :id => BSON::ObjectId(params[:parent_id]) }) rescue nil @parent_item = Item.first(:conditions => { :id => BSON::ObjectId(params[:parent_id]) }) rescue nil
end 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 # Check if the current_user is admin
def is_admin? def is_admin?
redirect_to root_url unless current_user.admin? redirect_to root_url unless current_user.admin?

View File

@ -16,7 +16,6 @@ class Panel::UsersController < ApplicationController
@user = User.new @user = User.new
@user_info_models = UserInfoModel.all.entries @user_info_models = UserInfoModel.all.entries
@user_role_models = UserRoleModel.all.entries @user_role_models = UserRoleModel.all.entries
session[:last_page] = get_go_back || panel_users_url
end end
def create def create
@ -33,7 +32,6 @@ class Panel::UsersController < ApplicationController
@user = User.find(params[:id]) @user = User.find(params[:id])
@user_info_models = UserInfoModel.all.entries @user_info_models = UserInfoModel.all.entries
@user_role_models = UserRoleModel.all.entries @user_role_models = UserRoleModel.all.entries
session[:last_page] = get_go_back || panel_users_url
end end
def update def update

View File

@ -17,7 +17,28 @@ module ApplicationHelper
end end
def link_back 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 end
def message_for_item(message, item = nil) def message_for_item(message, item = nil)

View File

@ -36,4 +36,3 @@
<%= link_to t('blog.edit'), edit_panel_blog_post_path(@post) %> | <%= link_to t('blog.edit'), edit_panel_blog_post_path(@post) %> |
<%= link_back %> <%= link_back %>
<%= session[:last_page].inspect %>