PageRenderTime 27ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/plugins/savage_beast/app/helpers/application_helper.rb

https://github.com/hosh/ansuz
Ruby | 96 lines | 76 code | 15 blank | 5 comment | 11 complexity | 8cb7cbe195452d36e900c15ecd462ae3 MD5 | raw file
Possible License(s): GPL-2.0, MIT
  1. require 'md5'
  2. module ApplicationHelper
  3. # convenient plugin point
  4. def head_extras
  5. end
  6. def submit_tag(value = "Save Changes"[], options={} )
  7. or_option = options.delete(:or)
  8. return super + "<span class='button_or'>"+"or"[]+" " + or_option + "</span>" if or_option
  9. super
  10. end
  11. def ajax_spinner_for(id, spinner="spinner.gif")
  12. "<img src='/plugin_assets/savage_beast/images/#{spinner}' style='display:none; vertical-align:middle;' id='#{id.to_s}_spinner'> "
  13. end
  14. def avatar_for(user, size=32)
  15. image_tag "http://www.gravatar.com/avatar.php?gravatar_id=#{MD5.md5(user.email)}&rating=PG&size=#{size}", :size => "#{size}x#{size}", :class => 'photo'
  16. end
  17. def beast_user_name
  18. (current_user ? current_user.display_name : "Guest" )
  19. end
  20. def beast_user_link
  21. user_link = (current_user ? user_path(current_user) : "#")
  22. link_to beast_user_name, user_link
  23. end
  24. def feed_icon_tag(title, url)
  25. (@feed_icons ||= []) << { :url => url, :title => title }
  26. link_to image_tag('feed-icon.png', :size => '14x14', :style => 'margin-right:5px', :alt => "Subscribe to #{title}", :plugin => "savage_beast"), url
  27. end
  28. def search_posts_title
  29. returning(params[:q].blank? ? 'Recent Posts'[] : "Searching for"[] + " '#{h params[:q]}'") do |title|
  30. title << " "+'by {user}'[:by_user,h(User.find(params[:user_id]).display_name)] if params[:user_id]
  31. title << " "+'in {forum}'[:in_forum,h(Forum.find(params[:forum_id]).name)] if params[:forum_id]
  32. end
  33. end
  34. def topic_title_link(topic, options)
  35. if topic.title =~ /^\[([^\]]{1,15})\]((\s+)\w+.*)/
  36. "<span class='flag'>#{$1}</span>" +
  37. link_to(h($2.strip), forum_topic_path(@forum, topic), options)
  38. else
  39. link_to(h(topic.title), forum_topic_path(@forum, topic), options)
  40. end
  41. end
  42. def search_posts_path(rss = false)
  43. options = params[:q].blank? ? {} : {:q => params[:q]}
  44. prefix = rss ? 'formatted_' : ''
  45. options[:format] = 'rss' if rss
  46. [[:user, :user_id], [:forum, :forum_id]].each do |(route_key, param_key)|
  47. return send("#{prefix}#{route_key}_posts_path", options.update(param_key => params[param_key])) if params[param_key]
  48. end
  49. options[:q] ? search_all_posts_path(options) : send("#{prefix}all_posts_path", options)
  50. end
  51. # on windows and this isn't working like you expect?
  52. # check: http://beast.caboo.se/forums/1/topics/657
  53. # strftime on windows doesn't seem to support %e and you'll need to
  54. # use the less cool %d in the strftime line below
  55. def distance_of_time_in_words(from_time, to_time = 0, include_seconds = false)
  56. from_time = from_time.to_time if from_time.respond_to?(:to_time)
  57. to_time = to_time.to_time if to_time.respond_to?(:to_time)
  58. distance_in_minutes = (((to_time - from_time).abs)/60).round
  59. case distance_in_minutes
  60. when 0..1 then (distance_in_minutes==0) ? 'a few seconds ago'[] : '1 minute ago'[]
  61. when 2..59 then "{minutes} minutes ago"[:minutes_ago, distance_in_minutes]
  62. when 60..90 then "1 hour ago"[]
  63. when 90..1440 then "{hours} hours ago"[:hours_ago, (distance_in_minutes.to_f / 60.0).round]
  64. when 1440..2160 then '1 day ago'[] # 1 day to 1.5 days
  65. when 2160..2880 then "{days} days ago"[:days_ago, (distance_in_minutes.to_f / 1440.0).round] # 1.5 days to 2 days
  66. else from_time.strftime("%b %e, %Y %l:%M%p"[:datetime_format]).gsub(/([AP]M)/) { |x| x.downcase }
  67. end
  68. end
  69. def pagination collection
  70. if collection.page_count > 1
  71. "<p class='pages'>" + 'Pages'[:pages_title] + ": <strong>" +
  72. will_paginate(collection, :inner_window => 10, :next_label => "next"[], :prev_label => "previous"[]) +
  73. "</strong></p>"
  74. end
  75. end
  76. def next_page collection
  77. unless collection.current_page == collection.page_count or collection.page_count == 0
  78. "<p style='float:right;'>" + link_to("Next page"[], { :page => collection.current_page.next }.merge(params.reject{|k,v| k=="page"})) + "</p>"
  79. end
  80. end
  81. end