public
Description: The open source social networking platform in Ruby on Rails from the author of RailsSpace
Homepage: http://insoshi.com
Clone URL: git://github.com/insoshi/insoshi.git
Search Repo:
Michael Hartl (author)
Fri May 09 10:42:46 -0700 2008
commit  3044ee9172c5fa0402d97bda8a09d39223378402
tree    7b6df12845477ec807fd4799dea81f4b9f976ef3
parent  ca931746525b469a2384bb1414adc6a0186ab9bc
insoshi / app / helpers / application_helper.rb
100644 128 lines (114 sloc) 4.15 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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
# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper
  
  ## Menu helpers
  
  def menu
    home = menu_element("Home", home_path)
    people = menu_element("People", people_path)
    if Forum.count == 1
      forum = menu_element("Forum", forum_path(Forum.find(:first)))
    else
      forum = menu_element("Forums", forums_path)
    end
    resources = menu_element("Resources", "http://docs.insoshi.com/")
    if logged_in? and not admin_view?
      profile = menu_element("Profile", person_path(current_person))
      messages = menu_element("Messages", messages_path)
      blog = menu_element("Blog", blog_path(current_person.blog))
      photos = menu_element("Photos", photos_path)
      contacts = menu_element("Contacts",
                              person_connections_path(current_person))
      links = [home, profile, contacts, messages, blog, people, forum]
    elsif logged_in? and admin_view?
      home = menu_element("Home", home_path)
      people = menu_element("People", admin_people_path)
      forums = menu_element(inflect("Forum", Forum.count),
                             admin_forums_path)
      preferences = menu_element("Prefs", admin_preferences_path)
      links = [home, people, forums, preferences]
    else
      links = [home, people]
    end
    if global_prefs.about.blank?
      links
    else
      links.push(menu_element("About", about_url))
    end
  end
  
  def menu_element(content, address)
    { :content => content, :href => address }
  end
  
  def menu_link_to(link, options = {})
    link_to(link[:content], link[:href], options)
  end
  
  def menu_li(link, options = {})
    klass = "n-#{link[:content].downcase}"
    klass += " active" if current_page?(link[:href])
    content_tag(:li, menu_link_to(link, options), :class => klass)
  end
  
  # Return true if the user is viewing the site in admin view.
  def admin_view?
    params[:controller] =~ /admin/ and admin?
  end
  
  def admin?
    logged_in? and current_person.admin?
  end
  
  # Set the input focus for a specific id
  # Usage: <%= set_focus_to_id 'form_field_label' %>
  def set_focus_to_id(id)
    javascript_tag("$('#{id}').focus()");
  end
  
  # Display text by sanitizing and formatting.
  # The formatting is done by Markdown via the BlueCloth gem.
  # The html_options, if present, allow the syntax
  # display("foo", :class => "bar")
  # => '<p class="bar">foo</p>'
  def display(text, html_options = nil)
    if html_options
      html_options = html_options.stringify_keys
      tag_options = tag_options(html_options)
    else
      tag_options = nil
    end
    markdown(sanitize(text)).gsub("<p>", "<p#{tag_options}>")
  rescue
    # Sometimes Markdown throws exceptions, so rescue gracefully.
    content_tag(:p, sanitize(text))
  end
  
  # Output a column div.
  # The current two-column layout has primary & secondary columns.
  # The options hash is handled so that the caller can pass options to
  # content_tag.
  # The LEFT, RIGHT, and FULL constants are defined in
  # config/initializers/global_constants.rb
  def column_div(options = {}, &block)
    klass = options.delete(:type) == :primary ? "col1" : "col2"
    # Allow callers to pass in additional classes.
    options[:class] = "#{klass} #{options[:class]}".strip
    content = content_tag(:div, capture(&block), options)
    concat(content, block.binding)
  end
 
  def email_link(person, options = {})
    reply = options[:replying_to]
    if reply
      path = reply_message_path(reply)
    else
      path = new_person_message_path(person)
    end
    img = image_tag("icons/email.gif")
    action = reply.nil? ? "Send a message" : "Send reply"
    opts = { :class => 'email-link' }
    str = link_to(img, path, opts)
    str << "&nbsp;"
    str << link_to_unless_current(action, path, opts)
  end
 
  def formatting_note
    %(HTML and
#{link_to("Markdown",
                "http://daringfireball.net/projects/markdown/basics")}
supported)
  end
 
  private
  
    def inflect(word, number)
      number > 1 ? word.pluralize : word
    end
end