-
Notifications
You must be signed in to change notification settings - Fork 163
Expand file tree
/
Copy pathapplication_controller.rb
More file actions
213 lines (175 loc) · 5.57 KB
/
application_controller.rb
File metadata and controls
213 lines (175 loc) · 5.57 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
class ApplicationController < ActionController::Base
include Pundit
include ActivateNavigation
rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized
require "csv"
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
helper_method :current_event
helper_method :current_website
helper_method :display_staff_event_subnav?
helper_method :display_staff_selection_subnav?
helper_method :display_staff_program_subnav?
helper_method :display_website_subnav?
helper_method :program_mode?
helper_method :schedule_mode?
helper_method :program_tracks
before_action :set_paper_trail_whodunnit
before_action :current_event
before_action :configure_permitted_parameters, if: :devise_controller?
layout 'application'
decorates_assigned :event
def after_sign_in_path_for(user)
if session[:pending_invite_accept_url]
session[:pending_invite_accept_url]
elsif !user.complete?
edit_profile_path
elsif request.referrer.present? && request.referrer != new_user_session_url
request.referrer
elsif session[:target]
session.delete(:target)
elsif user.staff_for?(current_event)
event_staff_path(current_event)
elsif user.proposals.any?
proposals_path
elsif user.admin?
admin_events_path
elsif current_event
event_path(current_event)
else
root_path
end
end
private
def current_event
@current_event ||= set_current_event(session[:current_event_id]) if session[:current_event_id]
end
def current_website
@current_website ||= begin
if current_event
current_event.website
elsif params[:slug]
Website.joins(:event).find_by(events: { slug: params[:slug] })
else
older_domain_website || latest_domain_website
end
end&.decorate
end
def older_domain_website
@older_domain_website ||=
domain_websites.find_by(events: { slug: params[:domain_page_or_slug] })
end
def latest_domain_website
@latest_domain_website ||= domain_websites.first
end
def domain_websites
Website.domain_match(request.domain).joins(:event).order(created_at: :desc)
end
def set_current_event(event_id)
@current_event = Event.find_by(id: event_id).try(:decorate)
session[:current_event_id] = @current_event.try(:id)
@current_event
end
def pundit_user
@pundit_user ||= CurrentEventContext.new(current_user, current_event)
end
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:pending_invite_email])
end
def event_staff?(event)
if event && current_user
event.teammates.where(user_id: current_user.id).any?
end
end
def require_user
unless user_signed_in?
session[:target] = request.path
flash[:danger] = "You must be signed in to access this page. If you haven't created an account, please create one."
redirect_to new_user_session_url
end
end
def require_event
@event = Event.find_by(slug: params[:event_slug] || params[:slug])
if @event
set_current_event(event.id)
else
flash[:danger] = "Your event could not be found, please check the url."
redirect_to events_path
end
end
def require_proposal
@proposal = @event.proposals.find_by!(uuid: params[:proposal_uuid] || params[:uuid])
end
def require_website
redirect_to not_found_path and return unless current_website
end
def user_not_authorized
flash[:alert] = "You are not authorized to perform this action."
redirect_to(request.referrer || root_path)
end
def event_params
params.require(:event).permit(
:name, :contact_email, :slug, :url, :valid_proposal_tags,
:valid_review_tags, :custom_fields_string, :state, :guidelines,
:closes_at, :speaker_notification_emails, :accept, :reject,
:waitlist, :opens_at, :start_date, :end_date)
end
def render_json(object, options={})
send_data(render_to_string(json: object), options)
end
def set_title(title)
@title = title[0..25] if title
end
def enable_staff_event_subnav
@display_staff_subnav = true
end
def display_staff_event_subnav?
@display_staff_subnav
end
def enable_staff_selection_subnav
@display_selection_subnav = true
end
def display_staff_selection_subnav?
@display_selection_subnav
end
def enable_staff_program_subnav
@display_program_subnav = true
end
def display_staff_program_subnav?
@display_program_subnav
end
def enable_staff_schedule_subnav
@display_schedule_subnav = true
end
def display_website_subnav?
@display_website_subnav
end
def enable_website_subnav
@display_website_subnav = true
end
def program_mode?
@display_program_subnav || @display_selection_subnav
end
def schedule_mode?
@display_schedule_subnav
end
def program_tracks
@program_tracks ||= current_event && current_event.tracks.any? ? current_event.tracks : []
end
def set_cache_headers
return unless Rails.configuration.action_controller.perform_caching
server_cache_age =
current_website.caching_off? ? 0 : ENV.fetch('CACHE_CONTROL_S_MAXAGE', 1.week)
expires_in(
ENV.fetch('CACHE_CONTROL_MAX_AGE', 0).to_i,
public: !current_website.caching_off?,
's-maxage': server_cache_age.to_i
)
response.headers['Surrogate-Key'] = current_website.event.slug if FastlyService.service
fresh_when(
current_website,
last_modified: current_website.purged_at || current_website.updated_at
) unless current_website.caching_off?
end
end