Module: ApplicationHelper

Includes:
PathHelper
Included in:
ArticlesCsv, FinancialTransactionSerializer, FinancialTransactionsCsv, Mailer, OrdergroupsCsv, RenderPDF
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

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


196
197
198
199
200
# File 'app/helpers/application_helper.rb', line 196

def base_errors resource
  return '' if resource.errors.empty? || resource.errors[:base].empty?
  messages = resource.errors[:base].map { |msg| (:li, msg) }.join
  render :partial => 'shared/base_errors', :locals => {:error_messages => messages}
end

#bootstrap_flash_patchedString

Returns flash messages html.

Use this instead of twitter-bootstrap's bootstrap_flash method for safety, until CVE-2014-4920 is fixed.

Returns:

  • (String)

    Flash message html.

See Also:


181
182
183
184
185
186
187
188
189
190
191
192
# File 'app/helpers/application_helper.rb', line 181

def bootstrap_flash_patched
  flash_messages = []
  flash.each do |type, message|
    type = :success if type == 'notice'
    type = :error   if type == 'alert'
    text = (:div,
                       (:button, I18n.t('ui.marks.close').html_safe, :class => "close", "data-dismiss" => "alert") +
                           message, :class => "alert fade in alert-#{type}")
    flash_messages << text if message
  end
  flash_messages.join("\n").html_safe
end

#close_button(dismiss) ⇒ String

Returns HTML for close button dismissing.

Parameters:

  • dismiss (String, Symbol)

    Bootstrap dismiss value (modal, alert)

Returns:

  • (String)

    HTML for close button dismissing


244
245
246
247
248
# File 'app/helpers/application_helper.rb', line 244

def close_button(dismiss)
   :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


238
239
240
# File 'app/helpers/application_helper.rb', line 238

def expand(text, options={})
  Foodsoft::ExpansionVariables.expand(text, options)
end

#foodcoop_css_path(options = {}) ⇒ String

Returns path to foodcoop CSS style (with MD5 parameter for caching).

Returns:

  • (String)

    path to foodcoop CSS style (with MD5 parameter for caching)


251
252
253
# File 'app/helpers/application_helper.rb', line 251

def foodcoop_css_path(options={})
  super(options.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).

Returns:

  • (String)

    stylesheet tag for foodcoop CSS style (custom_css foodcoop config)

See Also:


257
258
259
260
261
# File 'app/helpers/application_helper.rb', line 257

def foodcoop_css_tag(options={})
  unless FoodsoftConfig[:custom_css].blank?
    stylesheet_link_tag foodcoop_css_path, media: 'all'
  end
end

#format_currency(amount) ⇒ Object


23
24
25
26
# File 'app/helpers/application_helper.rb', line 23

def format_currency(amount)
  class_name = amount < 0 ? 'negative_amout' : 'positive_amount'
   :span, number_to_currency(amount), class: class_name
end

#format_date(time = Time.now) ⇒ Object


11
12
13
# File 'app/helpers/application_helper.rb', line 11

def format_date(time = Time.now)
  I18n.l(time.to_date) unless time.nil?
end

#format_datetime(time = Time.now) ⇒ Object


15
16
17
# File 'app/helpers/application_helper.rb', line 15

def format_datetime(time = Time.now)
  I18n.l(time) unless time.nil?
end

#format_datetime_timespec(time, format) ⇒ Object


19
20
21
# File 'app/helpers/application_helper.rb', line 19

def format_datetime_timespec(time, format)
  I18n.l(time, :format => format) unless (time.nil? || format.nil?)
end

#format_roles(record, icon = false) ⇒ Object


158
159
160
161
162
163
164
165
166
167
# File 'app/helpers/application_helper.rb', line 158

def format_roles(record, icon=false)
  roles = %w(suppliers article_meta orders pickups finance invoices admin)
  roles.select! {|role| record.send "role_#{role}?"}
  names = Hash[roles.map{|r| [r, I18n.t("helpers.application.role_#{r}")]}]
  if icon
    roles.map{|r| image_tag("role-#{r}.png", size: '22x22', border: 0, alt: names[r], title: names[r])}.join('&nbsp;').html_safe
  else
    roles.map{|r| names[r]}.join(', ')
  end
end

#format_time(time = Time.now) ⇒ Object


7
8
9
# File 'app/helpers/application_helper.rb', line 7

def format_time(time = Time.now)
  I18n.l(time, :format => "%d.%m.%Y %H:%M") 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.

95
96
97
98
99
100
101
102
103
104
105
106
# File 'app/helpers/application_helper.rb', line 95

