diff --git a/app/models/concerns/taggable.rb b/app/models/concerns/taggable.rb index 1cce0152d..52efc04a7 100644 --- a/app/models/concerns/taggable.rb +++ b/app/models/concerns/taggable.rb @@ -3,6 +3,7 @@ module Taggable included do acts_as_taggable + validate :max_number_of_tags end def tag_list_with_limit(limit = nil) @@ -17,4 +18,8 @@ module Taggable count = tags.size - limit count < 0 ? 0 : count end + + def max_number_of_tags + errors.add(:tag_list, :less_than_or_equal_to, count: 6) if tag_list.count > 6 + end end diff --git a/config/locales/activerecord.en.yml b/config/locales/activerecord.en.yml index 303bd5ded..d72967bfa 100644 --- a/config/locales/activerecord.en.yml +++ b/config/locales/activerecord.en.yml @@ -34,3 +34,13 @@ en: organization: name: Organization name responsible_name: Person in charge + errors: + models: + debate: + attributes: + tag_list: + less_than_or_equal_to: tags must be less than or equal to %{count} + proposal: + attributes: + tag_list: + less_than_or_equal_to: tags must be less than or equal to %{count} \ No newline at end of file diff --git a/config/locales/activerecord.es.yml b/config/locales/activerecord.es.yml index 3ba5e7cca..f4bf58b04 100644 --- a/config/locales/activerecord.es.yml +++ b/config/locales/activerecord.es.yml @@ -56,3 +56,13 @@ es: organization: name: Nombre de organización responsible_name: Persona responsable del colectivo + errors: + models: + debate: + attributes: + tag_list: + less_than_or_equal_to: los temas deben ser menor o igual que %{count} + proposal: + attributes: + tag_list: + less_than_or_equal_to: los temas deben ser menor o igual que %{count} diff --git a/spec/features/debates_spec.rb b/spec/features/debates_spec.rb index e542feb99..21b131c79 100644 --- a/spec/features/debates_spec.rb +++ b/spec/features/debates_spec.rb @@ -339,13 +339,13 @@ feature 'Debates' do describe 'Limiting tags shown' do scenario 'Index page shows up to 5 tags per debate' do - tag_list = ["Hacienda", "Economía", "Medio Ambiente", "Corrupción", "Fiestas populares", "Prensa", "Huelgas"] + tag_list = ["Hacienda", "Economía", "Medio Ambiente", "Corrupción", "Fiestas populares", "Prensa"] create :debate, tag_list: tag_list visit debates_path within('.debate .tags') do - expect(page).to have_content '2+' + expect(page).to have_content '1+' end end diff --git a/spec/features/proposals_spec.rb b/spec/features/proposals_spec.rb index 453f3053a..ad5644747 100644 --- a/spec/features/proposals_spec.rb +++ b/spec/features/proposals_spec.rb @@ -422,13 +422,13 @@ feature 'Proposals' do describe 'Limiting tags shown' do scenario 'Index page shows up to 5 tags per proposal' do - tag_list = ["Hacienda", "Economía", "Medio Ambiente", "Corrupción", "Fiestas populares", "Prensa", "Huelgas"] + tag_list = ["Hacienda", "Economía", "Medio Ambiente", "Corrupción", "Fiestas populares", "Prensa"] create :proposal, tag_list: tag_list visit proposals_path within('.proposal .tags') do - expect(page).to have_content '2+' + expect(page).to have_content '1+' end end diff --git a/spec/features/tags_spec.rb b/spec/features/tags_spec.rb index 232c220c0..3f310071e 100644 --- a/spec/features/tags_spec.rb +++ b/spec/features/tags_spec.rb @@ -90,6 +90,24 @@ feature 'Tags' do expect(page).to have_content 'Impuestos' end + scenario 'Create with too many tags' do + user = create(:user) + login_as(user) + + visit new_debate_path + fill_in 'debate_title', with: 'Title' + fill_in 'debate_description', with: 'Description' + fill_in 'debate_captcha', with: correct_captcha_text + check 'debate_terms_of_service' + + fill_in 'debate_tag_list', with: "Impuestos, Economía, Hacienda, Sanidad, Educación, Política, Igualdad" + + click_button 'Start a debate' + + expect(page).to have_content '1 error prohibited this debate from being saved:' + expect(page).to have_content 'tags must be less than or equal to 6' + end + scenario 'Update' do debate = create(:debate, tag_list: 'Economía') diff --git a/spec/models/debate_spec.rb b/spec/models/debate_spec.rb index c4ac619b0..a9ef3892a 100644 --- a/spec/models/debate_spec.rb +++ b/spec/models/debate_spec.rb @@ -57,10 +57,22 @@ describe Debate do end end - it "should sanitize the tag list" do - debate.tag_list = "user_id=1" - debate.valid? - expect(debate.tag_list).to eq(['user_id1']) + describe "#tag_list" do + it "should sanitize the tag list" do + debate.tag_list = "user_id=1" + debate.valid? + expect(debate.tag_list).to eq(['user_id1']) + end + + it "should not be valid with a tag list of more than 6 elements" do + debate.tag_list = ["Hacienda", "Economía", "Medio Ambiente", "Corrupción", "Fiestas populares", "Prensa", "Huelgas"] + expect(debate).to_not be_valid + end + + it "should be valid with a tag list of 6 elements" do + debate.tag_list = ["Hacienda", "Economía", "Medio Ambiente", "Corrupción", "Fiestas populares", "Prensa"] + expect(debate).to be_valid + end end it "should not be valid without accepting terms of service" do diff --git a/spec/models/proposal_spec.rb b/spec/models/proposal_spec.rb index 8b58d0eec..ac7c1edfb 100644 --- a/spec/models/proposal_spec.rb +++ b/spec/models/proposal_spec.rb @@ -99,10 +99,22 @@ describe Proposal do end end - it "should sanitize the tag list" do - proposal.tag_list = "user_id=1" - proposal.valid? - expect(proposal.tag_list).to eq(['user_id1']) + describe "tag_list" do + it "should sanitize the tag list" do + proposal.tag_list = "user_id=1" + proposal.valid? + expect(proposal.tag_list).to eq(['user_id1']) + end + + it "should not be valid with a tag list of more than 6 elements" do + proposal.tag_list = ["Hacienda", "Economía", "Medio Ambiente", "Corrupción", "Fiestas populares", "Prensa", "Huelgas"] + expect(proposal).to_not be_valid + end + + it "should be valid with a tag list of more than 6 elements" do + proposal.tag_list = ["Hacienda", "Economía", "Medio Ambiente", "Corrupción", "Fiestas populares", "Prensa"] + expect(proposal).to be_valid + end end it "should not be valid without accepting terms of service" do