module Rostra
module Base
module ApplicationHelper
# Snippet used to share a question on Twitter.
#
def twitter_sharing_link
raw %{
}
end
# Snippet used to share a question on Facebook.
#
def facebook_sharing_link
raw %{
}
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.
#
def link_to_profile(user)
link_to user.rostra_user_name, main_app.user_url(user)
end
# Used to add meta description tags to rostra pages
#
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
# Used to add meta keyword tags to rostra pages
#
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
# Used to populate both the +title+ and +h1+ elements for each page.
#
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
# Creates a list of tags linking to the index showing only questions with that tag
#
def tag_list(question)
tags = question.tags.map { |tag| link_to tag, questions_path(:tag_search => "#{tag}")}.join(', ')
content_tag :div, "Tags: #{tags}".html_safe, class: 'tags'
end
# 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+
#
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
# Method to build links for ajax-y voting arrows.
#
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 ? '▲' : '▼'
link_to entity_arrow.html_safe, vote_path(resource, direction), method: :put, remote: true, title: "vote #{direction}", class: "vote #{direction} #{selected}"
end
# Returns a rostra object's base class name. For example, +@question+ is an instance of
# Rostra::Question and so:
#
# class_name(@question) # => 'question'
#
def class_name(resource)
resource.class.name.split('::').last.downcase
end
# Checks for the current page using rails 3 route-style syntax:
#
# current_rostra_page?('questions#new')
#
def current_rostra_page?(controller_and_action)
controller_and_action == "#{controller_name}##{action_name}"
end
private
def vote_path(resource, direction)
return '#' if !can_participate_in_rostra? || can?(:manage, resource)
if resource.is_a?(Rostra::Question)
vote_question_path(resource, vote_direction: direction)
else
vote_question_answer_path(resource.question, resource, vote_direction: direction)
end
end
end
end
end