/
base_controller.rb
90 lines (70 loc) · 2.8 KB
/
base_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
class Admin::BaseController < ApplicationController
layout "admin"
renders_with_error_proc :below_field
include CacheableFlash
include Widgets
before_filter :set_site, :set_locale, :set_timezone
helper :base, :content, :comments, :users
helper_method :admin_section_path_for
authentication_required
attr_accessor :site
widget :menu_global, :partial => 'widgets/admin/menu_global'
widget :menu_site, :partial => 'widgets/admin/menu_site',
:except => { :controller => 'admin/sites', :action => [:index, :new] }
widget :section_tree, :partial => 'widgets/admin/section_tree',
:except => { :controller => 'admin/sites', :action => [:index, :new] },
:only => { :controller => ['admin/sites', 'admin/sections', 'admin/articles', 'admin/wikipages'] }
widget :menu_section, :partial => 'widgets/admin/menu_section',
:except => { :controller => ['admin/sections'], :action => [:index, :new] },
:only => { :controller => ['admin/sections', 'admin/articles', 'admin/wikipages', 'admin/categories', 'admin/comments'] }
# TODO delegate this to the section class? or the controller, even?
# like Admin::WikipagesController.default_route_helper
def admin_section_path_for(section)
case section
when Wiki then admin_wikipages_path section.site, section
when Blog then admin_articles_path section.site, section
when Forum then admin_section_path section.site, section
when Section then admin_articles_path section.site, section
# else admin_articles_path section.site, section
end
end
protected
def require_authentication
unless current_user and current_user.has_role?(Role.build(:admin, @site))
return redirect_to_login("You need to be an admin to view this page.")
end
super
end
def redirect_to_login(notice = nil)
store_return_location
flash[:notice] = notice
redirect_to login_path
end
def rescue_action(exception)
if exception.is_a? ActionController::RoleRequired
@error = exception
render :template => 'shared/messages/insufficient_permissions'
else
super
end
end
def current_page
@page ||= params[:page].blank? ? 1 : params[:page].to_i
end
def set_locale
I18n.locale = params[:locale] || I18n.default_locale
end
def set_timezone
Time.zone = @site.timezone if @site
end
def set_site
@site = Site.find(params[:site_id])
Thread.current[:site] = @site
end
def set_section
@section = @site.sections.find(params[:section_id]) if params[:section_id]
end
def current_role_context
@section || @site || Site.new
end
end