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
@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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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?

View File

@ -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

View File

@ -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)

View File

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