Merge pull request #3264 from consul/tags-length
[Backport] Set tags max length to 160
This commit is contained in:
@@ -8,7 +8,7 @@ App.Tags =
|
|||||||
|
|
||||||
unless $this.data('initialized') is 'yes'
|
unless $this.data('initialized') is 'yes'
|
||||||
$this.on('click', ->
|
$this.on('click', ->
|
||||||
name = $(this).text()
|
name = '"' + $(this).text() + '"'
|
||||||
current_tags = $tag_input.val().split(',').filter(Boolean)
|
current_tags = $tag_input.val().split(',').filter(Boolean)
|
||||||
|
|
||||||
if $.inArray(name, current_tags) >= 0
|
if $.inArray(name, current_tags) >= 0
|
||||||
|
|||||||
2
app/models/tag.rb
Normal file
2
app/models/tag.rb
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
class Tag < ActsAsTaggableOn::Tag
|
||||||
|
end
|
||||||
9
db/migrate/20190205131722_increase_tag_name_limit.rb
Normal file
9
db/migrate/20190205131722_increase_tag_name_limit.rb
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
class IncreaseTagNameLimit < ActiveRecord::Migration
|
||||||
|
def up
|
||||||
|
change_column :tags, :name, :string, limit: 160
|
||||||
|
end
|
||||||
|
|
||||||
|
def down
|
||||||
|
change_column :tags, :name, :string, limit: 40
|
||||||
|
end
|
||||||
|
end
|
||||||
18
db/schema.rb
18
db/schema.rb
@@ -11,7 +11,7 @@
|
|||||||
#
|
#
|
||||||
# It's strongly recommended that you check this file into your version control system.
|
# It's strongly recommended that you check this file into your version control system.
|
||||||
|
|
||||||
ActiveRecord::Schema.define(version: 20190131122858) do
|
ActiveRecord::Schema.define(version: 20190205131722) do
|
||||||
|
|
||||||
# These are extensions that must be enabled in order to support this database
|
# These are extensions that must be enabled in order to support this database
|
||||||
enable_extension "plpgsql"
|
enable_extension "plpgsql"
|
||||||
@@ -1311,15 +1311,15 @@ ActiveRecord::Schema.define(version: 20190131122858) do
|
|||||||
add_index "taggings", ["taggable_id", "taggable_type", "context"], name: "index_taggings_on_taggable_id_and_taggable_type_and_context", using: :btree
|
add_index "taggings", ["taggable_id", "taggable_type", "context"], name: "index_taggings_on_taggable_id_and_taggable_type_and_context", using: :btree
|
||||||
|
|
||||||
create_table "tags", force: :cascade do |t|
|
create_table "tags", force: :cascade do |t|
|
||||||
t.string "name", limit: 40
|
t.string "name", limit: 160
|
||||||
t.integer "taggings_count", default: 0
|
t.integer "taggings_count", default: 0
|
||||||
t.integer "debates_count", default: 0
|
t.integer "debates_count", default: 0
|
||||||
t.integer "proposals_count", default: 0
|
t.integer "proposals_count", default: 0
|
||||||
t.integer "spending_proposals_count", default: 0
|
t.integer "spending_proposals_count", default: 0
|
||||||
t.string "kind"
|
t.string "kind"
|
||||||
t.integer "budget/investments_count", default: 0
|
t.integer "budget/investments_count", default: 0
|
||||||
t.integer "legislation/proposals_count", default: 0
|
t.integer "legislation/proposals_count", default: 0
|
||||||
t.integer "legislation/processes_count", default: 0
|
t.integer "legislation/processes_count", default: 0
|
||||||
end
|
end
|
||||||
|
|
||||||
add_index "tags", ["debates_count"], name: "index_tags_on_debates_count", using: :btree
|
add_index "tags", ["debates_count"], name: "index_tags_on_debates_count", using: :btree
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
class TagSanitizer
|
class TagSanitizer
|
||||||
DISALLOWED_STRINGS = %w(? < > = /)
|
DISALLOWED_STRINGS = %w[? < > = /]
|
||||||
|
|
||||||
def sanitize_tag(tag)
|
def sanitize_tag(tag)
|
||||||
tag = tag.dup
|
tag = tag.dup
|
||||||
DISALLOWED_STRINGS.each do |s|
|
DISALLOWED_STRINGS.each do |s|
|
||||||
tag.gsub!(s, '')
|
tag.gsub!(s, "")
|
||||||
end
|
end
|
||||||
tag.truncate(TagSanitizer.tag_max_length)
|
tag.truncate(TagSanitizer.tag_max_length)
|
||||||
end
|
end
|
||||||
@@ -14,7 +14,7 @@ class TagSanitizer
|
|||||||
end
|
end
|
||||||
|
|
||||||
def self.tag_max_length
|
def self.tag_max_length
|
||||||
40
|
160
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
FactoryBot.define do
|
FactoryBot.define do
|
||||||
factory :tag, class: 'ActsAsTaggableOn::Tag' do
|
factory :tag, class: "ActsAsTaggableOn::Tag" do
|
||||||
sequence(:name) { |n| "Tag #{n} name" }
|
sequence(:name) { |n| "Tag #{n} name" }
|
||||||
|
|
||||||
trait :category do
|
trait :category do
|
||||||
@@ -7,6 +7,12 @@ FactoryBot.define do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
factory :tagging, class: "ActsAsTaggableOn::Tagging" do
|
||||||
|
context "tags"
|
||||||
|
association :taggable, factory: :proposal
|
||||||
|
tag
|
||||||
|
end
|
||||||
|
|
||||||
factory :topic do
|
factory :topic do
|
||||||
sequence(:title) { |n| "Topic title #{n}" }
|
sequence(:title) { |n| "Topic title #{n}" }
|
||||||
sequence(:description) { |n| "Description as comment #{n}" }
|
sequence(:description) { |n| "Description as comment #{n}" }
|
||||||
|
|||||||
37
spec/models/tag_spec.rb
Normal file
37
spec/models/tag_spec.rb
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
require "rails_helper"
|
||||||
|
|
||||||
|
describe Tag do
|
||||||
|
|
||||||
|
it "decreases tag_count when a debate is hidden" do
|
||||||
|
debate = create(:debate)
|
||||||
|
tag = create(:tag)
|
||||||
|
tagging = create(:tagging, tag: tag, taggable: debate)
|
||||||
|
|
||||||
|
expect(tag.taggings_count).to eq(1)
|
||||||
|
|
||||||
|
debate.update(hidden_at: Time.now)
|
||||||
|
|
||||||
|
tag.reload
|
||||||
|
expect(tag.taggings_count).to eq(0)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "decreases tag_count when a proposal is hidden" do
|
||||||
|
proposal = create(:proposal)
|
||||||
|
tag = create(:tag)
|
||||||
|
tagging = create(:tagging, tag: tag, taggable: proposal)
|
||||||
|
|
||||||
|
expect(tag.taggings_count).to eq(1)
|
||||||
|
|
||||||
|
proposal.update(hidden_at: Time.now)
|
||||||
|
|
||||||
|
tag.reload
|
||||||
|
expect(tag.taggings_count).to eq(0)
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "name validation" do
|
||||||
|
it "160 char name should be valid" do
|
||||||
|
tag = build(:tag, name: Faker::Lorem.characters(160))
|
||||||
|
expect(tag).to be_valid
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
Reference in New Issue
Block a user