Module: Calagator::ApplicationHelper

Includes:
TimeRangeHelper
Defined in:
app/helpers/calagator/application_helper.rb

Constant Summary collapse

FLASH_TYPES =
%i[success failure].freeze

Instance Method Summary collapse

Methods included from TimeRangeHelper

#normalize_time

Instance Method Details

#cache_if(condition, name = {}, &block) ⇒ Object

Caches block in view only if the condition is true. skionrails.wordpress.com/2008/05/22/conditional-fragment-caching/



60
61
62
63
64
65
66
# File 'app/helpers/calagator/application_helper.rb', line 60

def cache_if(condition, name = {}, &block)
  if condition
    cache(name, &block)
  else
    yield
  end
end

#datestamp(item) ⇒ Object

returns html markup with source (if any), imported/created time, and - if modified - modified time



45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/helpers/calagator/application_helper.rb', line 45

def datestamp(item)
  source = if item.source.nil?
             "added directly to #{Calagator.title}"
           else
             "imported from #{link_to truncate(item.source.name, length: 40), url_for(item.source)}"
  end
  created = " <br /><strong>#{normalize_time(item.created_at, format: :html)}</strong>"
  updated = if item.updated_at > item.created_at
              " and last updated <br /><strong>#{normalize_time(item.updated_at, format: :html)}</strong>"
  end
  raw "This item was #{source}#{created}#{updated}."
end

#datetime_format(time, format) ⇒ Object



34
35
36
37
# File 'app/helpers/calagator/application_helper.rb', line 34

def datetime_format(time, format)
  format = format.gsub(/(%[dHImU])/, '*\1')
  time.strftime(format).gsub(/\*0*/, '').html_safe
end

#format_description(string) ⇒ Object

Returns HTML string of an event or venue description for display in a view.



9
10
11
# File 'app/helpers/calagator/application_helper.rb', line 9

def format_description(string)
  sanitize(auto_link(upgrade_br(markdown(string))))
end

#markdown(text) ⇒ Object



13
14
15
# File 'app/helpers/calagator/application_helper.rb', line 13

def markdown(text)
  BlueCloth.new(text, relaxed: true).to_html
end

#render_flashObject



24
25
26
27
28
29
30
31
32
# File 'app/helpers/calagator/application_helper.rb', line 24

def render_flash
  FLASH_TYPES.map do |type|
    next if flash[type].blank?

    (:div, class: "flash #{type} flash_#{type}") do
      "#{type == :failure ? 'ERROR: ' : ''}#{flash[type]}".html_safe
    end
  end.compact.join.html_safe
end

#source_code_versionObject

Return a string describing the source code version being used



40
41
42
# File 'app/helpers/calagator/application_helper.rb', line 40

def source_code_version
  Calagator::VERSION
end


68
69
70
71
72
73
74
# File 'app/helpers/calagator/application_helper.rb', line 68

def subnav_class_for(controller_name, action_name)
  css_class = "#{controller.controller_name}_#{controller.action_name}_subnav"
  if [controller.controller_name, controller.action_name] == [controller_name, action_name]
    css_class += ' active'
  end
  css_class
end

#upgrade_br(content) ⇒ Object

Return a HTML string with the BR tags converted to XHTML compliant markup.



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

def upgrade_br(content)
  content.gsub('<br>', '<br />')
end