Merge pull request #149 from dgilperez/issue-144

Limitar número de etiquetas mostradas issue#144
This commit is contained in:
Raimond Garcia
2015-08-12 17:40:07 +02:00
6 changed files with 71 additions and 9 deletions

View File

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

View File

@@ -42,6 +42,17 @@ class Debate < ActiveRecord::Base
super.try :html_safe
end
def tag_list_with_limit(limit = nil)
tags.most_used(limit).pluck :name
end
def tags_count_out_of_limit(limit = nil)
return 0 unless limit
count = tags.count - limit
count < 0 ? 0 : count
end
protected
def sanitize_description

View File

@@ -15,7 +15,7 @@
<%= link_to debate.description, debate %>
<div class="truncate"></div>
</div>
<%= render "shared/tags", debate: debate %>
<%= render "shared/tags", debate: debate, limit: 5 %>
</div>
</div>

View File

@@ -1,3 +1,13 @@
<%- limit ||= nil %>
<% if debate.tags.any? %>
<span class='tags'><%= tags(debate) %></span>
<% end %>
<span class='tags'>
<% debate.tag_list_with_limit(limit).each do |tag| %>
<%= link_to sanitize(tag), debates_path(tag: tag) %>
<% end %>
<% if debate.tags_count_out_of_limit(limit) > 0 %>
<%= link_to "#{debate.tags_count_out_of_limit(limit)}+", debate_path(debate) %>
<% end %>
</span>
<% end %>

View File

@@ -14,7 +14,7 @@
<%= link_to featured_debate.description, featured_debate %>
<div class="truncate"></div>
</div>
<%= render "shared/tags", debate: featured_debate %>
<%= render "shared/tags", debate: featured_debate, limit: 5 %>
</div>
<div class="row">

View File

@@ -130,4 +130,50 @@ feature 'Debates' do
expect(page).to have_content "Let's..."
end
describe 'Limiting tags shown' do
let(:all_tags) {
["Hacienda", "Economía", "Medio Ambiente", "Corrupción", "Fiestas populares", "Prensa", "Huelgas"]
}
let(:debate) {
create :debate, tag_list: all_tags
}
scenario 'Index page shows up to 5 tags per debate' do
debate
visible_tags = ["Medio Ambiente", "Corrupción", "Fiestas populares", "Prensa", "Huelgas"]
visit debates_path
within('.debate .tags') do
visible_tags.each do |tag|
expect(page).to have_content tag
end
expect(page).to have_content '2+'
end
end
scenario 'Index page shows 3 tags with no plus link' do
tag_list = ["Medio Ambiente", "Corrupción", "Fiestas populares"]
debate = create :debate, tag_list: tag_list
visit debates_path
within('.debate .tags') do
tag_list.each do |tag|
expect(page).to have_content tag
end
expect(page).not_to have_content '+'
end
end
scenario 'Debate#show shows the full tag list' do
visit debate_path(debate)
within("#debate-#{debate.id}") do
all_tags.each do |tag|
expect(page).to have_content tag
end
end
end
end
end