Module: BaseHelper

Includes:
ActsAsTaggableOn::TagsHelper, Ckeditor::ApplicationHelper
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



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

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



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

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



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

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



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

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



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

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

#city_cloud(cities, classes) ⇒ Object



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

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


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

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



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

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



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
240
# File 'app/helpers/base_helper.rb', line 181

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



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

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



350
351
352
353
354
355
356
# File 'app/helpers/base_helper.rb', line 350

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)


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

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

#is_current_user_and_featured?(u) ⇒ Boolean

Returns:

  • (Boolean)


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

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

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



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

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

#last_activeObject



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

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


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

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



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
179
# File 'app/helpers/base_helper.rb', line 112

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


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

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

#possesive(user) ⇒ Object



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

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

#profile_completeness(user) ⇒ Object



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

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



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

def render_jumbotron
  if @jumbotron
    @jumbotron.html_safe
  end
end

#render_widgetsObject



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

def render_widgets
  if @widgets
    @widgets.html_safe
  end
end

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

Yields:

  • (content)


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

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



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

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



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

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)


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

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

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



358
359
360
361
362
363
# File 'app/helpers/base_helper.rb', line 358

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



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

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



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

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

#topnav_tab(name, options) ⇒ Object



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

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



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

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



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

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

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



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

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