Module: ApplicationHelper
- Defined in:
- app/helpers/application_helper.rb
Overview
Application-wide helpers
Instance Method Summary collapse
-
#bootstrap_flash_messages ⇒ Object
Outputs the flash messages in a bootstrap optimized way.
-
#breadcrumbs ⇒ String
Outputs the breadcrumbs based in the @breadcrumbs variable.
-
#collapse_link(target, label = '') ⇒ String
Outputs a trigger for (un)collapsing a target element.
-
#country_label(country_code) ⇒ Object
Outputs country code in a human readable and localized way, using the locale file for localized_country_select.
-
#currencies_for_select(field, event = nil) ⇒ Array
Currency codes that can be selected for a given field.
-
#current_role ⇒ Object
Outputs the role of the current user.
-
#current_role?(role) ⇒ Boolean
Checks if the current user belongs to a given role.
-
#dropdown(label, links) ⇒ String
Outputs a Bootstrap's button dropdown menu.
-
#enabled?(*setting_path) ⇒ Boolean
Checks whether a concrete setting is enabled in the config file.
-
#expenses_sum(object, attr) ⇒ String
Outputs the sum of the expenses of a request or a reimbursement (or for a collection of requests/reimbursents), as a list of comma separated number_to_currency (one for every used currency).
-
#goto_link(path) ⇒ String
Outputs a link with the "go to" label and an icon.
-
#icon_to(name, path, options = {}) ⇒ String
Outputs a link with an icon inside (and visible no text).
-
#machine_url(machine) ⇒ Object
URL for accesing to a given request or reimbursement.
-
#markdown(text) ⇒ Object
Output the text in a markdown manner.
-
#menu_path_to(machine_class) ⇒ Object
Path to the list of requests or reimbursements with the ransack filters already configured based on the assign_state definitions of the corresponding class.
-
#nav_tab(label, path) ⇒ Object
Outputs a
- element suitable for use in Bootstrap nav menus, that is, with the 'active' css class when needed.
-
#pdf_header_image ⇒ String
Image to be used as header in pdf files.
-
#state_change_links(machine) ⇒ String
Outputs a list of links to create every possible and authorized state transition for a given state machine.
-
#state_info(object_with_state) ⇒ String
Outputs the state of a model instance with a help tooltip if needed.
-
#timestamped_state(object_with_state) ⇒ String
Outputs the state of a model instance with the appropiate css class and the associated date.
-
#user_signed_in_by_ichain? ⇒ Boolean
Checks if the current user logged in using iChain.
Instance Method Details
#bootstrap_flash_messages ⇒ Object
Outputs the flash messages in a bootstrap optimized way
Stolen from https://github.com/seyhunak/twitter-bootstrap-rails
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'app/helpers/application_helper.rb', line 224 def = [] flash.each do |type, | # Skip empty messages, e.g. for devise messages set to nothing in a locale file. next if .blank? type = 'success' if type == 'notice' type = 'danger' if %w[error alert].include?(type) next unless %w[danger info success warning].include?(type) Array().each do |msg| text = content_tag(:div, content_tag(:button, raw('×'), :class => 'close', 'data-dismiss' => 'alert') + msg.html_safe, class: "alert fade in alert-#{type}") << text if end end .join("\n").html_safe end |
#breadcrumbs ⇒ String
Outputs the breadcrumbs based in the @breadcrumbs variable
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 |
# File 'app/helpers/application_helper.rb', line 284 def return '' unless @breadcrumbs&.respond_to?(:map) crumbs = @breadcrumbs.map do |b| # First of all, adjust the label,... label = b[:label] # ...that can be a string, but... unless label.is_a? String # ...also a symbol... label = if label.is_a? Symbol I18n.t(label) # ...or a more complex object. # In that case, some methods are tried before # falling back to 'classname #id' elsif label.respond_to? :name label.name elsif label.respond_to? :title label.title else "#{label.class.model_name.human} ##{label.id}" end end if b[:url].blank? || current_page?(b[:url]) content_tag(:li, label, class: 'active') else content_tag(:li, link_to(label, b[:url])) end end crumbs = crumbs.join(' ' + content_tag(:span, '>', class: 'divider') + ' ') content_tag(:ul, crumbs.html_safe, class: 'breadcrumb') end |
#collapse_link(target, label = '') ⇒ String
Outputs a trigger for (un)collapsing a target element
78 79 80 81 |
# File 'app/helpers/application_helper.rb', line 78 def collapse_link(target, label = '') link_to(label.html_safe + content_tag(:i, '', class: 'icon-resize-vertical'), "\##{target}", title: t(:collapse), data: { toggle: :collapse }) end |
#country_label(country_code) ⇒ Object
Outputs country code in a human readable and localized way, using the locale file for localized_country_select
param [String] country_code as specified in locale file return [String] HTML output
23 24 25 26 27 28 29 |
# File 'app/helpers/application_helper.rb', line 23 def country_label(country_code) if country_code.blank? t('show_for.blank') else country_code + ' - ' + t("countries.#{country_code}") end end |
#currencies_for_select(field, event = nil) ⇒ Array
Currency codes that can be selected for a given field
251 252 253 254 255 256 257 258 259 |
# File 'app/helpers/application_helper.rb', line 251 def currencies_for_select(field, event = nil) if Rails.configuration.site['budget_limits'] && event && event.budget && event.budget.currency && %w[approved authorized].include?(field.to_s) currencies = [event.budget.currency] end currencies ||= Rails.configuration.site["currencies_for_#{field}"] currencies ||= I18n.translate(:currencies).keys.sort end |
#current_role ⇒ Object
Outputs the role of the current user. Useful, for example, for deciding which partial view should be used.
116 117 118 |
# File 'app/helpers/application_helper.rb', line 116 def current_role current_user.find_profile.role_name end |
#current_role?(role) ⇒ Boolean
Checks if the current user belongs to a given role
124 125 126 |
# File 'app/helpers/application_helper.rb', line 124 def current_role?(role) current_role == role.to_s end |
#dropdown(label, links) ⇒ String
Outputs a Bootstrap's button dropdown menu
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'app/helpers/application_helper.rb', line 170 def dropdown(label, links) if links.empty? '' else = links.map do |l| if l.empty? content_tag(:li, '', class: 'divider') else content_tag(:li, l) end end content_tag(:div, link_to(label.html_safe + ' ' + content_tag(:span, '', class: 'caret'), '#', class: 'btn dropdown-toggle', data: { toggle: 'dropdown' }) + content_tag(:ul, .join("\n").html_safe, class: 'dropdown-menu'), class: 'btn-group') end end |
#enabled?(*setting_path) ⇒ Boolean
Checks whether a concrete setting is enabled in the config file
322 323 324 |
# File 'app/helpers/application_helper.rb', line 322 def enabled?(*setting_path) Rails.configuration.site.dig(*setting_path)['enabled'] end |
#expenses_sum(object, attr) ⇒ String
Outputs the sum of the expenses of a request or a reimbursement (or for a collection of requests/reimbursents), as a list of comma separated number_to_currency (one for every used currency)
101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'app/helpers/application_helper.rb', line 101 def expenses_sum(object, attr) sum = if object.respond_to?(:size) if first = object.first first.class.expenses_sum(attr, object) else [] end else object.expenses_sum(attr) end sum.map { |k, v| number_to_currency(v, unit: (k || '?')) }.join(', ') end |
#goto_link(path) ⇒ String
Outputs a link with the "go to" label and an icon
87 88 89 90 |
# File 'app/helpers/application_helper.rb', line 87 def goto_link(path) link_to("#{t(:goto)} ".html_safe + content_tag(:i, '', class: 'icon-share-alt'), path, title: t(:goto)) end |
#icon_to(name, path, options = {}) ⇒ String
Outputs a link with an icon inside (and visible no text)
69 70 71 |
# File 'app/helpers/application_helper.rb', line 69 def icon_to(name, path, = {}) link_to(content_tag(:i, '', class: "icon-#{name}"), path, ) end |
#machine_url(machine) ⇒ Object
URL for accesing to a given request or reimbursement
This helper deals with the singleton issues in the reimbursement routes definition.
194 195 196 197 198 199 200 |
# File 'app/helpers/application_helper.rb', line 194 def machine_url(machine) if machine.is_a?(Reimbursement) request_reimbursement_url(machine.request) else send(:"#{machine.class.model_name.element}_url", machine) end end |
#markdown(text) ⇒ Object
Output the text in a markdown manner
57 58 59 60 61 |
# File 'app/helpers/application_helper.rb', line 57 def markdown(text) renderer = Redcarpet::Render::HTML.new(hard_wrap: true) markdown = Redcarpet::Markdown.new(renderer, autolink: true) markdown.render(text).html_safe end |
#menu_path_to(machine_class) ⇒ Object
Path to the list of requests or reimbursements with the ransack filters already configured based on the assign_state definitions of the corresponding class.
207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'app/helpers/application_helper.rb', line 207 def (machine_class) helper = machine_class.model_name.to_s.tableize + '_path' # Assistants are a special case, they should have the same default filters than tsp role = current_role.to_s == 'assistant' ? :tsp : current_role states = machine_class.states_assigned_to(role) # FIXME: requesters are also an special case nowadays. Let's look for a # better solution afterward. if current_role?(:none) || states.blank? send helper else send(helper, q: { state_in: states }) end end |
#nav_tab(label, path) ⇒ Object
Outputs a
param [String] label Text to display param [String] path Target URL return [String] HTML output
13 14 15 16 |
# File 'app/helpers/application_helper.rb', line 13 def nav_tab(label, path) css = current_page?(path) ? 'active' : nil content_tag(:li, link_to(t(label), path), class: css) end |
#pdf_header_image ⇒ String
Image to be used as header in pdf files
264 265 266 267 268 269 270 |
# File 'app/helpers/application_helper.rb', line 264 def pdf_header_image if theme = Rails.configuration.site['theme'] path = File.join(Rails.root.to_s, 'app', 'themes', theme, 'assets', 'images', 'pdf', 'header.png') return path if File.exist?(path) end File.join(Rails.root.to_s, 'app', 'assets', 'images', 'pdf', 'header.png') end |
#state_change_links(machine) ⇒ String
Outputs a list of links to create every possible and authorized state transition for a given state machine. If the user is authorized, it also adds the 'cancel' and 'adjust state' options.
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 |
# File 'app/helpers/application_helper.rb', line 136 def state_change_links(machine) resource_path = case machine.class.name when 'Shipment' shipment_path(machine) when 'Reimbursement' request_reimbursement_path(machine.request) when 'TravelSponsorship' travel_sponsorship_path(machine) end trans_path = resource_path + '/state_transitions/new.js?state_transition[state_event]=' links = machine.state_events.map do |event| next unless can? event, machine link_to(t("activerecord.state_machines.events.#{event}").titleize, trans_path + event.to_s, remote: true) end.compact # Add cancel link if can? :cancel, machine links << '' links << link_to(t('helpers.links.cancel'), "#{trans_path}cancel", remote: true) end # Add adjust_state link if can? :adjust_state, machine links << '' links << link_to(t('helpers.links.adjust_state'), resource_path + '/state_adjustments/new.js', remote: true) end dropdown t('helpers.workflow_actions'), links end |
#state_info(object_with_state) ⇒ String
Outputs the state of a model instance with a help tooltip if needed
46 47 48 49 50 51 52 53 54 |
# File 'app/helpers/application_helper.rb', line 46 def state_info(object_with_state) msg = content_tag(:span, object_with_state.human_state_name, class: object_with_state.state) msg += " (#{object_with_state.human_state_description})" if object_with_state.state_updated_at.blank? msg += ' ' msg += content_tag(:span, '!', title: t(:state_help), class: 'badge with-tooltip') end raw(msg) end |
#timestamped_state(object_with_state) ⇒ String
Outputs the state of a model instance with the appropiate css class and the associated date
36 37 38 39 40 |
# File 'app/helpers/application_helper.rb', line 36 def (object_with_state) msg = content_tag(:span, object_with_state.human_state_name, class: object_with_state.state) msg += ' ' + t(:since, date: l(object_with_state.state_updated_at, format: :long)) unless object_with_state.state_updated_at.blank? raw(msg) end |
#user_signed_in_by_ichain? ⇒ Boolean
Checks if the current user logged in using iChain
275 276 277 278 279 |
# File 'app/helpers/application_helper.rb', line 275 def user_signed_in_by_ichain? return false unless user_signed_in? current_user.respond_to?(:signed_in_by_ichain?) && current_user.signed_in_by_ichain? end |