def heading_helper(model, attribute, options = {})
  i18nopts = {count: 2}.merge(options.select {|a| !['short', 'desc'].include?(a) })
  s = model.human_attribute_name(attribute, i18nopts)
  if options[:short]
    desc = options[:desc]
    desc ||= model.human_attribute_name("#{attribute}_desc".to_sym, options.merge({fallback: true, default: '', count: 2}))
    desc.blank? && desc = s
    sshort = model.human_attribute_name("#{attribute}_short".to_sym, options.merge({fallback: true, default: '', count: 2}))
    s = raw "<abbr title='#{desc}'>#{sshort}</abbr>" unless sshort.blank?
  end
  s
end

#icon(name, options = {}) ⇒ Object


135
136
137
138
139
140
141
142
143
144
145
146
# File 'app/helpers/application_helper.rb', line 135

def icon(name, options={})
  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')}
  }
  options[:alt] ||= icons[name][:alt]
  options[:title] ||= icons[name][:title]
  options.merge!({:size => '16x16',:border => "0"})

  image_tag icons[name][:file], options
end

#items_per_page(options = {}) ⇒ Object

Link-collection for per_page-options when using the pagination-plugin


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/helpers/application_helper.rb', line 37

def items_per_page(options = {})
  per_page_options = options[:per_page_options] || [20, 50, 100, 500]
  current = options[:current] || @per_page
  params = params || {}

  links = per_page_options.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 options[:wrap] == false
    links.join.html_safe
  else
     :div, class: 'btn-group pull-right' do
      links.join.html_safe
    end
  end

end

169
170
171
172
# File 'app/helpers/application_helper.rb', line 169

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"
end

Generates a link to the top of the website


109
110
111
112
113
# File 'app/helpers/application_helper.rb', line 109

def link_to_top
  link_to '#' do
     :i, nil, class: 'icon-arrow-up icon-large'
  end
end

Creates ajax-controlled-links for pagination


29
30
31
32
33
34
# File 'app/helpers/application_helper.rb', line 29

def pagination_links_remote(collection, options = {})
  per_page = options[:per_page] || @per_page
  params = options[:params] || {}
  params = params.merge({:per_page => per_page})
  paginate collection, :params => params, :remote => true
end

Remote links with default 'loader'.gif during request


149
150
151
152
153
154
155
156
# File 'app/helpers/application_helper.rb', line 149

def remote_link_to(text, options={})
  remote_options = {
    :before => "Element.show('loader')",
    :success => "Element.hide('loader')",
    :method => :get
  }
  link_to(text, options[:url], remote_options.merge(options))
end

#show_title?Boolean

Returns:

  • (Boolean)

127
128
129
# File 'app/helpers/application_helper.rb', line 127

def show_title?
  @show_title
end

#show_user(user = @current_user, options = {}) ⇒ Object

show a user, depending on settings


203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'app/helpers/application_helper.rb', line 203

def show_user(user=@current_user, options = {})
  if user.nil?
    "?"
  elsif FoodsoftConfig[:use_nick]
    if options[:full] && options[:markup]
      raw "<b>#{h user.nick}</b> (#{h user.first_name} #{h user.last_name})"
    elsif options[: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}" + (options[:unique] ? " (\##{user.id})" : '')
  end
end

render user presentation linking to default action (plugins can override this)


221
222
223
# File 'app/helpers/application_helper.rb', line 221

def show_user_link(user=@current_user)
  show_user user
end

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
# File 'app/helpers/application_helper.rb', line 59

def sort_link_helper(text, key, options = {})
  # Hmtl options
  remote = options[:remote].nil? ? true : options[:remote]
  class_name = case params[:sort]
                 when key then
                   'sortup'
                 when key + '_reverse' then
                   'sortdown'
                 else
                   nil
               end
  html_options = {
      :title => I18n.t('helpers.application.sort_by', text: text),
      :remote => remote,
      :class => class_name
  }


  # Url options
  key += "_reverse" if params[:sort] == key
  per_page = options[:per_page] || @per_page
  url_options = params.merge(per_page: per_page, sort: key)
  url_options.merge!({page: params[:page]}) if params[:page]
  url_options.merge!({query: params[:query]}) if params[:query]

  link_to(text, url_for(url_options), html_options)
end

#tab_is_active?(tab) ⇒ Boolean

Returns:

  • (Boolean)

131
132
133
# File 'app/helpers/application_helper.rb', line 131

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


122
123
124
125
# File 'app/helpers/application_helper.rb', line 122

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


226
227
228
229
230
231
232
233
234
# File 'app/helpers/application_helper.rb', line 226

def truncate(text, options={}, &block)
  return text if !text || text.length <= (options[:length] || 30)
  text_truncated = super(text, options, &block)
  if options[:tooltip]
     :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


116
117
118
119
# File 'app/helpers/application_helper.rb', line 116

def weekday(dayNumber)
  weekdays = I18n.t('date.day_names')
  return weekdays[dayNumber]
end