Module: ReaderHelper

Includes:
Admin::RegionsHelper, SnailHelpers
Included in:
GroupTags, MessageTags, ReaderTags
Defined in:
app/helpers/reader_helper.rb

Instance Method Summary collapse

Instance Method Details

#choose_page(object, field, select_options = {}) ⇒ Object



103
104
105
106
107
108
# File 'app/helpers/reader_helper.rb', line 103

def choose_page(object, field, select_options={})
  root = Page.respond_to?(:homepage) ? Page.homepage : Page.find_by_parent_id(nil)
  options = page_option_branch(root)
  options.unshift [t("reader_extension.none_option"), nil]
  select object, field, options, select_options
end

#clean_html(text) ⇒ Object



57
58
59
# File 'app/helpers/reader_helper.rb', line 57

def clean_html(text)
  Sanitize.clean(textilize(text), Sanitize::Config::GENEROUS)
end

#country_options_for_select(selected = nil, default_selected = Snail.home_country) ⇒ Object



141
142
143
# File 'app/helpers/reader_helper.rb', line 141

def country_options_for_select(selected = nil, default_selected = Snail.home_country)
  usps_country_options_for_select(selected, default_selected)
end


153
154
155
# File 'app/helpers/reader_helper.rb', line 153

def email_link(address)
  mail_to address, nil, :encode => :hex, :replace_at => ' at ', :class => 'mailto'
end

#friendly_date(datetime) ⇒ Object



121
122
123
# File 'app/helpers/reader_helper.rb', line 121

def friendly_date(datetime)
  I18n.l(datetime, :format => friendly_date_format(datetime)) if datetime
end

#friendly_date_format(datetime) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'app/helpers/reader_helper.rb', line 125

def friendly_date_format(datetime)
  if datetime && date = datetime.to_date
    if (date.to_datetime == Date.today)
      :today
    elsif (date.to_datetime == Date.yesterday)
      :yesterday
    elsif (date.to_datetime > 6.days.ago)
      :recently
    elsif (date.year == Date.today.year)
      :this_year
    else
      :standard
    end
  end
end

#gravatar_for(reader = nil, gravatar_options = {}, img_options = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'app/helpers/reader_helper.rb', line 16

def gravatar_for(reader=nil, gravatar_options={}, img_options ={})
  size = gravatar_options[:size] || 40
  img_options[:size] ||= "#{size}x#{size}"
  gravatar_options[:default] ||= "#{request.protocol}#{request.host_with_port}/images/furniture/no_gravatar.png"
  img_options[:alt] ||= reader.preferred_name if reader
  if reader.nil? || reader.email.blank?
    image_tag gravatar_options[:default], img_options
  else
    image_tag gravatar_url(reader.email, gravatar_options), img_options
  end
end

#group_options_for_selectObject



145
146
147
# File 'app/helpers/reader_helper.rb', line 145

def group_options_for_select
  options_from_tree(Group.arrange).unshift([t("reader_extension.any_option"), nil])
end


48
49
50
51
# File 'app/helpers/reader_helper.rb', line 48

def home_page_link(options={})
  home_page = Page.find_by_parent_id(nil)
  link_to home_page.title, home_page.url, options
end


36
37
38
39
40
41
42
# File 'app/helpers/reader_helper.rb', line 36

def link_to_group(group)
  if page = group.homepage
    link_to group.name, page.url
  else
    link_to group.name, group_url(group)
  end
end


44
45
46
# File 'app/helpers/reader_helper.rb', line 44

def link_to_message(message)
  link_to message.subject, message_url(message)
end


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

def link_to_reader(reader)
  if page = ReaderPage.first
    page.url_for(reader)
  else
    reader_url(reader)
  end
end

#message_preview(subject, body, reader) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'app/helpers/reader_helper.rb', line 88

def message_preview(subject, body, reader)
  preview = <<EOM
From: #{current_user.name} &lt;#{current_user.email}&gt;
To: #{reader.preferred_name} &lt;#{reader.email}&gt;
Date: #{Time.now.to_date.to_s :long}
<strong>Subject: #{subject}</strong>

Dear #{reader.preferred_name},

#{body}

EOM
simple_format(preview)
end

#options_from_tree(tree, options = {}) ⇒ Object

Takes a hash of hashes in the format returned by Ancestry#arrange, returns an indented option set.



159
160
161
162
163
164
165
166
167
168
169
170
# File 'app/helpers/reader_helper.rb', line 159

def options_from_tree(tree, options={})
  option_list = []
  depth = options[:depth] || 0
  exception = options[:except]
  tree.each_pair do |key, values|
    unless key == exception
      option_list << [". " * depth + key.name, key.id]
      option_list << options_from_tree(values, :depth => depth + 1, :except => exception) if values.any?
    end
  end
  option_list.compact
end

#page_option_branch(page, depth = 0) ⇒ Object



110
111
112
113
114
115
116
117
118
119
# File 'app/helpers/reader_helper.rb', line 110

def page_option_branch(page, depth=0)
  options = []
  unless page.title.first == '_'
    options << ["#{". " * depth}#{h(page.title)}", page.id]
    page.children.each do |child|
      options += page_option_branch(child, depth + 1)
    end
  end
  options
end

#pagination_and_summary_for(list, name = '') ⇒ Object



71
72
73
74
75
76
77
78
79
# File 'app/helpers/reader_helper.rb', line 71

def pagination_and_summary_for(list, name='')
  %{<div class="pagination">
      #{will_paginate list, :container => false}
      <span class="pagination_summary">
        #{pagination_summary(list, name)}
      </span>
    </div>
  }
end

#pagination_summary(list, name = '') ⇒ Object



81
82
83
84
85
86
# File 'app/helpers/reader_helper.rb', line 81

def pagination_summary(list, name='')
  total = list.total_entries
  start = list.offset + 1
  finish = ((list.offset + list.per_page) < list.total_entries) ? list.offset + list.per_page : list.total_entries
  t("reader_extension.showing_of_total", :count => total, :start => start, :finish => finish, :name => name, :names => name.pluralize)
end

#parent_group_options_for_select(group = nil) ⇒ Object



149
150
151
# File 'app/helpers/reader_helper.rb', line 149

def parent_group_options_for_select(group=nil)
  options_from_tree(Group.arrange, :except => group).unshift([t("reader_extension.none_option"), nil])
end

#scrub_html(text) ⇒ Object



53
54
55
# File 'app/helpers/reader_helper.rb', line 53

def scrub_html(text)
  Sanitize.clean(textilize(text))
end

#standard_gravatar_for(reader = nil, url = nil) ⇒ Object



9
10
11
12
13
14
# File 'app/helpers/reader_helper.rb', line 9

def standard_gravatar_for(reader=nil, url=nil)
  size = Radiant::Config['reader.gravatar_size'] || 40
  url ||= reader_url(reader)
  gravatar = gravatar_for(reader, {:size => size}, {:class => 'gravatar offset', :width => size, :height => size})
  (:div, link_to(gravatar, url), :class => "speaker", :width => size, :height => size)
end

#truncate_words(text = '', options = {}) ⇒ Object



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

def truncate_words(text='', options={})
  return '' if text.blank?
  options = {:length => options} unless options.is_a? Hash
  options.reverse_merge!(:length => 30, :omission => '&hellip;')
  words = text.split
  length = options[:length].to_i
  options[:omission] = '' unless words.size > length
  words[0..(length-1)].join(" ") + options[:omission]
end