Module: Rostra::Base::ApplicationHelper

Included in:
ApplicationHelper
Defined in:
app/helpers/rostra/base/application_helper.rb

Instance Method Summary collapse

Instance Method Details

#class_name(resource) ⇒ Object

Returns a rostra object’s base class name. For example, @question is an instance of Rostra::Question and so:

class_name(@question)  # => 'question'


109
110
111
# File 'app/helpers/rostra/base/application_helper.rb', line 109

def class_name(resource)
  resource.class.name.split('::').last.downcase
end

#current_rostra_page?(controller_and_action) ⇒ Boolean

Checks for the current page using rails 3 route-style syntax:

current_rostra_page?('questions#new')

Returns:

  • (Boolean)


117
118
119
# File 'app/helpers/rostra/base/application_helper.rb', line 117

def current_rostra_page?(controller_and_action)
  controller_and_action == "#{controller_name}##{action_name}"
end

Snippet used to share a question on Facebook.



16
17
18
19
20
21
# File 'app/helpers/rostra/base/application_helper.rb', line 16

def facebook_sharing_link
  raw %{
    <a name="fb_share"></a>
    <script src="http://static.ak.fbcdn.net/connect.php/js/FB.Share" type="text/javascript"></script>
  }
end

#include_meta_descriptionObject

Used to add meta description tags to rostra pages



32
33
34
35
36
37
38
# File 'app/helpers/rostra/base/application_helper.rb', line 32

def include_meta_description
  description = case "#{controller_name}##{action_name}"
  when "questions#show" then @question.title
  when "questions#index" then "Recently asked questions"
  end
  tag(:meta, { :name => "description", :content => description })
end

#include_meta_keywordsObject

Used to add meta keyword tags to rostra pages



42
43
44
45
46
47
48
# File 'app/helpers/rostra/base/application_helper.rb', line 42

def include_meta_keywords
  keywords = case "#{controller_name}##{action_name}"
  when "questions#show" then @question.tag_list.join(', ')
  when "questions#index" then "add, some, default, keywords"
  end
  tag(:meta, { :name => "keywords", :content => keywords })
end

Method used to render to user names (e.g. where it says: “John Does says:”). You can, for example, use this method to turn “John Doe” into a link to his profile page.



26
27
28
# File 'app/helpers/rostra/base/application_helper.rb', line 26

def link_to_profile(user)
  link_to user.rostra_user_name, main_app.user_url(user)
end

Method to build links for ajax-y voting arrows.



94
95
96
97
98
99
100
101
102
# File 'app/helpers/rostra/base/application_helper.rb', line 94

def link_to_vote(direction, resource)
  if can_participate_in_rostra? && ( (direction == :up && rostra_user.voted_for?(resource)) || (direction == :down && rostra_user.voted_against?(resource)) )
    selected = 'selected'
  else
    selected = ''
  end
  entity_arrow = direction == :up ? '&#x25B2;' : '&#x25BC;'
  link_to entity_arrow.html_safe, vote_path(resource, direction), method: :put, remote: true, title: "vote #{direction}", class: "vote #{direction} #{selected}"
end

#page_title_helperObject

Used to populate both the title and h1 elements for each page.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/helpers/rostra/base/application_helper.rb', line 52

def page_title_helper
  case "#{controller_name}##{action_name}"
  when "questions#show" then @question.title
  when "questions#index" then
    if params[:tag_search].present?
      "Recent Questions for tag #{params[:tag_search]}"
    else
      "Recent Questions"
    end
  when "questions#new" then "Post a new question"
  when "questions#edit" then "Editing question"
  when "answers#edit" then "Editing answer"
  else "Recent Questions"
  end
end

#rostra_user_avatar(user) ⇒ Object

Finds the url to the user’s avatar following this logic:

1. Calls +avatar+ on the user object
2. Uses the users email address to look for a gravatar
3. Renders +app/assets/images/rostra/anonymous_avatar.png+


81
82
83
84
85
86
87
88
89
90
# File 'app/helpers/rostra/base/application_helper.rb', line 81

def rostra_user_avatar(user)
  if user.respond_to?(:avatar)
    url = user.avatar
  else
    default_url = "#{main_app.root_url}assets/rostra/anonymous_avatar.png"
    gravatar_id = Digest::MD5.hexdigest(user.rostra_user_email.downcase)
    url = "http://gravatar.com/avatar/#{gravatar_id}.png?s=48&d=#{CGI.escape(default_url)}"
  end
  image_tag(url, class: 'avatar')
end

#tag_list(question) ⇒ Object

Creates a list of tags linking to the index showing only questions with that tag



70
71
72
73
# File 'app/helpers/rostra/base/application_helper.rb', line 70

def tag_list(question)
  tags = question.tags.map { |tag| link_to tag, questions_path(:tag_search => "#{tag}")}.join(', ')
   :div, "Tags: #{tags}".html_safe, class: 'tags'
end

Snippet used to share a question on Twitter.



7
8
9
10
11
12
# File 'app/helpers/rostra/base/application_helper.rb', line 7

def twitter_sharing_link
  raw %{
    <a href="https://twitter.com/share" class="twitter-share-button" data-count="none">Tweet</a>
    <script type="text/javascript" src="//platform.twitter.com/widgets.js"></script>
  }
end