-
Notifications
You must be signed in to change notification settings - Fork 0
/
application_controller.rb
108 lines (91 loc) · 3.47 KB
/
application_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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
class ApplicationController < ActionController::Base
include ApplicationHelper
include RankingsHelper
before_action :set_locale
before_action :build_navigation
def set_locale
I18n.locale = if user_signed_in?
current_user.locale
else
I18n.default_locale
end
end
def build_navigation
return unless user_signed_in?
@admin_nav = [
{ :name => t('nav.user.admin.upload-session'), :path => new_session_path, :organizer => true },
{ :name => t('nav.user.admin.create-season'), :path => new_season_path },
{ :name => t('nav.user.admin.import-tracks'), :path => new_track_path },
{ :name => t('nav.user.admin.import-cars'), :path => new_car_path },
# { :name => t('nav.user.admin.new-team'), :path => new_team_path },
# { :name => t('nav.user.admin.new-tournament'), :path => new_tournament_path },
{ :name => t('nav.user.admin.import-users'), :path => users_new_path }
]
@nav = [
{ :name => t('nav.user.admin.title'), :path => '', :sub => @admin_nav, :organizer => true },
{ :name => t('nav.user.my-profile'), :path => user_path(current_user.username), :user => true },
{ :name => t('nav.user.settings'), :path => main_app.edit_user_registration_path, :user => true }
]
end
def nav_path(item)
item[:path] || main_app.routes.url_helpers.url_for(
:only_path => true,
:controller => nav_controller(item).controller_path,
:action => nav_action(item)
)
end
def nav_link(item)
%(<a class="dropdown-item" href="#{nav_path(item)}">#{item[:name]}</a>).html_safe
end
def render_navigation(item)
if item[:sub]
subs = item[:sub].map do |sub|
if sub[:user] || user_is_admin? || ((sub[:organizer] && user_is_organizer?) || (sub[:mod] && user_is_mod?))
render_navigation(sub)
end
end
if item[:user] || user_is_admin? || ((item[:organizer] && user_is_organizer?) || (item[:mod] && user_is_mod?))
%(
<li class="dropdown-submenu">
<a class="dropdown-item dropdown-toggle" href="#">#{item[:name]}</a>
<ul class="dropdown-menu">
#{subs.join}
</ul>
</li>
).html_safe
end
else
if item[:user] || user_is_admin? || ((item[:organizer] && user_is_organizer?) || (item[:mod] && user_is_mod?))
%(<li>#{nav_link(item)}</li>).html_safe
end
end
end
helper_method :render_navigation
def index
@current_ranking = Rails.cache.fetch('current_ranking', :expires_in => 1.day) do
current_ranking
end
if @current_ranking.nil?
@recent_sessions = []
return
end
@current_sessions = Rails.cache.fetch('current_sessions', :expires_in => 1.day) do
current_ranking.sessions
end
@recent_sessions = Rails.cache.fetch('recent_sessions', :expires_in => 1.day) do
@current_sessions.last(5).reverse!
end
end
def authenticate_admin
redirect_to root_path, :notice => t('alerts.no-permission') unless user_is_admin?
end
def authenticate_mod
redirect_to root_path, :notice => t('alerts.no-permission') unless user_is_mod? || user_is_admin?
end
def authenticate_organizer
redirect_to root_path, :notice => t('alerts.no-permission') unless user_is_organizer? || user_is_admin?
end
def authenticate_staff
redirect_to root_path, :notice => t('alerts.no-permission') unless user_is_staff?
end
end