From c2753fdfb471209fe7f2cdb8844e049207af8ba3 Mon Sep 17 00:00:00 2001 From: unarist Date: Fri, 14 Jul 2017 02:31:33 +0900 Subject: Make tag search case insensitive again (#4184) --- app/models/tag.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'app/models/tag.rb') diff --git a/app/models/tag.rb b/app/models/tag.rb index 08e3c1b03..a14e2cc98 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -23,7 +23,7 @@ class Tag < ApplicationRecord class << self def search_for(term, limit = 5) pattern = sanitize_sql_like(term) + '%' - Tag.where('name like ?', pattern).order(:name).limit(limit) + Tag.where('lower(name) like lower(?)', pattern).order(:name).limit(limit) end end end -- cgit From a49be27145a858a51e1b9104b0ebc5f63a03b81b Mon Sep 17 00:00:00 2001 From: masarakki Date: Fri, 14 Jul 2017 18:02:49 +0900 Subject: add validation to tag name (#4194) --- app/models/tag.rb | 5 +++-- spec/models/tag_spec.rb | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) (limited to 'app/models/tag.rb') diff --git a/app/models/tag.rb b/app/models/tag.rb index a14e2cc98..0fa08e157 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -12,9 +12,10 @@ class Tag < ApplicationRecord has_and_belongs_to_many :statuses - HASHTAG_RE = /(?:^|[^\/\)\w])#([[:word:]_]*[[:alpha:]_][[:word:]_]*)/i + HASHTAG_NAME_RE = '[[:word:]_]*[[:alpha:]_][[:word:]_]*' + HASHTAG_RE = /(?:^|[^\/\)\w])#(#{HASHTAG_NAME_RE})/i - validates :name, presence: true, uniqueness: true + validates :name, presence: true, uniqueness: true, format: { with: /\A#{HASHTAG_NAME_RE}\z/i } def to_param name diff --git a/spec/models/tag_spec.rb b/spec/models/tag_spec.rb index 555474c44..f727fa1dd 100644 --- a/spec/models/tag_spec.rb +++ b/spec/models/tag_spec.rb @@ -1,6 +1,24 @@ require 'rails_helper' RSpec.describe Tag, type: :model do + describe 'validations' do + it 'invalid with #' do + expect(Tag.new(name: '#hello_world')).to_not be_valid + end + + it 'invalid with .' do + expect(Tag.new(name: '.abcdef123')).to_not be_valid + end + + it 'invalid with spaces' do + expect(Tag.new(name: 'hello world')).to_not be_valid + end + + it 'valid with aesthetic' do + expect(Tag.new(name: 'aesthetic')).to be_valid + end + end + describe 'HASHTAG_RE' do subject { Tag::HASHTAG_RE } -- cgit