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'


123
124
125
# File 'app/helpers/rostra/base/application_helper.rb', line 123

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)


131
132
133
# File 'app/helpers/rostra/base/application_helper.rb', line 131

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
# File 'app/helpers/rostra/base/application_helper.rb', line 16

def facebook_sharing_link(question)
  raw %{
    <div class="fb-like" data-href="#{question_url(question)}" data-send="true" data-layout="button_count" data-width="450" data-show-faces="true" data-font="arial"></div>
  }
end

Javascript snippet required by facebook to support share links.



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

def include_facebook_sharing_link_javascript
  raw %{
    <div id="fb-root"></div>
    <script>(function(d, s, id) {
      var js, fjs = d.getElementsByTagName(s)[0];
      if (d.getElementById(id)) return;
      js = d.createElement(s); js.id = id;
      js.src = "//connect.facebook.net/en_US/all.js#xfbml=1";
      fjs.parentNode.insertBefore(js, fjs);
    }(document, 'script', 'facebook-jssdk'));</script>
  }
end

#include_meta_descriptionObject

Used to add meta description tags to rostra pages



46
47
48
49
50
51
52
# File 'app/helpers/rostra/base/application_helper.rb', line 46

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



56
57
58
59
60
61
62
# File 'app/helpers/rostra/base/application_helper.rb', line 56

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.



40
41
42
# File 'app/helpers/rostra/base/application_helper.rb', line 40

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.



108
109
110
111
112
113
114
115
116
# File 'app/helpers/rostra/base/application_helper.rb', line 108

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.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/helpers/rostra/base/application_helper.rb', line 66

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+


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

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



84
85
86
87
# File 'app/helpers/rostra/base/application_helper.rb', line 84

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