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
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

View File

@@ -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}

View File

@@ -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}

View File

@@ -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

View File

@@ -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

View File

@@ -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')

View File

@@ -57,12 +57,24 @@ describe Debate do
end
end
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
debate.terms_of_service = nil
expect(debate).to_not be_valid

View File

@@ -99,12 +99,24 @@ describe Proposal do
end
end
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
proposal.terms_of_service = nil
expect(proposal).to_not be_valid