Module: ApplicationHelper

Defined in:
app/helpers/application_helper.rb

Instance Method Summary collapse

Instance Method Details

#active_path(key) ⇒ Object



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

def active_path(key)
  Rails.cache.fetch "active_path_#{key}_#{I18n.locale}", expires_in: 4.hours do
    id = Settings["#{key}_id".gsub(/(?:_id)+$/, '_id')]
    if id
      structure_path(Structure.find_by(id: id))
    else
      root_path
    end
  end
end

#anchor_page_path(child, args = {}) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'app/helpers/application_helper.rb', line 91

def anchor_page_path(child, args = {})
  if child.parent.position_type == PositionType.composite
    prefix = args.delete :anchor_prefix
    args[:anchor] = "#{prefix.presence || ''}#{child.slug}"
    if child.parent.structure_type == StructureType.main
      root_path(args)
    else
      page_path(child.parent.slug, args)
    end
  else
    page_path(child.slug)
  end
end

#current_structure?(item) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
75
76
77
78
79
80
81
# File 'app/helpers/application_helper.rb', line 72

def current_structure?(item)
  return if item.nil?

  controller_factor = case item.structure_type
                      when StructureType.posts then params[:controller] == 'posts' && params[:action] == 'index'
                      else false
  end

  current_page?(structure_path(item)) || controller_factor
end

#embed_google_map_tag(query, params = {}) ⇒ Object



249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'app/helpers/application_helper.rb', line 249

def embed_google_map_tag(query, params = {})
  kind = params[:kind] || 'place'
  kind = 'view' if query =~ /^\d{1,3}(.\d{1,10})?,\d{1,3}(.\d{1,10})?$/

  case kind.to_s
  when 'view'
    params[:endpoint] ||= 'https://www.google.com/maps/embed/v1/view'
    query_key = 'center'
  else
    params[:endpoint] ||= 'https://www.google.com/maps/embed/v1/place'
    query_key = 'q'
  end
  params[:title] ||= @structure ? @structure.meta_tag(:title) || @structure.title : Settings.slogan
  params[:url] ||= request.url
  params[:src] = [
    params.delete(:endpoint),
    [
      ['key', ENV['GMAPS_API_KEY']],
      ['zoom', 18],
      [query_key, query]
    ].map { |p| p.join('=') }.join('&')
  ].join('?')
  params[:frameborder] ||= 0
  params[:style] ||= 'border:0'
  params[:allowfullscreen] ||= ''
   :iframe, nil, params
end

#entry_url(entry) ⇒ Object



17
18
19
20
21
22
23
24
# File 'app/helpers/application_helper.rb', line 17

def entry_url(entry)
  return root_url if entry.nil?

  case entry.class.name
  when 'Post' then post_url(entry)
  when 'Structure' then page_url(slug: entry.slug)
  end
end

#feature_enabled?(key) ⇒ Boolean

Returns:

  • (Boolean)


331
332
333
334
335
336
# File 'app/helpers/application_helper.rb', line 331

def feature_enabled?(key)
  Rails.cache.fetch "feature_#{key.hash}", expires_in: 4.hours do
    Settings[key].to_s.casecmp('true') == 0 ||
      Settings["enable_#{key}"].to_s.casecmp('true') == 0
  end
end

#feature_price(price, tag = :div, args = {}) ⇒ Object



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

def feature_price(price, tag = :div, args = {})
  currency = args[:currency_symbol] || '$'

  fprice = format('%g', price)
  fprice = (args[:money_tag], fprice) if args[:money_tag]
   tag, [currency, fprice].join.html_safe, class: 'global-currency-price', "data-price": price
end

#feed_item_url(item) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'app/helpers/application_helper.rb', line 62

def feed_item_url(item)
  return if item.nil?

  case item.class.name
  when 'Post' then post_url(item.slug)
  when 'Structure' then page_url(slug: item.slug)
  else root_url
  end
end

#file_provider_available?(provider) ⇒ Boolean

Returns:

  • (Boolean)


326
327
328
329
# File 'app/helpers/application_helper.rb', line 326

def file_provider_available?(provider)
  TranslationCms.available_file_providers.present? &&
    TranslationCms.available_file_providers.include?(provider.to_s)
end

#flag_css_by_code(country_code) ⇒ Object



317
318
319
320
321
322
323
324
# File 'app/helpers/application_helper.rb', line 317

