-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathworkshops_controller.rb
More file actions
294 lines (253 loc) · 9.16 KB
/
workshops_controller.rb
File metadata and controls
294 lines (253 loc) · 9.16 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
class WorkshopsController < ApplicationController
include AhoyTracking, TagAssignable
skip_before_action :authenticate_user!, only: [ :index, :show ]
def index
authorize!
@category_types = CategoryType.published.general.order(:name).decorate
@sectors = Sector.published.order(:name)
@windows_types = WindowsType.all
if turbo_frame_request?
search_service = WorkshopSearchService.new(params, user: current_user).call
@sort = search_service.sort
track_index_intent(Workshop, search_service.workshops, params)
@workshops = authorized_scope(search_service.workshops
.includes(:categories, :windows_type, :bookmarks,
created_by: [ :person ], primary_asset: [ :file_attachment ]))
.paginate(page: params[:page], per_page: params[:per_page] || 12)
render :workshop_results
else
@sort = params[:sort].presence || "title"
render :index
end
end
def summary
authorize! :workshop, to: :summary?
@year = params[:year] ? params[:year].to_i : Date.current.year.to_i
@month = params[:month] ? params[:month].to_i : Date.current.month.to_i
reports = build_report
@report = reports[0]
types = reports.map do |r|
r.windows_type
end
@workshop_logs = current_user.organization_monthly_workshop_logs(
reports.first.date, *types,
)
logs = @workshop_logs.map { |_k, v| v }.flatten
@total_ongoing = logs.reduce(0) { |sum, l| sum += l.num_ongoing }
@total_first_time = logs.reduce(0) { |sum, l| sum += l.num_first_time }
combined_windows_type = WindowsType.where(short_name: "COMBINED").first
@combined_workshop_logs = current_user.organization_workshop_logs(
@report.date, combined_windows_type, current_user.agency_id
)
authorize! @combined_workshop_logs
end
def build_report
authorize! :workshop, to: :summary?
date = Date.new(@year, @month)
form_builder = FormBuilder
.monthly
report = form_builder.map do |f|
Report.new(
type: f.report_type,
windows_type: f.windows_type,
date: date
)
end
report.each do |r|
r.media_files.build
end
report
end
def new
if params[:workshop_idea_id].present?
@workshop_idea = WorkshopIdea.find(params[:workshop_idea_id])
@workshop = WorkshopFromIdeaService.new(@workshop_idea, user: current_user).call
authorize! @workshop
else
@workshop = Workshop.new(created_by: current_user)
authorize! @workshop
end
set_form_variables
end
def create
@workshop = current_user.workshops.build(workshop_params)
authorize! @workshop
success = false
Workshop.transaction do
if @workshop.save
assign_associations(@workshop)
if params[:promote_idea_assets] == "true"
@workshop.attach_assets_from_idea!
end
success = true
end
rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotSaved, ActiveRecord::RecordNotUnique => e
log_workshop_error("creation", e)
raise ActiveRecord::Rollback
end
if success
flash[:notice] = "Workshop created successfully."
redirect_to @workshop
else
set_form_variables
flash.now[:alert] = "Unable to save the workshop."
render :new
end
end
def edit
@workshop = Workshop.find(params[:id])
authorize! @workshop
set_form_variables
if turbo_frame_request?
render :editor_lazy
else
render :edit
end
end
def show
if turbo_frame_request?
@workshop = Workshop.find(params[:id]).decorate
authorize! @workshop
set_show
render partial: "show_lazy", locals: { workshop: @workshop }
else
@workshop = Workshop.find(params[:id]).decorate
authorize! @workshop
track_view(@workshop)
render :show
end
end
def destroy
@workshop = Workshop.find(params[:id])
authorize! @workshop
@workshop.destroy!
redirect_to workshops_path, notice: "Workshop was successfully destroyed."
end
def update
@workshop = Workshop.find(params[:id])
authorize! @workshop
success = false
Workshop.transaction do
if @workshop.update(workshop_params)
assign_associations(@workshop)
success = true
end
rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotSaved, ActiveRecord::RecordNotUnique => e
log_workshop_error("update", e)
raise ActiveRecord::Rollback
end
if success
flash[:notice] = "Workshop updated successfully."
redirect_to @workshop
else
set_form_variables
flash[:alert] = "Unable to update the workshop."
render :edit
end
end
private
def set_show
@quotes = Quote.where(workshop_id: @workshop.id).published
@leader_spotlights = @workshop.associated_resources.leader_spotlights.where(published: true)
@workshop_variations = authorized_scope(@workshop.workshop_variations)
.includes(:windows_type, :created_by, primary_asset: [ :file_attachment ])
.order(created_at: :desc)
@sectors = @workshop.sectorable_items.map { |item| item.sector if item.sector.published? }.compact if @workshop.sectorable_items.any?
@mentions = @workshop.all_mentions_grouped
end
def set_form_variables
@age_ranges = Category.includes(:category_type)
.where("category_types.name = 'AgeRange'").pluck(:name)
potential_series = authorized_scope(Workshop.published).includes(:windows_type)
potential_series = potential_series.where.not(id: @workshop.id) if @workshop.persisted?
@potential_series_workshops = authorized_scope(potential_series).order(:title)
@windows_types = WindowsType.all
@workshop_ideas = authorized_scope(WorkshopIdea.order(created_at: :desc))
.map { |wi|
[ "#{wi.created_at.strftime("%Y-%m-%d")
} - (#{wi.created_by.full_name}): #{wi.title}", wi.id ] }
@categories_grouped =
Category
.includes(:category_type)
.published
.order(:position, :name)
.group_by(&:category_type)
.select { |type, _| type.nil? || (type.published? && !type.story_specific? && !type.profile_specific?) }
.sort_by { |type, _| type&.name.to_s.downcase }
@sectors = Sector.published.order(:name)
@workshop.build_primary_asset if @workshop.primary_asset.blank?
@workshop.gallery_assets.build
end
def log_workshop_error(action, error)
Rails.logger.error "Workshop #{action} failed: #{error.class} - #{error.message}\n#{error.backtrace.join("\n")}"
end
def workshop_params
params.require(:workshop).permit(
:title, :title_spanish, :featured, :published,
:full_name, :created_by_id, :windows_type_id, :workshop_idea_id, :author_credit_preference,
:month, :year,
:publicly_visible,
:publicly_featured,
:time_intro, :time_closing, :time_creation, :time_demonstration,
:time_warm_up, :time_opening, :time_opening_circle,
:age_range, :age_range_spanish,
:closing, :closing_spanish,
:creation, :creation_spanish,
:demonstration, :demonstration_spanish,
:extra_field, :extra_field_spanish,
:introduction, :introduction_spanish,
:materials, :materials_spanish,
:misc1, :misc1_spanish,
:misc2, :misc2_spanish,
:notes, :notes_spanish,
:objective, :objective_spanish,
:opening_circle, :opening_circle_spanish,
:optional_materials, :optional_materials_spanish,
:setup, :setup_spanish,
:tips, :tips_spanish,
:timeframe_spanish,
:visualization, :visualization_spanish,
:warm_up, :warm_up_spanish,
:rhino_objective,
:rhino_materials,
:rhino_optional_materials,
:rhino_setup,
:rhino_introduction,
:rhino_opening_circle,
:rhino_demonstration,
:rhino_warm_up,
:rhino_visualization,
:rhino_creation,
:rhino_closing,
:rhino_notes,
:rhino_tips,
:rhino_misc1,
:rhino_misc2,
:rhino_extra_field,
:rhino_objective_spanish,
:rhino_materials_spanish,
:rhino_optional_materials_spanish,
:rhino_age_range_spanish,
:rhino_setup_spanish,
:rhino_introduction_spanish,
:rhino_opening_circle_spanish,
:rhino_demonstration_spanish,
:rhino_warm_up_spanish,
:rhino_visualization_spanish,
:rhino_creation_spanish,
:rhino_closing_spanish,
:rhino_notes_spanish,
:rhino_tips_spanish,
:rhino_misc1_spanish,
:rhino_misc2_spanish,
:rhino_extra_field_spanish,
category_ids: [],
sector_ids: [],
primary_asset_attributes: [ :id, :file, :_destroy ],
gallery_assets_attributes: [ :id, :file, :_destroy ],
workshop_series_children_attributes: [ :id, :workshop_child_id, :workshop_parent_id, :theme_name,
:series_description, :series_description_spanish,
:position, :_destroy ]
)
end
end