Module: BaseHelper

Includes:
ActsAsTaggableOn::TagsHelper
Included in:
BaseController, UserNotifier
Defined in:
app/helpers/base_helper.rb

Overview

Methods added to this helper will be available to all templates in the application.

Instance Method Summary collapse

Instance Method Details



241
242
243
244
245
# File 'app/helpers/base_helper.rb', line 241

def add_friend_link(user = nil)
html = ""
  html << render(:partial => 'shared/add_friend_link', :locals => {:user => user})
html.html_safe
end

#ajax_spinner_for(id, spinner = "spinner.gif") ⇒ Object



278
279
280
# File 'app/helpers/base_helper.rb', line 278

def ajax_spinner_for(id, spinner="spinner.gif")
  image_tag spinner, class: 'hide', id: "#{id.to_s}_spinner"
end

#avatar_for(user, size = 32) ⇒ Object



282
283
284
# File 'app/helpers/base_helper.rb', line 282

def avatar_for(user, size=32)
  image_tag user.avatar_photo_url(:thumb), :class => 'thumbnail'
end

#block_to_partial(partial_name, html_options = {}, &block) ⇒ Object



41
42
43
# File 'app/helpers/base_helper.rb', line 41

def block_to_partial(partial_name, html_options = {}, &block)
  concat(render(:partial => partial_name, :locals => {:body => capture(&block), :html_options => html_options}))
end

#box(html_options = {}, &block) ⇒ Object



45
46
47
# File 'app/helpers/base_helper.rb', line 45

def box(html_options = {}, &block)
  block_to_partial('shared/box', html_options, &block)
end

#city_cloud(cities, classes) ⇒ Object



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

def city_cloud(cities, classes)
  max, min = 0, 0
  cities.each { |c|
    max = c.users.size.to_i if c.users.size.to_i > max
    min = c.users.size.to_i if c.users.size.to_i < min
  }

  divisor = ((max - min) / classes.size) + 1

  cities.each { |c|
    yield c, classes[(c.users.size.to_i - min) / divisor]
  }
end


266
267
268
# File 'app/helpers/base_helper.rb', line 266

def clippings_link
  "javascript:(function() {d=document, w=window, e=w.getSelection, k=d.getSelection, x=d.selection, s=(e?e():(k)?k():(x?x.createRange().text:0)), e=encodeURIComponent, document.location='#{home_url}new_clipping?uri='+e(document.location)+'&title='+e(document.title)+'&selection='+e(s);} )();"
end

#commentable_url(comment) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
# File 'app/helpers/base_helper.rb', line 7

def commentable_url(comment)
  if comment.recipient && comment.commentable
    if comment.commentable_type != "User"
      polymorphic_url([comment.recipient, comment.commentable])+"#comment_#{comment.id}"
    elsif comment
      user_url(comment.recipient)+"#comment_#{comment.id}"
    end
  elsif comment.commentable
    polymorphic_url(comment.commentable)+"#comment_#{comment.id}"
  end
end

#container_titleObject



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'app/helpers/base_helper.rb', line 180

