-
Notifications
You must be signed in to change notification settings - Fork 488
/
application_helper.rb
204 lines (179 loc) · 7.37 KB
/
application_helper.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
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
module ApplicationHelper
# Returns a string build from the start and end date of the given conference.
#
# If the conference is only one day long
# * %B %d %Y (January 17 2014)
# If the conference starts and ends in the same month and year
# * %B %d - %d, %Y (January 17 - 21 2014)
# If the conference ends in another month but in the same year
# * %B %d - %B %d, %Y (January 31 - February 02 2014)
# All other cases
# * %B %d, %Y - %B %d, %Y (December 30, 2013 - January 02, 2014)
def date_string(start_date, end_date)
startstr = 'Unknown - '
endstr = 'Unknown'
# When the conference is in the same month
if start_date.month == end_date.month && start_date.year == end_date.year
if start_date.day == end_date.day
startstr = start_date.strftime('%B %d')
endstr = end_date.strftime(' %Y')
else
startstr = start_date.strftime('%B %d - ')
endstr = end_date.strftime('%d, %Y')
end
elsif start_date.month != end_date.month && start_date.year == end_date.year
startstr = start_date.strftime('%B %d - ')
endstr = end_date.strftime('%B %d, %Y')
else
startstr = start_date.strftime('%B %d, %Y - ')
endstr = end_date.strftime('%B %d, %Y')
end
result = startstr + endstr
result
end
# Returns time with conference timezone
def time_with_timezone(time)
time.strftime('%F %R') + ' ' + @conference.timezone.to_s
end
# Set resource_name for devise so that we can call the devise help links (views/devise/shared/_links) from anywhere (eg sign_up form in proposals#new)
def resource_name
:user
end
def add_association_link(association_name, form_builder, div_class, html_options = {})
link_to_add_association 'Add ' + association_name.to_s.singularize, form_builder, div_class, html_options.merge(class: 'assoc btn btn-success')
end
def remove_association_link(association_name, form_builder)
link_to_remove_association('Remove ' + association_name.to_s.singularize, form_builder, class: 'assoc btn btn-danger') + tag(:hr)
end
def dynamic_association(association_name, title, form_builder, options = {})
render 'shared/dynamic_association', association_name: association_name, title: title, f: form_builder, hint: options[:hint]
end
def tracks(conference)
conference.confirmed_tracks.collect(&:name).to_sentence
end
def difficulty_levels(conference)
all = conference.program.difficulty_levels.map {|t| t.title}
first = all[0...-1]
last = all[-1]
ts = ''
if all.length > 1
ts << first.join(', ')
ts << " and #{last}"
else
ts = all.join
end
ts
end
def unread_notifications(user)
Comment.accessible_by(current_ability).find_since_last_login(user)
end
# Recieves a PaperTrail::Version object
# Outputs the list of attributes that were changed in the version (ignoring changes from one blank value to another)
# Eg: If version.changeset = '{"title"=>[nil, "Premium"], "description"=>[nil, "Premium = Super cool"], "conference_id"=>[nil, 3]}'
# Output will be 'title, description and conference'
def updated_attributes(version)
version.changeset
.reject{ |_, values| values[0].blank? && values[1].blank? }
.keys.map{ |key| key.gsub('_id', '').tr('_', ' ')}.join(', ')
.reverse.sub(',', ' dna ').reverse
end
def normalize_array_length(hashmap, length)
hashmap.each_value do |value|
if value.length < length
value.fill(value[-1], value.length...length)
end
end
end
# Same as redirect_to(:back) if there is a valid HTTP referer, otherwise redirect_to()
def redirect_back_or_to(options = {}, response_status = {})
if request.env['HTTP_REFERER']
redirect_to :back
else
redirect_to options, response_status
end
end
def concurrent_events(event)
return nil unless event.scheduled? && event.program.selected_event_schedules
event_schedule = event.program.selected_event_schedules.find { |es| es.event == event }
other_event_schedules = event.program.selected_event_schedules.reject { |other_event_schedule| other_event_schedule == event_schedule }
concurrent_events = []
event_time_range = (event_schedule.start_time.strftime '%Y-%m-%d %H:%M')...(event_schedule.end_time.strftime '%Y-%m-%d %H:%M')
other_event_schedules.each do |other_event_schedule|
next unless other_event_schedule.event.confirmed?
other_event_time_range = (other_event_schedule.start_time.strftime '%Y-%m-%d %H:%M')...(other_event_schedule.end_time.strftime '%Y-%m-%d %H:%M')
if (event_time_range.to_a & other_event_time_range.to_a).present?
concurrent_events << other_event_schedule.event
end
end
concurrent_events
end
def speaker_links(event)
safe_join(event.speakers.map{ |speaker| link_to speaker.name, admin_user_path(speaker) }, ',')
end
def speaker_selector_input(form, conference)
users = conference.participants.pluck(:id, :username).map do |user|
[user[0], user[1]]
end
users = users.sort_by { |user| user[1].downcase }
form.input :speakers, as: :select,
collection: options_for_select(users.map {|user| [user[1], user[0]]}, @event.speakers.map(&:id)),
include_blank: false, label: 'Speakers', input_html: { class: 'select-help-toggle', multiple: 'true' }
end
def responsibles_selector_input(form)
users = User.active.pluck(:id, :name, :username, :email).map { |user| [user[0], user[1].blank? ? user[2] : user[1], user[2], user[3]] }.sort_by { |user| user[1].downcase }
form.input :responsibles, as: :select,
collection: options_for_select(users.map {|user| ["#{user[1]} (#{user[2]}) #{user[3]}", user[0]]}, @booth.responsibles.map(&:id)),
include_blank: false, label: 'Responsibles', input_html: { class: 'select-help-toggle', multiple: 'true' },
hint: 'The people responsible for the booth. You can only select existing users.'
end
def event_types_sentence(conference)
conference.event_types.map { |et| et.title.pluralize }.to_sentence
end
def sign_in_path
if ENV['OSEM_ICHAIN_ENABLED'] == 'true'
new_user_ichain_session_path
else
new_user_session_path
end
end
def rescheduling_hint(affected_event_count)
if affected_event_count > 0
"You have #{affected_event_count} scheduled #{'event'.pluralize(affected_event_count)}. Changing the conference hours will unschedule those scheduled outside the conference hours."
end
end
##
# ====Gets
# a conference object
# ==== Returns
# class hidden if conference is over
def hidden_if_conference_over(conference)
'hidden' if Date.today > conference.end_date
end
def nav_root_link_for(conference)
link_text = (
conference.try(:organization).try(:name) ||
ENV['OSEM_NAME'] ||
'OSEM'
)
link_to(
link_text,
root_path,
class: 'navbar-brand',
title: 'Open Source Event Manager'
)
end
# returns the url to be used for logo on basis of sponsorship level position
def get_logo(object)
if object.try(:sponsorship_level)
if object.sponsorship_level.position == 1
object.picture.first.url
elsif object.sponsorship_level.position == 2
object.picture.second.url
else
object.picture.others.url
end
else
object.picture.large.url
end
end
end