def flag_css_by_code(country_code)
  return if country_code.nil?

  Rails.cache.fetch "flag_css_by_code_#{country_code}", expires_in: 1.day do
    country = ISO3166::Country.find_country_by_alpha3(country_code)
    country&.alpha2&.downcase
  end
end

#humanized_attribute(record, key, &block) ⇒ Object



310
311
312
313
314
315
# File 'app/helpers/application_helper.rb', line 310

def humanized_attribute(record, key, &block)
  return unless defined?(record.human_value)

  value = record.human_value(key)
  (defined?(::Rails) ? capture(value, &block) : yield(value)) if value.present?
end


204
205
206
207
208
209
# File 'app/helpers/application_helper.rb', line 204

def intent_tweet_link(name = nil, text = nil, url = nil, html_options = {}, &block)
  html_options = (html_options || {}).stringify_keys
  text = "\u201c#{text.truncate(140 - 3 - 23)}\u201d"
  html_options['href'] = "https://twitter.com/intent/tweet?text=#{text}&url=#{url}"
  (:a, name || text, html_options, &block)
end

#is_current_anchor_page?(child) ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
86
87
88
89
# File 'app/helpers/application_helper.rb', line 83

def is_current_anchor_page?(child)
  if child.parent.position_type == PositionType.composite
    current_structure? child.parent
  else
    current_structure? child
  end
end

#nofollow_page?Boolean

Returns:

  • (Boolean)


211
212
213
214
215
216
217
# File 'app/helpers/application_helper.rb', line 211

def nofollow_page?
  current_page?(new_order_path) ||
    controller_name == 'subscribers' ||
    TranslationCms::NOFOLLOW_SLUGS.include?(@structure.try(:slug)) ||
    (@structure.try(:position_type) == PositionType.bottom) ||
    writer_preview_page?
end

#noindex_follow_page?Boolean

Returns:

  • (Boolean)


219
220
221
# File 'app/helpers/application_helper.rb', line 219

def noindex_follow_page?
  paginated_page?
end

#payment_callback_url(options = {}) ⇒ Object



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

def payment_callback_url(options = {})
  payment = options[:payment]
  params = { provider: options[:provider], tr: payment.payment_token }

  case options[:mode]
  when :fail then payment.promo_uid.blank? ? payments_integrations_fail_url(params) : promo_fail_url(params)
  when :success then payment.promo_uid.blank? ? payments_integrations_success_url(params) : promo_success_url(params)
  end
end

#pretty_number(number, precision = 2, strip_zeros = true) ⇒ Object



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

def pretty_number(number, precision = 2, strip_zeros = true)
  number_with_precision(number, precision: precision, strip_insignificant_zeros: strip_zeros)
end


179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'app/helpers/application_helper.rb', line 179

def profile_nav_current_link(name = nil, html_options = {}, &block)
  # sample
  # <div class="profile_nav_current">
  #   <a href="javascript:void(0);">My Discounts <span class="bell active"></span></a>
  #   <b></b>
  # </div>
  if block_given?
    html_options = name
    name = nil
  end
  html_options = (html_options || {}).stringify_keys
  html_options[:class] = 'cabinet_nav_current' if html_options.blank?

  link = (:a, name, { href: 'javascript:void(0);' }, &block)
  drop = (:b)

  (:div, link + drop, html_options)
end

#rating_field(stars_count = 5, rating = 5, html_options = {}, &block) ⇒ Object



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'app/helpers/application_helper.rb', line 223

def rating_field(stars_count = 5, rating = 5, html_options = {}, &block)
  unique_id = html_options[:id].presence || Time.current.hash.abs
  ans = ''
  # prevent non-rated field when rate > stars
  rating = stars_count if rating > stars_count
  stars_count.times do |index|
    html_options[:id] = "rate_#{unique_id}_#{index}"
    if block_given?
      value = {
        uid: unique_id,
        value: index + 1,
        filled: index + 1 <= rating.floor,
        checked: index + 1 == rating.floor
      }
      ans << (defined?(::Rails) ? capture(value, &block) : yield(value))
    else
      ans << radio_button_tag("rate_#{unique_id}", index + 1, index + 1 == rating.floor, html_options)
    end
  end
  ans.html_safe
end

#render_submenu_block(options = {}) ⇒ Object



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

def render_submenu_block(options = {})
  action ||= controller.action_name
  class_name = controller.class.name.downcase
  namespace = (class_name.include?('::') ? class_name.split('::').first : nil)

  partials = options[:partials] || []
  partials << "#{controller.controller_path}/submenu_#{action}"
  partials << "#{controller.controller_path}/submenu"
  partials << "#{namespace}/submenu" if namespace
  partials << 'shared/submenu'

  partials.each do |pname|
    return render(partial: pname) if lookup_context.exists?(pname, nil, true)
  end

  ''
