Module: ApplicationHelper

Included in:
BaseCell, CommentsController, PostsController
Defined in:
app/helpers/application_helper.rb

Instance Method Summary collapse

Instance Method Details

#benchmark(name) ⇒ Object


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

def benchmark(name)
  t0 = Time.now

  yield

  t1 = Time.now

  Rails.logger.debug "TEMPLATE BENCHMARK '#{name}': #{t1 - t0}"
end

#bootstrap_class_for(flash_type) ⇒ Object


68
69
70
# File 'app/helpers/application_helper.rb', line 68

def bootstrap_class_for(flash_type)
  { success: 'alert-success', error: 'alert-danger', alert: 'alert-warning', notice: 'alert-info' }[flash_type] || flash_type.to_s
end

#counter_tag(post_id, _options = {}) ⇒ Object


2
3
4
# File 'app/helpers/application_helper.rb', line 2

def counter_tag(post_id, _options = {})
  raw "<img src='/p/views/#{post_id}.png' alt='' title='' width=1, height=1 />"
end

#format_date(date, time: true) ⇒ Object


37
38
39
40
# File 'app/helpers/application_helper.rb', line 37

def format_date(date, time: true)
  f = time ? '%H:%M &nbsp;  %e %b %Y' : '%e %b %Y'
  raw(localize(date, format: f))
end

#glyphicon(name, _type = :bootstrap) ⇒ Object


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

def glyphicon(name, _type = :bootstrap)
  (:i, nil, class: "glyphicon glyphicon-#{name}")
end

#js_redirect_to(target) ⇒ Object


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

def js_redirect_to(target)
  javascript_tag do
    concat raw(
      <<-JS
    setTimeout(function() {
      window.location.href = "#{target}";
    }, 3000)
    JS
    )
  end
end

#recaptcha_tagsObject


87
88
89
90
91
92
# File 'app/helpers/application_helper.rb', line 87

def recaptcha_tags
  capture do
    concat javascript_include_tag('https://www.google.com/recaptcha/api.js')
    concat (:div, nil, class: 'g-recaptcha', 'data-sitekey' => ENV['RECAPTCHA_PUBLIC_KEY'].html_safe)
  end
end

#send_ga_eventsObject


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

def send_ga_events
  events = session.try(:[], :ga_events)

  if events.present?
    javascript_tag do
      events.each do |event|
        concat raw("ga('send', 'event', '#{event['category']}', '#{event['action']}', '#{event['label']}', #{event['value'] || 1}, {'nonInteraction': #{event['interaction'] ? 0 : 1}});")
      end

      # clear to not send twice
      session[:ga_events] = []
    end
  end
end

#smart_date(date) ⇒ Object


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/helpers/application_helper.rb', line 10

def smart_date(date)
  if date > 1.minute.ago
    t 'just_now'
  elsif date > 60.minutes.ago
    minutes = ((Time.now - date) / 1.minute).round
    unit = Russian.pluralize(minutes, 'minute', 'minutes', 'minutes_5')

    "#{minutes} #{t unit} #{t :ago}"
  elsif date.today?
    l date, format: '%H:%M'
  elsif date > Time.now.yesterday.beginning_of_day
    l date, format: "#{t(:yesterday)} %H:%M"
  elsif date > (Time.now - 7.days).beginning_of_day
    l date, format: '%A %H:%M'
  else
    l date, format: '%e %b %Y'
  end
end

#snippet(key, variables = {}, options = {}, &block) ⇒ Object


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/helpers/application_helper.rb', line 42

def snippet(key, variables = {}, options = {}, &block)
  snippet = Snippet.find_by(key: key)

  if snippet.present?
    snippet_body = if snippet.v2.present?
                     ab_variant? ? snippet.v2 : snippet.body
                   else
                     snippet.body
                   end

    if snippet_body.present?
      html = snippet_body.to_s

      template = Liquid::Template.parse(html) # Parses and compiles the template
      raw template.render(variables.stringify_keys)
    end
  elsif block_given?
    html = capture(&block)
    Snippet.create(key: key, body: html) unless options[:do_not_create_missing]

    html
  else
    Snippet.create(key: key).body unless options[:do_not_create_missing]
  end
end

#time_or_date(date) ⇒ Object


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

def time_or_date(date)
  if date > Time.now.yesterday.beginning_of_day
    date.strftime('%H:%M')
  else
    l date, format: '%e %b %Y'
  end
end