Merge pull request #582 from juandefelix/limit-tags
Limit number of tags in debates and proposals
This commit is contained in:
@@ -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
|
||||||
|
|||||||
@@ -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}
|
||||||
@@ -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}
|
||||||
|
|||||||
@@ -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
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
||||||
|
|
||||||
|
|||||||
@@ -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')
|
||||||
|
|
||||||
|
|||||||
@@ -57,12 +57,24 @@ describe Debate do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "#tag_list" do
|
||||||
it "should sanitize the tag list" do
|
it "should sanitize the tag list" do
|
||||||
debate.tag_list = "user_id=1"
|
debate.tag_list = "user_id=1"
|
||||||
debate.valid?
|
debate.valid?
|
||||||
expect(debate.tag_list).to eq(['user_id1'])
|
expect(debate.tag_list).to eq(['user_id1'])
|
||||||
end
|
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
|
it "should not be valid without accepting terms of service" do
|
||||||
debate.terms_of_service = nil
|
debate.terms_of_service = nil
|
||||||
expect(debate).to_not be_valid
|
expect(debate).to_not be_valid
|
||||||
|
|||||||
@@ -99,12 +99,24 @@ describe Proposal do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "tag_list" do
|
||||||
it "should sanitize the tag list" do
|
it "should sanitize the tag list" do
|
||||||
proposal.tag_list = "user_id=1"
|
proposal.tag_list = "user_id=1"
|
||||||
proposal.valid?
|
proposal.valid?
|
||||||
expect(proposal.tag_list).to eq(['user_id1'])
|
expect(proposal.tag_list).to eq(['user_id1'])
|
||||||
end
|
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
|
it "should not be valid without accepting terms of service" do
|
||||||
proposal.terms_of_service = nil
|
proposal.terms_of_service = nil
|
||||||
expect(proposal).to_not be_valid
|
expect(proposal).to_not be_valid
|
||||||
|
|||||||
Reference in New Issue
Block a user