forked from snap-cloud/snapcon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommercials_controller.rb
96 lines (81 loc) · 2.95 KB
/
commercials_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
# frozen_string_literal: true
module Admin
class CommercialsController < Admin::BaseController
load_and_authorize_resource :conference, find_by: :short_title
load_and_authorize_resource through: :conference, except: %i[new create]
def index
@commercials = @conference.commercials
@commercial = @conference.commercials.build
end
def create
@commercial = @conference.commercials.build(commercial_params)
authorize! :create, @commercial
if @commercial.save
redirect_to admin_conference_commercials_path,
notice: 'Materials were successfully created.'
else
redirect_to admin_conference_commercials_path,
error: 'An error prohibited materials from being saved: ' \
"#{@commercial.errors.full_messages.join('. ')}."
end
end
def update
if @commercial.update(commercial_params)
redirect_to admin_conference_commercials_path,
notice: 'Materials were successfully updated.'
else
redirect_to admin_conference_commercials_path,
error: 'An error prohibited materials from being saved: ' \
"#{@commercial.errors.full_messages.join('. ')}."
end
end
def destroy
@commercial.destroy
redirect_to admin_conference_commercials_path, notice: 'Materials were successfully removed.'
end
def render_commercial
result = Commercial.render_from_url(params[:url])
if result[:error]
render plain: result[:error], status: :bad_request
else
render plain: result[:html]
end
end
##
# Received a file from user
# Reads file and creates commercial for event
# File content example:
# EventID, Title, URL
def mass_upload
errors = Commercial.read_file(params[:file]) if params[:file]
if !params[:file]
flash[:error] = 'Empty file detected while adding materials to Event'
elsif errors.present?
errors_text = aggregate_errors(errors)
flash[:notice] = if errors_text.length > 4096
'Errors are too long to be displayed. Please check the logs.'
else
errors_text
end
else
flash[:notice] = 'Successfully added materials.'
end
redirect_back(fallback_location: root_path)
end
private
# Aggregate errors and ensure that they do not exceed 4 KB in total size
def aggregate_errors(errors)
errors_text = ''
if errors[:no_event].any?
errors_text += 'Unable to find events with IDs: ' + errors[:no_event].join(', ') + '. '
end
if errors[:validation_errors].any?
errors_text += 'Validation errors: ' + errors[:validation_errors].join('. ')
end
errors_text
end
def commercial_params
params.require(:commercial).permit(:title, :url)
end
end
end