-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathapplication_helper.rb
More file actions
215 lines (183 loc) · 6.86 KB
/
application_helper.rb
File metadata and controls
215 lines (183 loc) · 6.86 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
module ApplicationHelper
def search_page(params)
params[:search] ? params[:search][:page] : 1
end
def checked?(param = false)
param == "1"
end
def months_with_year
(1..12).collect { |m| "#{m}/#{today.year}" }
end
def current_month_with_year
today.strftime("%_m/%Y")
end
def current_year
today.year
end
def today
Date.today
end
def display_banner
# Cache banners to avoid repeated queries during page render
@banners ||= Banner.active.select("id, content").to_a
return if @banners.empty?
safe_content_array = @banners.map { |banner|
sanitize(
banner.content,
tags: %w[a],
attributes: %w[href]
)
}
safe_content = safe_content_array.join("<br>")
content_tag(:div, id: "banner-news", class: "bg-yellow-200 text-black text-center px-4 py-2") do
content_tag(:div, safe_content.html_safe, class: "font-medium")
end
end
def sortable_field_display_name(name)
case name
when :adult
"Adult Windows"
when :children
"Children's Windows"
else
name.to_s.titleize
end
end
def icon_for_mimetype(mime)
mimes = {
"image" => "fa-file-image",
"audio" => "fa-file-audio",
"video" => "fa-file-video",
# Documents
"application/pdf" => "fa-file-pdf",
"application/msword" => "fa-file-word",
"application/vnd.ms-word" => "fa-file-word",
"application/vnd.oasis.opendocument.text" => "fa-file-word",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document" => "fa-file-word",
'application/vnd.ms-excel': "fa-file-excel",
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" => "fa-file-excel",
"application/vnd.oasis.opendocument.spreadsheet" => "fa-file-excel",
"application/vnd.ms-powerpoint" => "fa-file-powerpoint",
"application/vnd.openxmlformats-officedocument.presentationml" => "fa-file-powerpoint",
"application/vnd.oasis.opendocument.presentation" => "fa-file-powerpoint",
# Archives
"application/gzip" => "fa-file-archive"
}
if mime
m = mimes[mime.split("/").first]
m ||= mimes[mime]
end
m ||= "fa-file"
"fas #{m}"
end
def display_count(value)
value.to_i.zero? ? "--" : number_with_delimiter(value)
end
def navbar_bg_class
staging_environment? ? "bg-red-600" : "bg-primary"
end
def staging_environment?
ENV["RAILS_ENV"] == "staging" || Rails.env == "staging"
end
def favicon_file
case Rails.env.to_s
when "production"
"logo-circle.png"
when "staging"
"favicon.png"
else
"theme_default.png"
end
end
def email_confirmation_icon(user)
if user.unconfirmed_email.present?
content_tag(:span, "pending confirmation", class: "text-yellow-600 font-medium", title: "Email change pending confirmation")
elsif user.confirmed_at.present?
content_tag(:span, "confirmed", class: "text-green-600 font-medium", title: "Email confirmed")
else
content_tag(:span, "unconfirmed", class: "text-red-600 font-medium", title: "Email not confirmed")
end
end
def email_label_with_confirmation_icon(user)
"Email #{email_confirmation_icon(user)}".html_safe
end
# Returns checkbox options for the visibility filter dropdown.
# Each entry is [label, param_name, admin_only].
# Options adapt to the model's columns and the user's role.
def visibility_filter_options(model_class, admin:, authenticated:)
cols = model_class.column_names
options = []
if admin
options << [ "Published", :published, true ]
options << [ "Unpublished", :unpublished, true ]
options << [ "Featured", :featured, false ] if cols.include?("featured")
options << [ "Publicly Visible", :publicly_visible, false ] if cols.include?("publicly_visible")
options << [ "Publicly Featured", :publicly_featured, false ] if cols.include?("publicly_featured")
elsif authenticated
options << [ "Not Featured", :not_featured, false ] if cols.include?("featured")
options << [ "Featured", :featured, false ] if cols.include?("featured")
options << [ "Publicly Visible", :publicly_visible, false ] if cols.include?("publicly_visible")
options << [ "Publicly Featured", :publicly_featured, false ] if cols.include?("publicly_featured")
else
options << [ "Not Publicly Featured", :not_publicly_featured, false ] if cols.include?("publicly_featured")
options << [ "Publicly Featured", :publicly_featured, false ] if cols.include?("publicly_featured")
end
options
end
# Fundamental US time zones only (for user preference dropdown).
# Order: Eastern → Pacific, then Alaska, Hawaii, Arizona.
def default_organization_for_form(object)
return object.organization if object.organization.present?
if current_user.super_user?
Organization.find_by(name: ENV["ORGANIZATION_NAME"])
elsif current_user.person&.affiliations&.count == 1
current_user.person.primary_organization
end
end
def custom_caret_style
"background-image:url(\"data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%236b7280' d='M2 4l4 4 4-4'/%3E%3C/svg%3E\");background-position:right 0.75rem center;background-size:12px;background-repeat:no-repeat;"
end
def select_caret_class(blank:)
"w-full px-3 py-2 pr-10 border border-gray-300 rounded-lg appearance-none #{"select-placeholder" if blank}"
end
def select_caret_onchange
"if(this.value){this.classList.remove('select-placeholder')}else{this.classList.add('select-placeholder')}"
end
def hidden_fields_for_params(hash, prefix = nil)
return "".html_safe if hash.blank?
fields = []
hash.each do |key, value|
field_name = prefix ? "#{prefix}[#{key}]" : key.to_s
case value
when ActionDispatch::Http::UploadedFile
next
when Hash
fields << hidden_fields_for_params(value, field_name)
when Array
value.each_with_index do |item, i|
if item.is_a?(Hash)
fields << hidden_fields_for_params(item, "#{field_name}[#{i}]")
else
fields << tag.input(type: "hidden", name: "#{field_name}[]", value: item)
end
end
else
next if key.to_s == "id" && value.blank?
fields << tag.input(type: "hidden", name: field_name, value: value)
end
end
safe_join(fields)
end
def us_time_zone_fundamentals
zone_names = [
"Eastern Time (US & Canada)",
"Central Time (US & Canada)",
"Mountain Time (US & Canada)",
"Pacific Time (US & Canada)",
"Alaska",
"Hawaii",
"Arizona"
]
ActiveSupport::TimeZone.us_zones.select { |z| zone_names.include?(z.name) }.sort_by { |z| zone_names.index(z.name) }.map { |z| [ z.to_s, z.name ] }
end
end