diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 70828c01a..cee6db199 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -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 + "%" diff --git a/app/models/debate.rb b/app/models/debate.rb index 26fd2dbf0..fe75811fa 100644 --- a/app/models/debate.rb +++ b/app/models/debate.rb @@ -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 diff --git a/app/views/debates/_debate.html.erb b/app/views/debates/_debate.html.erb index f8343e8be..56a5127c5 100644 --- a/app/views/debates/_debate.html.erb +++ b/app/views/debates/_debate.html.erb @@ -15,7 +15,7 @@ <%= link_to debate.description, debate %>
- <%= render "shared/tags", debate: debate %> + <%= render "shared/tags", debate: debate, limit: 5 %> diff --git a/app/views/shared/_tags.html.erb b/app/views/shared/_tags.html.erb index 8be6da579..6a61bd4b4 100644 --- a/app/views/shared/_tags.html.erb +++ b/app/views/shared/_tags.html.erb @@ -1,3 +1,13 @@ +<%- limit ||= nil %> + <% if debate.tags.any? %> - <%= tags(debate) %> -<% end %> \ No newline at end of file + + <% 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 %> + +<% end %> diff --git a/app/views/welcome/_featured_debate.html.erb b/app/views/welcome/_featured_debate.html.erb index 93626b4c7..a1992e337 100644 --- a/app/views/welcome/_featured_debate.html.erb +++ b/app/views/welcome/_featured_debate.html.erb @@ -14,7 +14,7 @@ <%= link_to featured_debate.description, featured_debate %>
- <%= render "shared/tags", debate: featured_debate %> + <%= render "shared/tags", debate: featured_debate, limit: 5 %>
diff --git a/spec/features/debates_spec.rb b/spec/features/debates_spec.rb index b0c7a3a60..de3e05943 100644 --- a/spec/features/debates_spec.rb +++ b/spec/features/debates_spec.rb @@ -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