end

#satellite_price(price) ⇒ Object



55
56
57
58
59
60
# File 'app/helpers/application_helper.rb', line 55

def satellite_price(price)
  @_rate ||= TranslationCms.settings.base_rate
  number_to_currency(price.to_f * @_rate.value, unit: @_rate.symbol)
    .gsub(@_rate.symbol, (:sup, @_rate.symbol))
    .html_safe
end

#skype_to(skype, name = nil, html_options = {}, &block) ⇒ Object



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

def skype_to(skype, name = nil, html_options = {}, &block)
  if block_given?
    html_options = name
    name = nil
  end
  html_options = (html_options || {}).stringify_keys

  extras = %w[type topic].map! do |item|
    option = html_options.delete(item) || next
    if item =~ /type/i
      case option
      when /chat/i
        'chat'
      when /call/i
        'call'
      when /video/i
        'call&video=true'
      end
    else
      "#{item}=#{Rack::Utils.escape_path(option)}"
    end
  end.compact
  extras = extras.empty? ? '' : '?' + extras.join('&')

  html_options['href'] = "skype:#{skype}#{extras}"

  (:a, name || skype, html_options, &block)
end

#structure_path(record) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
# File 'app/helpers/application_helper.rb', line 4

def structure_path(record)
  return '#' if record.nil?

  case record.structure_type.kind
  when :page then page_path(slug: record.slug)
  when :redirect then (record.redirect_url.presence || '#')
  when :main then root_path
  when :group then '#'
  else
    page_path(slug: record.slug) if record.structure_type.pageable?
  end
end

#subscriber_partial_formObject



198
199
200
201
202
# File 'app/helpers/application_helper.rb', line 198

def subscriber_partial_form
  prefix = "#{params[:from]}_" if %w[prices posts discounts].include?(params[:from])

  "subscribers/#{prefix}form"
end

#subservice_path(structure) ⇒ Object



132
133
134
135
136
# File 'app/helpers/application_helper.rb', line 132

def subservice_path(structure)
  return if structure.blank? || !structure.service_page?

  service_page_path(structure.sub_service_params)
end

#tel_to(tel, name = nil, html_options = {}, &block) ⇒ Object



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

def tel_to(tel, name = nil, html_options = {}, &block)
  if block_given?
    html_options = name
    name = nil
  end
  html_options = (html_options || {}).stringify_keys

  html_options['href'] = "tel:#{tel}"

  (:a, name || tel, html_options, &block)
end

#upload_evaluation_attachment_path(params = {}) ⇒ Object



277
278
279
280
281
282
283
284
285
286
# File 'app/helpers/application_helper.rb', line 277

def upload_evaluation_attachment_path(params = {})
  params.reverse_merge!(
    assetable_type: 'OrderForm',
    assetable_id: 0,
    klass: 'PickedFile'
  )
  path = (params)
  # path.gsub!(%r{^/account/}, '/uploader/') unless account_signed_in?
  path
end

#upload_free_quote_attachment_path(params = {}) ⇒ Object



299
300
301
302
303
304
305
306
307
308
# File 'app/helpers/application_helper.rb', line 299

def upload_free_quote_attachment_path(params = {})
  params.reverse_merge!(
    assetable_type: 'FreeQuote',
    assetable_id: 0,
    klass: 'FreeQuoteFile',
    guid: '______guid______'
  )
  path = (params)
  path
end

#upload_order_attachment_path(params = {}) ⇒ Object



288
289
290
291
292
293
294
295
296
297
# File 'app/helpers/application_helper.rb', line 288

def upload_order_attachment_path(params = {})
  params.reverse_merge!(
    assetable_type: 'OrderForm',
    assetable_id: 0,
    klass: 'PickedFile'
  )
  path = (params)
  path.gsub!(%r{^/account/}, '/uploader/') unless 
  path
end

#widget_header(widget, alt_url = nil) ⇒ Object



123
124
125
126
127
128
129
130
# File 'app/helpers/application_helper.rb', line 123

def widget_header(widget, alt_url = nil)
  title = widget.render_title
  if widget.link_url.blank?
    alt_url ? link_to(title, alt_url) : title
  else
    link_to(title, widget.link_url)
  end
end