Merge pull request #582 from juandefelix/limit-tags

Limit number of tags in debates and proposals
This commit is contained in:
Raimond Garcia
2015-10-05 10:14:06 +02:00
8 changed files with 79 additions and 12 deletions

View File

@@ -3,6 +3,7 @@ module Taggable
included do included do
acts_as_taggable acts_as_taggable
validate :max_number_of_tags
end end
def tag_list_with_limit(limit = nil) def tag_list_with_limit(limit = nil)
@@ -17,4 +18,8 @@ module Taggable
count = tags.size - limit count = tags.size - limit
count < 0 ? 0 : count count < 0 ? 0 : count
end end
def max_number_of_tags
errors.add(:tag_list, :less_than_or_equal_to, count: 6) if tag_list.count > 6
end
end end

View File

@@ -34,3 +34,13 @@ en:
organization: organization:
name: Organization name name: Organization name
responsible_name: Person in charge 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}

View File

@@ -56,3 +56,13 @@ es:
organization: organization:
name: Nombre de organización name: Nombre de organización
responsible_name: Persona responsable del colectivo 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}

View File

@@ -339,13 +339,13 @@ feature 'Debates' do
describe 'Limiting tags shown' do describe 'Limiting tags shown' do
scenario 'Index page shows up to 5 tags per debate' 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 create :debate, tag_list: tag_list
visit debates_path visit debates_path
within('.debate .tags') do within('.debate .tags') do
expect(page).to have_content '2+' expect(page).to have_content '1+'
end end
end end

View File

@@ -422,13 +422,13 @@ feature 'Proposals' do
describe 'Limiting tags shown' do describe 'Limiting tags shown' do
scenario 'Index page shows up to 5 tags per proposal' 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 create :proposal, tag_list: tag_list
visit proposals_path visit proposals_path
within('.proposal .tags') do within('.proposal .tags') do
expect(page).to have_content '2+' expect(page).to have_content '1+'
end end
end end

View File

@@ -90,6 +90,24 @@ feature 'Tags' do
expect(page).to have_content 'Impuestos' expect(page).to have_content 'Impuestos'
end 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 scenario 'Update' do
debate = create(:debate, tag_list: 'Economía') debate = create(:debate, tag_list: 'Economía')

View File

@@ -57,10 +57,22 @@ describe Debate do
end end
end end
it "should sanitize the tag list" do describe "#tag_list" do
debate.tag_list = "user_id=1" it "should sanitize the tag list" do
debate.valid? debate.tag_list = "user_id=1"
expect(debate.tag_list).to eq(['user_id1']) 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 end
it "should not be valid without accepting terms of service" do it "should not be valid without accepting terms of service" do

View File

@@ -99,10 +99,22 @@ describe Proposal do
end end
end end
it "should sanitize the tag list" do describe "tag_list" do
proposal.tag_list = "user_id=1" it "should sanitize the tag list" do
proposal.valid? proposal.tag_list = "user_id=1"
expect(proposal.tag_list).to eq(['user_id1']) 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 end
it "should not be valid without accepting terms of service" do it "should not be valid without accepting terms of service" do