def container_title
  app_base = configatron.community_name
  title = app_base

  case controller.controller_name
    when 'pages'
      if @page and @page.title
        title = @page.title
      end
    when 'posts'
      if @post and @post.title
        title = @post.title
        @canonical_url = user_post_url(@post.user, @post)
      end
    when 'users'
      if @user && !@user.new_record? && @user.
        title = @user.
        @canonical_url = user_url(@user)
      else
        title = :showing_users.l
      end
    when 'photos'
      if @user and @user.
        title = :users_photos.l(:user => @user.)
      end
    when 'clippings'
      if @user and @user.
        title = :user_clippings.l(:user => @user.)
      end
    when 'tags'
      case controller.action_name
        when 'show'
          if params[:type]
            title = I18n.translate('all_' + params[:type].downcase.pluralize + '_tagged', :tag_name => @tags.map(&:name).join(', '))
          else
            title = :posts_photos_and_bookmarks.l(:name => @tags.map(&:name).join(', '))
          end
          title += " (#{:related_tags.l}: #{@related_tags.join(', ')})" if @related_tags
          @canonical_url = tag_url(URI.escape(URI.escape(@tags_raw), /[\/.?#]/)) if @tags_raw
        else
          title = "Showing tags"
        end
    when 'categories'
      if @category and @category.name
        title = :posts_photos_and_bookmarks.l(:name => @category.name)
      else
        title = :showing_categories.l
      end
    when 'sessions'
      title = :login.l
  end

  if @page_title
    title = @page_title
  elsif title == app_base
    title = :showing.l + ' ' + controller.controller_name
  end

  title
end

#excerpt_with_jump(text, end_string = ' ...') ⇒ Object



100
101
102
103
104
105
106
107
108
109
# File 'app/helpers/base_helper.rb', line 100

def excerpt_with_jump(text, end_string = ' ...')
  return if text.blank?
  doc = Hpricot( text )
  paragraph = doc.at("p")
  if paragraph
    paragraph.to_html + end_string
  else
    truncate_words(text, 150, end_string)
  end
end

#flash_class(level) ⇒ Object



360
361
362
363
364
365
366
# File 'app/helpers/base_helper.rb', line 360

def flash_class(level)
  case level
    when :notice then "alert-info"
    when :error then "alert-danger"
    when :alert then "alert-warning"
  end
end

#forum_page?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'app/helpers/base_helper.rb', line 19

def forum_page?
  %w(forums topics sb_posts).include?(controller.controller_name)
end

#is_current_user_and_featured?(u) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
# File 'app/helpers/base_helper.rb', line 23

def is_current_user_and_featured?(u)
  u && u.eql?(current_user) && u.featured_writer?
end

#jumbotron(html_options = {}, &block) ⇒ Object



63
64
65
66
# File 'app/helpers/base_helper.rb', line 63

def jumbotron(html_options = {}, &block)
  @jumbotron = render(:partial => 'shared/jumbotron', :locals => {:body => capture(&block), :html_options => html_options})
  return ''
end

#last_activeObject



274
275
276
# File 'app/helpers/base_helper.rb', line 274

def last_active
  session[:last_active] ||= Time.now.utc
end


255
256
257
258
259
260
# File 'app/helpers/base_helper.rb', line 255

def more_comments_links(commentable)
  html = link_to fa_icon('plus-circle', :text => :all_comments.l), commentable_comments_url(commentable.class.to_s.tableize, commentable.to_param)
  html += "<br />".html_safe
  html += link_to fa_icon('rss', :text => :comments_rss.l), commentable_comments_url(commentable.class.to_s.tableize, commentable.to_param, :format => :rss)
  html.html_safe
end

#page_titleObject



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
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
178
# File 'app/helpers/base_helper.rb', line 111

def page_title
  divider = " | ".html_safe

  app_base = configatron.community_name
  tagline = " #{divider} #{configatron.community_tagline}"
title = app_base

case controller.controller_name
	when 'base'
      title += tagline
    when 'pages'
      if @page and @page.title
        title = @page.title + divider + app_base + tagline
      end
	when 'posts'
      if @post and @post.title
        title = @post.title + divider + app_base + tagline
        title += (@post.tags.empty? ? '' : "#{divider}#{:keywords.l}: " + @post.tags[0...4].join(', ') )
        @canonical_url = user_post_url(@post.user, @post)
      end
    when 'users'
      if @user && !@user.new_record? && @user.
        title = @user.
        title += divider + app_base + tagline
        @canonical_url = user_url(@user)
      else
        title = :showing_users.l+divider + app_base + tagline
      end
    when 'photos'
      if @user and @user.
        title = :users_photos.l(:user => @user.) + divider + app_base + tagline
      end
    when 'clippings'
      if @user and @user.
        title = :user_clippings.l(:user => @user.) + divider + app_base + tagline
      end
    when 'tags'
      case controller.action_name
        when 'show'
          if params[:type]
            title = I18n.translate('all_' + params[:type].downcase.pluralize + '_tagged', :tag_name => @tags.map(&:name).join(', '))
          else
            title = :posts_photos_and_bookmarks.l(:name => @tags.map(&:name).join(', '))
          end
          title += " (#{:related_tags.l}: #{@related_tags.join(', ')})" if @related_tags
          title += divider + app_base
          @canonical_url = tag_url(URI.escape(URI.escape(@tags_raw), /[\/.?#]/)) if @tags_raw
        else
          title = "Showing tags #{divider} #{app_base} #{tagline}"
        end
    when 'categories'
      if @category and @category.name
        title = :posts_photos_and_bookmarks.l(:name => @category.name) + divider + app_base + tagline
      else
        title = :showing_categories.l + divider + app_base + tagline
      end
    when 'sessions'
      title = :login.l + divider + app_base + tagline
  end

  if @page_title
    title = @page_title + divider + app_base + tagline
  elsif title == app_base
  title = :showing.l + ' ' + controller.controller_name + divider + app_base + tagline
  end

  title
end


270
271
272
# File 'app/helpers/base_helper.rb', line 270

def paginating_links(paginator, options = {}, html_options = {})
  paginate paginator, :theme => 'bootstrap'
end

#possesive(user) ⇒ Object



345
346
347
# File 'app/helpers/base_helper.rb', line 345

def possesive(user)
  user.gender ? (user.male? ? :his.l : :her.l)  : :their.l
end

#profile_completeness(user) ⇒ Object



326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'app/helpers/base_helper.rb', line 326

def profile_completeness(user)
  segments = [
    {:val => 2, :action => link_to(:upload_a_profile_photo.l, edit_user_path(user, :anchor => 'profile_details')), :test => !user.avatar.nil? },
    {:val => 1, :action => link_to(:tell_us_about_yourself.l, edit_user_path(user, :anchor => 'user_description')), :test => !user.description.blank?},
    {:val => 2, :action => link_to(:select_your_city.l, edit_user_path(user, :anchor => 'location_chooser')), :test => !user.metro_area.nil? },
    {:val => 1, :action => link_to(:tag_yourself.l, edit_user_path(user, :anchor => "user_tags")), :test => user.tags.any?},
    {:val => 1, :action => link_to(:invite_some_friends.l, new_invitation_path), :test => user.invitations.any?}
  ]

  completed_score = segments.select{|s| s[:test].eql?(true)}.sum{|s| s[:val]}
  incomplete = segments.select{|s| !s[:test] }

  total = segments.sum{|s| s[:val] }
  score = (completed_score.to_f/total.to_f)*100

  {:score => score, :incomplete => incomplete, :total => total}
end

#render_jumbotronObject



68
69
70
71
72
# File 'app/helpers/base_helper.rb', line 68

def render_jumbotron
  if @jumbotron
    @jumbotron.html_safe
  end
end

#render_widgetsObject



57
58
59
60
61
# File 'app/helpers/base_helper.rb', line 57

def render_widgets
  if @widgets
    @widgets.html_safe
  end
end

#rounded(options = {}) {|content| ... } ⇒ Object

Yields:

  • (content)


27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/helpers/base_helper.rb', line 27

def rounded(options={}, &content)
  options = {:class=>"box"}.merge(options)
  options[:class] = "box " << options[:class] if options[:class]!="box"

  str = '<div'
  options.collect {|key,val| str << " #{key}=\"#{val}\"" }
  str << '><div class="box_top"></div>'
  str << "\n"

  concat(str.html_safe)
  yield(content)
  concat('<br class="clear" /><div class="box_bottom"></div></div>'.html_safe)
end

#search_posts_titleObject



286
287
288
289
290
291
# File 'app/helpers/base_helper.rb', line 286

def search_posts_title
  (params[:q].blank? ? :recent_posts.l : :searching_for.l+" '#{h params[:q]}'").tap do |title|
    title << " by #{h User.find(params[:user_id]).display_name}" if params[:user_id]
    title << " in #{h Forum.find(params[:forum_id]).name}"       if params[:forum_id]
  end
end

#search_user_posts_path(rss = false) ⇒ Object



293
294
295
296
297
298
299
300
# File 'app/helpers/base_helper.rb', line 293

def search_user_posts_path(rss = false)
  options = params[:q].blank? ? {} : {:q => params[:q]}
  options[:format] = :rss if rss
  [[:user, :user_id], [:forum, :forum_id]].each do |(route_key, param_key)|
    return send("#{route_key}_sb_posts_path", options.update(param_key => params[param_key])) if params[param_key]
  end
  options[:q] ? search_all_sb_posts_path(options) : send("all_#{prefix}sb_posts_path", options)
end

Returns:

  • (Boolean)


262
263
264
# File 'app/helpers/base_helper.rb', line 262

def show_footer_content?
  return true #you can override this in your app
end

#tag_auto_complete_field(id, options = {}) ⇒ Object



368
369
370
371
372
373
# File 'app/helpers/base_helper.rb', line 368

def tag_auto_complete_field(id, options = {})
  options[:url][:format] = 'json'
  html = ""
  html << render(:partial => 'shared/tag_auto_complete', :locals => {:id => id, :options => options})
  html.html_safe
end

#time_ago_in_words(from_time, to_time = Time.now, include_seconds = false) ⇒ Object



302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'app/helpers/base_helper.rb', line 302

def time_ago_in_words(from_time, to_time = Time.now, include_seconds = false)
  from_time = from_time.to_time if from_time.respond_to?(:to_time)
  to_time = to_time.to_time if to_time.respond_to?(:to_time)
  distance_in_minutes = (((to_time - from_time).abs)/60).round

  case distance_in_minutes
    when 0              then :a_few_seconds_ago.l
    when 1..59          then :minutes_ago.l(:count => distance_in_minutes)
    when 60..1440       then :hours_ago.l(:count => (distance_in_minutes.to_f / 60.0).round)
    when 1440..2880     then :days_ago.l(:count => (distance_in_minutes.to_f / 1440.0).round) # 1.5 days to 2 days
    else I18n.l(from_time, :format => :short)
  end
end

#time_ago_in_words_or_date(date) ⇒ Object



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

def time_ago_in_words_or_date(date)
  if date.to_date.eql?(Time.now.to_date)
    display = I18n.l(date.to_time.localtime, :format => :time_ago)
  elsif date.to_date.eql?(Time.now.to_date - 1)
    display = :yesterday.l
  else
    display = I18n.l(date.to_date, :format => :short)
  end
end

#tiny_mce_init_if_neededObject



349
350
351
352
353
# File 'app/helpers/base_helper.rb', line 349

def tiny_mce_init_if_needed
  if !@uses_tiny_mce.blank?
    javascript_tag(tiny_mce_js)
  end
end

#tiny_mce_jsObject



355
356
357
358
# File 'app/helpers/base_helper.rb', line 355

def tiny_mce_js
  selector = @tiny_mce_configuration['selector']
  "jQuery(function(){jQuery('#{selector}').RichTextEditor(#{@tiny_mce_configuration.to_json})});".html_safe
end

#topnav_tab(name, options) ⇒ Object



247
248
249
250
251
252
253
# File 'app/helpers/base_helper.rb', line 247

def topnav_tab(name, options)
  classes = [options.delete(:class)]
  classes << 'current' if options[:section] && (options.delete(:section).to_a.include?(@section))

  string = "<li class='#{classes.join(' ')}'>" + link_to( (:span, name), options.delete(:url), options) + "</li>"
  string.html_safe
end

#truncate_words(text, length = 30, end_string = '...') ⇒ Object



88
89
90
91
92
93
# File 'app/helpers/base_helper.rb', line 88

def truncate_words(text, length = 30, end_string = '...')
  return if text.blank?
  words = strip_tags(text).split()
  string = words[0..(length-1)].join(' ') + (words.length > length ? end_string : '')
  string.html_safe
end

#truncate_words_with_highlight(text, phrase) ⇒ Object



95
96
97
98
# File 'app/helpers/base_helper.rb', line 95

def truncate_words_with_highlight(text, phrase)
  t = excerpt(text, phrase)
  highlight truncate_words(t, 18), phrase
end

#widget(html_options = {}, &block) ⇒ Object



51
52
53
54
55
# File 'app/helpers/base_helper.rb', line 51

def widget(html_options = {}, &block)
  @widgets ||= ''
  @widgets << render(:partial => 'shared/widget', :locals => {:body => capture(&block), :html_options => html_options})
  return ''
end