module Rostra
module ApplicationHelper
# Loop thru included helper files and include them in the project.
#
Rostra::Config.included_helpers.each do |helper_file|
include helper_file
end
# Creates a list of tags linking to the index showing only questions with that tag
#
def tag_list(question)
content_tag :div, "Tags: #{question.tags.map { |tag| link_to tag, questions_path(:tag_search => "#{tag}")}.join}".html_safe, class: 'tags'
end
# Finds the url to the user's avatar following this logic:
#
# 1. Calls the method defined in the Rostra::Config
# 2. Uses the users email address to looks for a gravatar
# 3. Renders app/assets/images/rostra/anonymous_avatar.png
#
def avatar_url(user)
avatar_method = Rostra::Config.rostra_user_avatar
if user.respond_to?(avatar_method)
user.send(avatar_method)
else
default_url = "#{main_app.root_url}assets/rostra/anonymous_avatar.png"
gravatar_id = Digest::MD5.hexdigest(user.rostra_user_email.downcase)
"http://gravatar.com/avatar/#{gravatar_id}.png?s=48&d=#{CGI.escape(default_url)}"
end
end
# Method to build links for ajax-y voting arrows.
#
def link_to_vote(direction, resource)
if user_signed_in? && ( (direction == :up && rostra_user.voted_for?(resource)) || (direction == :down && rostra_user.voted_against?(resource)) )
selected = 'selected'
else
selected = ''
end
link_to "Vote #{direction.capitalize}", vote_path(resource, direction), method: :put, remote: true, 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
private
def vote_path(resource, direction)
return '#' if !user_signed_in? || 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