Module: ApplicationHelper
- Includes:
- PathHelper
- Included in:
- ArticlesCsv, BankTransactionsCsv, FinancialTransactionSerializer, FinancialTransactionsCsv, InvoicesCsv, Mailer, OrdergroupsCsv, RenderPdf, UsersCsv
- Defined in:
- app/helpers/application_helper.rb
Overview
Methods added to this helper will be available to all templates in the application.
Instance Method Summary collapse
-
#base_errors(resource) ⇒ Object
render base errors in a form after failed validation railsapps.github.io/twitter-bootstrap-rails.html.
-
#bootstrap_flash_patched ⇒ String
Returns flash messages html.
-
#close_button(dismiss) ⇒ String
HTML for close button dismissing.
-
#expand(text, options = {}) ⇒ Object
Expand variables in text.
-
#foodcoop_css_path(options = {}) ⇒ String
Path to foodcoop CSS style (with MD5 parameter for caching).
-
#foodcoop_css_tag(_options = {}) ⇒ String
Stylesheet tag for foodcoop CSS style (
custom_css
foodcoop config). - #format_currency(amount) ⇒ Object
- #format_date(time = Time.now) ⇒ Object
- #format_datetime(time = Time.now) ⇒ Object
- #format_datetime_timespec(time, format) ⇒ Object
-
#format_iban(iban) ⇒ Object
Splits an IBAN into groups of 4 digits displayed with margins in between.
- #format_roles(record, icon = false) ⇒ Object
- #format_time(time = Time.now) ⇒ Object
-
#heading_helper(model, attribute, options = {}) ⇒ Object
Generates text for table heading for model attribute When the ‘short’ option is true, abbreviations will be used: When there is a non-empty model attribute ‘foo’, it looks for the model attribute translation ‘foo_short’ and use that as heading, with an abbreviation title of ‘foo’.
- #icon(name, options = {}) ⇒ Object
-
#items_per_page(options = {}) ⇒ Object
Link-collection for per_page-options when using the pagination-plugin.
- #link_to_gmaps(address) ⇒ Object
-
#link_to_top ⇒ Object
Generates a link to the top of the website.
-
#pagination_links_remote(collection, options = {}) ⇒ Object
Creates ajax-controlled-links for pagination.
-
#remote_link_to(text, options = {}) ⇒ Object
Remote links with default ‘loader’.gif during request.
- #show_title? ⇒ Boolean
-
#show_user(user = @current_user, options = {}) ⇒ Object
show a user, depending on settings.
-
#show_user_link(user = @current_user) ⇒ Object
render user presentation linking to default action (plugins can override this).
- #sort_link_helper(text, key, options = {}) ⇒ Object
- #tab_is_active?(tab) ⇒ Boolean
-
#title(page_title, show_title = true) ⇒ Object
to set a title for both the h1-tag and the title in the header.
-
#truncate(text, options = {}, &block) ⇒ Object
allow truncate to add title when tooltip option is given.
-
#weekday(dayNumber) ⇒ Object
Returns the weekday.
Methods included from PathHelper
#finance_group_transactions_path
Instance Method Details
#base_errors(resource) ⇒ Object
render base errors in a form after failed validation railsapps.github.io/twitter-bootstrap-rails.html
202 203 204 205 206 207 |
# File 'app/helpers/application_helper.rb', line 202 def base_errors(resource) return '' if resource.errors.empty? || resource.errors[:base].empty? = resource.errors[:base].map { |msg| content_tag(:li, msg) }.join render partial: 'shared/base_errors', locals: { error_messages: } end |
#bootstrap_flash_patched ⇒ String
Returns flash messages html.
Use this instead of twitter-bootstrap’s bootstrap_flash
method for safety, until CVE-2014-4920 is fixed.
187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'app/helpers/application_helper.rb', line 187 def bootstrap_flash_patched = [] flash.each do |type, | type = :success if type == 'notice' type = :error if type == 'alert' text = content_tag(:div, content_tag(:button, I18n.t('ui.marks.close').html_safe, :class => 'close', 'data-dismiss' => 'alert') + , class: "alert fade in alert-#{type}") << text if end .join("\n").html_safe end |
#close_button(dismiss) ⇒ String
Returns HTML for close button dismissing.
252 253 254 255 256 |
# File 'app/helpers/application_helper.rb', line 252 def (dismiss) content_tag :button, type: 'button', class: 'close', data: { dismiss: dismiss } do I18n.t('ui.marks.close').html_safe end end |
#expand(text, options = {}) ⇒ Object
Expand variables in text
246 247 248 |
# File 'app/helpers/application_helper.rb', line 246 def (text, = {}) Foodsoft::ExpansionVariables.(text, ) end |
#foodcoop_css_path(options = {}) ⇒ String
Returns path to foodcoop CSS style (with MD5 parameter for caching).
259 260 261 |
# File 'app/helpers/application_helper.rb', line 259 def foodcoop_css_path( = {}) super(.merge({ md5: Digest::MD5.hexdigest(FoodsoftConfig[:custom_css].to_s) })) end |
#foodcoop_css_tag(_options = {}) ⇒ String
Returns stylesheet tag for foodcoop CSS style (custom_css
foodcoop config).
265 266 267 268 269 |
# File 'app/helpers/application_helper.rb', line 265 def foodcoop_css_tag( = {}) return if FoodsoftConfig[:custom_css].blank? stylesheet_link_tag foodcoop_css_path, media: 'all' end |
#format_currency(amount) ⇒ Object
22 23 24 25 26 27 |
# File 'app/helpers/application_helper.rb', line 22 def format_currency(amount) return nil if amount.nil? class_name = amount < 0 ? 'negative_amout' : 'positive_amount' content_tag :span, number_to_currency(amount), class: class_name end |
#format_date(time = Time.now) ⇒ Object
10 11 12 |
# File 'app/helpers/application_helper.rb', line 10 def format_date(time = Time.now) I18n.l(time.to_date) unless time.nil? end |
#format_datetime(time = Time.now) ⇒ Object
14 15 16 |
# File 'app/helpers/application_helper.rb', line 14 def format_datetime(time = Time.now) I18n.l(time) unless time.nil? end |
#format_datetime_timespec(time, format) ⇒ Object
18 19 20 |
# File 'app/helpers/application_helper.rb', line 18 def format_datetime_timespec(time, format) I18n.l(time, format: format) unless time.nil? || format.nil? end |
#format_iban(iban) ⇒ Object
Splits an IBAN into groups of 4 digits displayed with margins in between
30 31 32 |
# File 'app/helpers/application_helper.rb', line 30 def format_iban(iban) iban && iban.scan(/..?.?.?/).map { |item| content_tag(:span, item, style: 'margin-right: 0.5em;') }.join.html_safe end |
#format_roles(record, icon = false) ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'app/helpers/application_helper.rb', line 162 def format_roles(record, icon = false) roles = %w[suppliers article_meta orders pickups finance invoices admin] roles.select! { |role| record.send "role_#{role}?" } names = roles.index_with { |r| I18n.t("helpers.application.role_#{r}") } if icon roles.map do |r| image_tag("role-#{r}.png", size: '22x22', border: 0, alt: names[r], title: names[r]) end.join(' ').html_safe else roles.map { |r| names[r] }.join(', ') end end |
#format_time(time = Time.now) ⇒ Object
6 7 8 |
# File 'app/helpers/application_helper.rb', line 6 def format_time(time = Time.now) I18n.l(time, format: :foodsoft_datetime) unless time.nil? end |
#heading_helper(model, attribute, options = {}) ⇒ Object
Generates text for table heading for model attribute When the ‘short’ option is true, abbreviations will be used:
When there is a non-empty model attribute 'foo', it looks for
the model attribute translation 'foo_short' and use that as
heading, with an abbreviation title of 'foo'. If a translation
'foo_desc' is present, that is used instead, but that can be
be overridden by the option 'desc'.
Other options are passed through to I18n.
97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'app/helpers/application_helper.rb', line 97 def heading_helper(model, attribute, = {}) i18nopts = { count: 2 }.merge(.select { |a| !%w[short desc].include?(a) }) s = model.human_attribute_name(attribute, i18nopts) if [:short] desc = [:desc] desc ||= model.human_attribute_name(:"#{attribute}_desc", .merge({ fallback: true, default: '', count: 2 })) desc.blank? && desc = s sshort = model.human_attribute_name(:"#{attribute}_short", .merge({ fallback: true, default: '', count: 2 })) s = raw "<abbr title='#{desc}'>#{sshort}</abbr>" if sshort.present? end s end |
#icon(name, options = {}) ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'app/helpers/application_helper.rb', line 139 def icon(name, = {}) icons = { delete: { file: 'b_drop.png', alt: I18n.t('ui.delete') }, edit: { file: 'b_edit.png', alt: I18n.t('ui.edit') }, members: { file: 'b_users.png', alt: I18n.t('helpers.application.edit_user') } } [:alt] ||= icons[name][:alt] [:title] ||= icons[name][:title] .merge!({ size: '16x16', border: '0' }) image_tag icons[name][:file], end |
#items_per_page(options = {}) ⇒ Object
Link-collection for per_page-options when using the pagination-plugin
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'app/helpers/application_helper.rb', line 43 def items_per_page( = {}) = [:per_page_options] || [20, 50, 100, 500] current = [:current] || @per_page params ||= {} links = .map do |per_page| params.merge!({ per_page: per_page }) link_class = 'btn' link_class << ' disabled' if per_page == current link_to(per_page, params, remote: true, class: link_class) end if [:wrap] == false links.join.html_safe else content_tag :div, class: 'btn-group pull-right' do links.join.html_safe end end end |
#link_to_gmaps(address) ⇒ Object
175 176 177 178 |
# File 'app/helpers/application_helper.rb', line 175 def link_to_gmaps(address) link_to h(address), "http://maps.google.com/?q=#{h(address)}", title: I18n.t('helpers.application.show_google_maps'), target: '_blank', rel: 'noopener' end |
#link_to_top ⇒ Object
Generates a link to the top of the website
113 114 115 116 117 |
# File 'app/helpers/application_helper.rb', line 113 def link_to_top link_to '#' do content_tag :i, nil, class: 'icon-arrow-up icon-large' end end |
#pagination_links_remote(collection, options = {}) ⇒ Object
Creates ajax-controlled-links for pagination
35 36 37 38 39 40 |
# File 'app/helpers/application_helper.rb', line 35 def pagination_links_remote(collection, = {}) per_page = [:per_page] || @per_page params = [:params] || {} params = params.merge({ per_page: per_page }) paginate collection, params: params, remote: true end |
#remote_link_to(text, options = {}) ⇒ Object
Remote links with default ‘loader’.gif during request
153 154 155 156 157 158 159 160 |
# File 'app/helpers/application_helper.rb', line 153 def remote_link_to(text, = {}) = { before: "Element.show('loader')", success: "Element.hide('loader')", method: :get } link_to(text, [:url], .merge()) end |
#show_title? ⇒ Boolean
131 132 133 |
# File 'app/helpers/application_helper.rb', line 131 def show_title? @show_title end |
#show_user(user = @current_user, options = {}) ⇒ Object
show a user, depending on settings
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'app/helpers/application_helper.rb', line 210 def show_user(user = @current_user, = {}) if user.nil? '?' elsif FoodsoftConfig[:use_nick] if [:full] && [:markup] raw "<b>#{h user.nick}</b> (#{h user.first_name} #{h user.last_name})" elsif [:full] "#{user.nick} (#{user.first_name} #{user.last_name})" else # when use_nick was changed from false to true, users may exist without nick user.nick.nil? ? I18n.t('helpers.application.nick_fallback') : user.nick end else "#{user.first_name} #{user.last_name}" + ([:unique] ? " (##{user.id})" : '') end end |
#show_user_link(user = @current_user) ⇒ Object
render user presentation linking to default action (plugins can override this)
228 229 230 |
# File 'app/helpers/application_helper.rb', line 228 def show_user_link(user = @current_user) show_user user end |
#sort_link_helper(text, key, options = {}) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'app/helpers/application_helper.rb', line 64 def sort_link_helper(text, key, = {}) # Hmtl options remote = [:remote].nil? ? true : [:remote] class_name = case params[:sort] when key 'sortup' when key + '_reverse' 'sortdown' end = { title: I18n.t('helpers.application.sort_by', text: text), remote: remote, class: class_name } # Url options key += '_reverse' if params[:sort] == key per_page = [:per_page] || @per_page = params.merge(per_page: per_page, sort: key) .merge!({ page: params[:page] }) if params[:page] .merge!({ query: params[:query] }) if params[:query] link_to(text, url_for(), ) end |
#tab_is_active?(tab) ⇒ Boolean
135 136 137 |
# File 'app/helpers/application_helper.rb', line 135 def tab_is_active?(tab) tab[:active].detect { |c| controller.controller_path.match(c) } end |
#title(page_title, show_title = true) ⇒ Object
to set a title for both the h1-tag and the title in the header
126 127 128 129 |
# File 'app/helpers/application_helper.rb', line 126 def title(page_title, show_title = true) content_for(:title) { page_title.to_s } @show_title = show_title end |
#truncate(text, options = {}, &block) ⇒ Object
allow truncate to add title when tooltip option is given
233 234 235 236 237 238 239 240 241 242 |
# File 'app/helpers/application_helper.rb', line 233 def truncate(text, = {}, &block) return text if !text || text.length <= ([:length] || 30) text_truncated = super(text, , &block) if [:tooltip] content_tag :span, text_truncated, title: text else text_truncated end end |
#weekday(dayNumber) ⇒ Object
Returns the weekday. 0 is sunday, 1 is monday and so on
120 121 122 123 |
# File 'app/helpers/application_helper.rb', line 120 def weekday(dayNumber) weekdays = I18n.t('date.day_names') weekdays[dayNumber] end |