From 7db2fecbb1f05c478a05fb373286521d4d6978de Mon Sep 17 00:00:00 2001 From: juandefelix Date: Sat, 3 Oct 2015 11:54:48 -0500 Subject: [PATCH 1/5] added validation for tag count --- app/models/concerns/taggable.rb | 5 +++++ 1 file changed, 5 insertions(+) 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 From 45a29f2f4d20990fd38780c0b2c34c9fbe18a900 Mon Sep 17 00:00:00 2001 From: juandefelix Date: Sat, 3 Oct 2015 11:58:09 -0500 Subject: [PATCH 2/5] added tag_list errors translations --- config/locales/activerecord.en.yml | 10 ++++++++++ config/locales/activerecord.es.yml | 10 ++++++++++ 2 files changed, 20 insertions(+) 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} From 03b0fceecc8dfa296a264bc8b18770267fdc4742 Mon Sep 17 00:00:00 2001 From: juandefelix Date: Sat, 3 Oct 2015 11:59:14 -0500 Subject: [PATCH 3/5] updated tag visibility tests for the new valid tag count --- spec/features/debates_spec.rb | 4 ++-- spec/features/proposals_spec.rb | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) 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 From 2503350526626c349bbb71bef81f07a995f6df26 Mon Sep 17 00:00:00 2001 From: juandefelix Date: Sat, 3 Oct 2015 11:59:54 -0500 Subject: [PATCH 4/5] added models tests for tag_list count validation --- spec/models/debate_spec.rb | 20 ++++++++++++++++---- spec/models/proposal_spec.rb | 20 ++++++++++++++++---- 2 files changed, 32 insertions(+), 8 deletions(-) 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 From 77a81578b7bac968ef87e76d026e0d83ccfccbfa Mon Sep 17 00:00:00 2001 From: juandefelix Date: Sat, 3 Oct 2015 13:39:34 -0500 Subject: [PATCH 5/5] added features test for tag limit --- spec/features/tags_spec.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) 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')