removes empty % sign when there are no votes

This commit is contained in:
rgarcia
2015-07-18 21:27:56 +02:00
parent 797160f108
commit afdb0980b0
3 changed files with 6 additions and 8 deletions

View File

@@ -4,4 +4,8 @@ module ApplicationHelper
debate.tag_list.map { |tag| link_to sanitize(tag), debates_path(tag: tag) }.join(', ').html_safe debate.tag_list.map { |tag| link_to sanitize(tag), debates_path(tag: tag) }.join(', ').html_safe
end end
def percentage(vote, debate)
return if debate.total_votes == 0
debate.send(vote).percent_of(debate.total_votes).to_s + "%"
end
end end

View File

@@ -12,12 +12,6 @@ class Debate < ActiveRecord::Base
validates :terms_of_service, acceptance: { allow_nil: false }, on: :create validates :terms_of_service, acceptance: { allow_nil: false }, on: :create
#vote can be 'likes' or 'dislikes'
def percentage(vote)
return if total_votes == 0
send(vote).percent_of(total_votes)
end
def likes def likes
get_likes.size get_likes.size
end end

View File

@@ -10,12 +10,12 @@
<div> <div>
<div id='in_favor'> <div id='in_favor'>
<%= link_to "up", debate_votes_path(@debate, value: 'yes'), method: "post" %> <%= link_to "up", debate_votes_path(@debate, value: 'yes'), method: "post" %>
<%= @debate.percentage('likes') %>% <%= percentage('likes', @debate) %>
</div> </div>
<div id='against'> <div id='against'>
<%= link_to "down", debate_votes_path(@debate, value: 'no'), method: "post" %> <%= link_to "down", debate_votes_path(@debate, value: 'no'), method: "post" %>
<%= @debate.percentage('dislikes') %>% <%= percentage('dislikes', @debate) %>
</div> </div>
Votos <%= @debate.total_votes %> Votos <%= @debate.total_votes %>