app/helpers/rostra/application_helper.rb in rostra-0.0.3 vs app/helpers/rostra/application_helper.rb in rostra-0.0.4

- old
+ new

@@ -1,4 +1,62 @@ module Rostra module ApplicationHelper + + # Creates a list of tags linking to...TODO: Where should these link? + # + def tag_list(question) + content_tag :div, "Tags: #{question.tags.map { |tag| link_to 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 <tt>Rostra::Config</tt> + # 2. Uses the users email address to looks for a gravatar + # 3. Renders <tt>app/assets/images/rostra/anonymous_avatar.png</tt> + # + 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, <tt>@question</tt> 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