Module: YouthTree::Helpers::GeneralHelper

Defined in:
lib/youth_tree/helpers/general_helper.rb

Instance Method Summary collapse

Instance Method Details

Content with links converted to their absolute version. doesn’t take into account https etc.



46
47
48
49
50
51
52
53
54
# File 'lib/youth_tree/helpers/general_helper.rb', line 46

def absolutize_links(html)
  return unless defined?(Nokogiri)
  doc = Nokogiri::HTML(html)
  doc.search('a').each do |link|
    href = link['href'].to_s
    link['href'] = "http://#{request.host}#{href}" if href =~ /^\//
  end
  doc.at('*').to_html.html_safe
end

#ampm(t) ⇒ Object

Returns a nicely formated am/pm time with leading zeroes dropped.



18
19
20
# File 'lib/youth_tree/helpers/general_helper.rb', line 18

def ampm(t)
  t.strftime("%I:%M %p").gsub(/^0/, '')
end

#button_tag(text, opts = {}) ⇒ Object

Returns a button



57
58
59
# File 'lib/youth_tree/helpers/general_helper.rb', line 57

def button_tag(text, opts = {})
   :button, text, opts
end

Properly formatted copyright text.



80
81
82
83
84
85
86
87
88
# File 'lib/youth_tree/helpers/general_helper.rb', line 80

def copyright(year, now = Time.now)
  if now.year == year
    year.to_s
  elsif year / 1000 == now.year / 1000 # same century
    (year.to_s + "–" + now.year.to_s[-2..3]).html_safe
  else
    (year.to_s + "–" + now.year.to_s).html_safe
  end
end

#destroy_value(form) ⇒ Object

Returns value for _destroy.



96
97
98
# File 'lib/youth_tree/helpers/general_helper.rb', line 96

def destroy_value(form)
  form.object._destroy ? 1 : 0
end

#first_paragraph_of(text) ⇒ Object

Returns the first paragraph of text, requires nokogiri.



28
29
30
31
32
# File 'lib/youth_tree/helpers/general_helper.rb', line 28

def first_paragraph_of(text)
  if defined?(Nokogiri)
    Nokogiri::HTML(text).at('p').try(:to_html).try(:html_safe) || text
  end
end

#flash_messages(*names) ⇒ Object

Generate flash message html



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/youth_tree/helpers/general_helper.rb', line 112

def flash_messages(*names)
  names = names.select { |k| flash[k].present? }
  return if names.blank?
  content = []
  names.each_with_index do |key, idx|
    value = flash[key]
    first, last = (idx == 0), (idx == names.length - 1)
    content << (:p, value, :class => "flash #{key} #{"first" if first} #{"last" if last}".strip)
  end
  (:section, content.sum(ActiveSupport::SafeBuffer.new), :id => "flash-messages").html_safe
end

#formatted_published_time(object) ⇒ Object

Published at for almost_happy.



40
41
42
# File 'lib/youth_tree/helpers/general_helper.rb', line 40

def formatted_published_time(object)
  object.published_at.blank? ? "Not yet published" : l(object.published_at, :format => :long)
end

#inline_button(text, url, opts = {}) ⇒ Object

HTML for an inline button.



125
126
127
128
129
# File 'lib/youth_tree/helpers/general_helper.rb', line 125

def inline_button(text, url, opts = {})
  form_tag(url) do
    concat (:button, text, :type => "submit")
  end
end

Simple menu item shorthand.



74
75
76
# File 'lib/youth_tree/helpers/general_helper.rb', line 74

def menu_link(*args, &blk)
  (:li, link_to(*args, &blk), :class => 'menu-item')
end

#meta_tag(name, content) ⇒ Object

Returns a simple meta tag.



35
36
37
# File 'lib/youth_tree/helpers/general_helper.rb', line 35

def meta_tag(name, content)
  tag(:meta, :name => name.to_s, :content => content.to_s)
end

#partial(name, options = nil) ⇒ Object

Shorthand to render a partial, defaulting to passing object / locals instead of options.



63
64
65
66
67
68
69
70
71
# File 'lib/youth_tree/helpers/general_helper.rb', line 63

def partial(name, options = nil)
  opts = {:partial => name.to_s}
  if options.is_a?(Hash)
    opts[:locals] = options
  elsif options.present?
    opts[:object] = options
  end
  render opts
end

#pretty_date_range(from, to) ⇒ Object

Returns a nicely formatted date rang.



6
7
8
9
10
11
12
13
14
15
# File 'lib/youth_tree/helpers/general_helper.rb', line 6

def pretty_date_range(from, to)
  from_day, to_day = from.day.ordinalize, to.day.ordinalize
  if [from.day, from.month, from.year] == [to.day, to.month, to.year]
    "#{ampm from} to #{ampm to}, #{to_day} #{to.strftime "%B, %Y"}"
  elsif [from.month, from.year] == [to.month, to.year]
    "#{ampm from} #{from_day} to #{ampm to} #{to_day}, #{to.strftime "%B, %Y"}"
  else
    "#{ampm from} #{from_day}, #{from.strftime "%B, %Y"} to #{ampm to} #{to_day}, #{to.strftime "%B, %Y"}"
  end
end

#pretty_time(t) ⇒ Object

A prettier form of the time.



23
24
25
# File 'lib/youth_tree/helpers/general_helper.rb', line 23

def pretty_time(t)
  "#{ampm t}, #{t.day.ordinalize} #{t.strftime "%B, %Y"}"
end

#ssl_opts(opts = {}) ⇒ Object

URL opts with ssl merged.



107
108
109
# File 'lib/youth_tree/helpers/general_helper.rb', line 107

def ssl_opts(opts = {})
  opts.merge(:protocol => Settings.ssl_protocol)
end

#uri2ssl(url) ⇒ Object

URL with ssl included.



101
102
103
104
# File 'lib/youth_tree/helpers/general_helper.rb', line 101

def uri2ssl(url)
  parts = url.to_s.split("://", 2)
  "#{Settings.ssl_protocol}://#{parts[1] || parts[0]}"
end

#yes_no?(value) ⇒ Boolean

Simply boolean display

Returns:

  • (Boolean)


91
92
93
# File 'lib/youth_tree/helpers/general_helper.rb', line 91

def yes_no?(value)
  value ? "Yes" : "No"
end