Module: BaseHelper

Includes:
ActsAsTaggableOn::TagsHelper
Included in:
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



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'app/helpers/base_helper.rb', line 151

def add_friend_link(user = nil)
html = "<span class='friend_request' id='friend_request_#{user.id}'>"
  html += link_to_remote :request_friendship.l,
		{ :update => "friend_request_#{user.id}",
			:loading => "$$('span#friend_request_#{user.id} span.spinner')[0].show(); $$('span#friend_request_#{user.id} a.add_friend_btn')[0].hide()", 
			:complete => visual_effect(:highlight, "friend_request_#{user.id}", :duration => 1),
        500 => "alert('#{escape_javascript(:sorry_there_was_an_error_requesting_friendship.l)}')",
			:url => hash_for_user_friendships_url(:user_id => current_user.id, :friend_id => user.id), 
			:method => :post 
		}, {:class => "add_friend button"}
html +=	"<span style='display:none;' class='spinner'>"
html += image_tag('spinner.gif')
html += "#{:requesting_friendship.l} ...</span></span>"
html.html_safe
end

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



198
199
200
# File 'app/helpers/base_helper.rb', line 198

def ajax_spinner_for(id, spinner="spinner.gif")
  "<img src='/assets/#{spinner}' style='display:none; vertical-align:middle;' id='#{id.to_s}_spinner'> ".html_safe
end

#avatar_for(user, size = 32) ⇒ Object



202
203
204
# File 'app/helpers/base_helper.rb', line 202

def avatar_for(user, size=32)
  image_tag user.avatar_photo_url(:medium), :size => "#{size}x#{size}", :class => 'photo'
end

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



37
38
39
# File 'app/helpers/base_helper.rb', line 37

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



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

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

#city_cloud(cities, classes) ⇒ Object



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

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


186
187
188
# File 'app/helpers/base_helper.rb', line 186

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

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

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



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

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

#feed_icon_tag(title, url) ⇒ Object



206
207
208
209
# File 'app/helpers/base_helper.rb', line 206

def feed_icon_tag(title, url)
  (@feed_icons ||= []) << { :url => url, :title => title }
  link_to image_tag('feed.png', :size => '14x14', :alt => :subscribe_to.l+" #{title}", :plugin => 'community_engine'), url
end

#forum_page?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'app/helpers/base_helper.rb', line 15

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

#is_current_user_and_featured?(u) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#last_activeObject



194
195
196
# File 'app/helpers/base_helper.rb', line 194

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


175
176
177
178
179
180
# File 'app/helpers/base_helper.rb', line 175

def more_comments_links(commentable)
  html = link_to "&raquo; ".html_safe + :all_comments.l, commentable_comments_url(commentable.class.to_s.tableize, commentable.to_param)
  html += "<br />".html_safe
html += link_to "&raquo; ".html_safe + :comments_rss.l, commentable_comments_url(commentable.class.to_s.tableize, commentable.to_param, :format => :rss)
html.html_safe
end

#page_titleObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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
# File 'app/helpers/base_helper.rb', line 82

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


190
191
192
# File 'app/helpers/base_helper.rb', line 190

def paginating_links(paginator, options = {}, html_options = {})
  paginate paginator
end

#possesive(user) ⇒ Object



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

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

#profile_completeness(user) ⇒ Object



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'app/helpers/base_helper.rb', line 251

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

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

Yields:

  • (content)


23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/helpers/base_helper.rb', line 23

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



211
212
213
214
215
216
# File 'app/helpers/base_helper.rb', line 211

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



218
219
220
221
222
223
224
225
# File 'app/helpers/base_helper.rb', line 218

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)


182
183
184
# File 'app/helpers/base_helper.rb', line 182

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

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



227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'app/helpers/base_helper.rb', line 227

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..1339       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 days to 2 days
    else I18n.l(from_time.to_date, :format => :published_date)
  end
end

#time_ago_in_words_or_date(date) ⇒ Object



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

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 => :date_ago)
  end
end

#tiny_mce_init_if_neededObject



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

def tiny_mce_init_if_needed
  if !@uses_tiny_mce.blank?
    tinymce_js = tinymce_javascript(@tiny_mce_configuration)      
    javascript_tag(tinymce_js)
  end
end

#topnav_tab(name, options) ⇒ Object



167
168
169
170
171
172
173
# File 'app/helpers/base_helper.rb', line 167

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



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

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



66
67
68
69
# File 'app/helpers/base_helper.rb', line 66

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