Module: ApplicationHelper
- Defined in:
- app/helpers/application_helper.rb
Overview
Contains general, simple view helper code. Designed to keep code out of our views as much as possible
Instance Method Summary collapse
- #admin? ⇒ Boolean
- #audited_value(value) ⇒ Object
- #body_class(class_name = "") ⇒ Object
- #body_class_default ⇒ Object
- #description_css_class(description) ⇒ Object
-
#devise_form_errors(errors) ⇒ Object
Unwraps Devise error messages so they look like flash messages, the way regular application alerts work.
- #devise_mapping ⇒ Object
-
#drop_down_menu_link(text, path, icon_name = nil, options = {}) ⇒ String
HTML link with the given icon attached.
-
#endpoint_collection ⇒ Array<String, Integer>
List of Endpoints suitable for use in select boxes in Representation forms.
-
#flash_class(level) ⇒ String
CSS class to use when styling a flash message.
- #image_status_css_class(status_code) ⇒ Object
-
#language_list ⇒ Array<Array>
A collection of languages suitable for use in a select box.
-
#language_name(language_code) ⇒ String
Human-friendly language name.
- #license_link(license) ⇒ Object
- #license_title(license) ⇒ Object
-
#minimum_password_length ⇒ Integer
Minimum number of password characters we accept.
-
#nav_menu_link(text, path) ⇒ Object
Used to render top-level navigation, so the current page gets an “active” CSS class applied.
-
#organizational_user_assignable_roles ⇒ Hash
A collection of User roles with human friendly labels that the current organizational user can assign.
-
#organizational_user_collection ⇒ Array<String, Integer>
List of users in the current organization, sorted by name, suitable for use in a select box.
- #resource ⇒ Object
- #resource_name ⇒ Object
- #to_html(content) ⇒ Object
- #to_html_attr(content) ⇒ Object
-
#to_html_title(content) ⇒ String
HTML-formatted string, suitable for use in an H1 tag.
- #to_text(content) ⇒ Object
-
#welcome_message ⇒ String
Welcome message, including the user's name if someone is logged-in.
Instance Method Details
#admin? ⇒ Boolean
142 143 144 |
# File 'app/helpers/application_helper.rb', line 142 def admin? current_user.try(:admin?) end |
#audited_value(value) ⇒ Object
186 187 188 189 190 191 192 |
# File 'app/helpers/application_helper.rb', line 186 def audited_value(value) if value.kind_of? Array value.last else value end end |
#body_class(class_name = "") ⇒ Object
92 93 94 95 |
# File 'app/helpers/application_helper.rb', line 92 def body_class(class_name="") class_name = "#{body_class_default} #{class_name}" content_for :body_class, class_name end |
#body_class_default ⇒ Object
88 89 90 |
# File 'app/helpers/application_helper.rb', line 88 def body_class_default [controller_name, action_name].join('-') + "#{controller_name} #{action_name}" end |
#description_css_class(description) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'app/helpers/application_helper.rb', line 129 def description_css_class(description) klass = "item " case description.status_id when 2 klass += "success" when 1 klass += "warning" when 3 klass += "danger" end klass end |
#devise_form_errors(errors) ⇒ Object
Unwraps Devise error messages so they look like flash messages, the way regular application alerts work
100 101 102 103 104 105 106 |
# File 'app/helpers/application_helper.rb', line 100 def devise_form_errors(errors) capture do errors..each_with_index.map do |msg, idx| concat render partial: 'alert', locals: { flash_type: :error, flash_message: msg, flash_id: "devise_flash_#{idx}" } end end end |
#devise_mapping ⇒ Object
84 85 86 |
# File 'app/helpers/application_helper.rb', line 84 def devise_mapping @devise_mapping ||= Devise.mappings[:user] end |
#drop_down_menu_link(text, path, icon_name = nil, options = {}) ⇒ String
Returns HTML link with the given icon attached
39 40 41 42 43 44 45 46 47 48 |
# File 'app/helpers/application_helper.rb', line 39 def (text, path, icon_name = nil, = {}) classes = %w[fa fa-fw] classes << "fa-#{icon_name}" if icon_name link_to(path, ) do concat tag.i('', class: classes, :'aria-hidden' => true) concat "\n" concat text end end |
#endpoint_collection ⇒ Array<String, Integer>
Returns list of Endpoints suitable for use in select boxes in Representation forms
9 10 11 |
# File 'app/helpers/application_helper.rb', line 9 def endpoint_collection Endpoint.sorted end |
#flash_class(level) ⇒ String
Returns CSS class to use when styling a flash message
110 111 112 |
# File 'app/helpers/application_helper.rb', line 110 def flash_class(level) FLASH_CLASSES.fetch(level.to_sym) end |
#image_status_css_class(status_code) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'app/helpers/application_helper.rb', line 114 def image_status_css_class(status_code) klass = "" case status_code when 0 klass += "undescribed" when 1 klass += "partial" when 2 klass += "warning" when 3 klass += "success" end return klass end |
#language_list ⇒ Array<Array>
Returns a collection of languages suitable for use in a select box
29 30 31 |
# File 'app/helpers/application_helper.rb', line 29 def language_list @language_list ||= LanguageList::COMMON_LANGUAGES.map { |l| [l.common_name, l.iso_639_1] } end |
#language_name(language_code) ⇒ String
Returns human-friendly language name
23 24 25 |
# File 'app/helpers/application_helper.rb', line 23 def language_name(language_code) LanguageList::LanguageInfo.find(language_code)&.name end |
#license_link(license) ⇒ Object
194 195 196 |
# File 'app/helpers/application_helper.rb', line 194 def license_link(license) "https://choosealicense.com/licenses/" + license end |
#license_title(license) ⇒ Object
198 199 200 201 202 203 204 205 206 207 |
# File 'app/helpers/application_helper.rb', line 198 def license_title(license) case license when "cc0-1.0" "Creative Commons Zero v1.0 Universal" when "cc-by-sa-4.0" "Creative Commons Attribution Share Alike 4.0" when "cc-by-4.0" "Creative Commons Attribution 4.0" end end |
#minimum_password_length ⇒ Integer
Returns minimum number of password characters we accept
52 53 54 |
# File 'app/helpers/application_helper.rb', line 52 def minimum_password_length User.password_length.min end |
#nav_menu_link(text, path) ⇒ Object
Used to render top-level navigation, so the current page gets an “active” CSS class applied
178 179 180 181 182 183 184 |
# File 'app/helpers/application_helper.rb', line 178 def (text, path) link_class = current_page?(path) ? "active" : "" content_tag(:li, class: link_class) do link_to(text, path) end end |
#organizational_user_assignable_roles ⇒ Hash
Returns a collection of User roles with human friendly labels that the current organizational user can assign
58 59 60 61 62 63 64 65 66 67 |
# File 'app/helpers/application_helper.rb', line 58 def organizational_user_assignable_roles roles = [] Coyote::Membership.each_role do |label, role_name, _| break unless organization_user.send(:"#{role_name}?") roles << [label, role_name] end roles end |
#organizational_user_collection ⇒ Array<String, Integer>
Returns List of users in the current organization, sorted by name, suitable for use in a select box
14 15 16 17 18 |
# File 'app/helpers/application_helper.rb', line 14 def organizational_user_collection # @return [Array<String, Integer>] list of users suitable for use in select boxes collection = current_organization.active_users.active.sort_by { |u| u.username.downcase } collection.map! { |u| [u.username, u.id] } end |
#resource ⇒ Object
80 81 82 |
# File 'app/helpers/application_helper.rb', line 80 def resource @resource ||= User.new end |
#resource_name ⇒ Object
76 77 78 |
# File 'app/helpers/application_helper.rb', line 76 def resource_name :user end |
#to_html(content) ⇒ Object
146 147 148 149 150 151 152 |
# File 'app/helpers/application_helper.rb', line 146 def to_html(content) if content.blank? "" else raw markdown.render(content) end end |
#to_html_attr(content) ⇒ Object
171 172 173 |
# File 'app/helpers/application_helper.rb', line 171 def to_html_attr(content) h to_text(content) end |
#to_html_title(content) ⇒ String
This is a hack to avoid <p> tags when rendering resource titles as H1, see github.com/vmg/redcarpet/issues/596
Returns HTML-formatted string, suitable for use in an H1 tag
165 166 167 168 169 |
# File 'app/helpers/application_helper.rb', line 165 def to_html_title(content) html = to_html(content) html.gsub!(%r{(?:^<p>|</p>\n)}i, '') html.html_safe end |
#to_text(content) ⇒ Object
154 155 156 157 158 159 160 |
# File 'app/helpers/application_helper.rb', line 154 def to_text(content) if content.blank? "" else to_html(content) end end |
#welcome_message ⇒ String
Returns welcome message, including the user's name if someone is logged-in
70 71 72 73 74 |
# File 'app/helpers/application_helper.rb', line 70 def msg = "Welcome to Coyote" msg << ", #{current_user}!" if current_user msg end |