mirror of
https://github.com/mastodon/mastodon.git
synced 2025-09-06 09:51:24 +00:00
Simplify tag search_for method
This commit is contained in:
parent
25f1a515f8
commit
e74e3af7e3
|
@ -32,7 +32,7 @@ class Admin::TagFilter
|
||||||
when :status
|
when :status
|
||||||
status_scope(value)
|
status_scope(value)
|
||||||
when :name
|
when :name
|
||||||
Tag.search_for(value.to_s.strip, params[:limit], params[:offset], exclude_unlistable: false)
|
Tag.search_for(value, params[:limit], params[:offset], exclude_unlistable: false)
|
||||||
when :order
|
when :order
|
||||||
order_scope(value)
|
order_scope(value)
|
||||||
else
|
else
|
||||||
|
|
|
@ -67,6 +67,8 @@ class Tag < ApplicationRecord
|
||||||
}
|
}
|
||||||
scope :matches_name, ->(term) { where(arel_table[:name].lower.matches(arel_table.lower("#{sanitize_sql_like(Tag.normalize(term))}%"), nil, true)) } # Search with case-sensitive to use B-tree index
|
scope :matches_name, ->(term) { where(arel_table[:name].lower.matches(arel_table.lower("#{sanitize_sql_like(Tag.normalize(term))}%"), nil, true)) } # Search with case-sensitive to use B-tree index
|
||||||
|
|
||||||
|
scope :by_name_length, -> { order(Arel.sql('LENGTH(name)').asc, name: :asc) }
|
||||||
|
|
||||||
update_index('tags', :self)
|
update_index('tags', :self)
|
||||||
|
|
||||||
def to_param
|
def to_param
|
||||||
|
@ -122,16 +124,19 @@ class Tag < ApplicationRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
def search_for(term, limit = 5, offset = 0, options = {})
|
def search_for(term, limit = 5, offset = 0, options = {})
|
||||||
stripped_term = term.strip
|
|
||||||
options.reverse_merge!({ exclude_unlistable: true, exclude_unreviewed: false })
|
options.reverse_merge!({ exclude_unlistable: true, exclude_unreviewed: false })
|
||||||
|
|
||||||
query = Tag.matches_name(stripped_term)
|
search_query(term.to_s.strip, options)
|
||||||
query = query.merge(Tag.listable) if options[:exclude_unlistable]
|
|
||||||
query = query.merge(matching_name(stripped_term).or(reviewed)) if options[:exclude_unreviewed]
|
|
||||||
|
|
||||||
query.order(Arel.sql('LENGTH(name)').asc, name: :asc)
|
|
||||||
.limit(limit)
|
.limit(limit)
|
||||||
.offset(offset)
|
.offset(offset)
|
||||||
|
.by_name_length
|
||||||
|
end
|
||||||
|
|
||||||
|
def search_query(term, options)
|
||||||
|
matches_name(term).tap do |query|
|
||||||
|
query.merge!(listable) if options[:exclude_unlistable]
|
||||||
|
query.merge!(reviewed) if options[:exclude_unreviewed]
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def find_normalized(name)
|
def find_normalized(name)
|
||||||
|
|
|
@ -280,6 +280,23 @@ RSpec.describe Tag do
|
||||||
expect(results).to eq [tag]
|
expect(results).to eq [tag]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'finds tag records from padded term queries' do
|
||||||
|
tag = Fabricate(:tag, name: 'MATCH')
|
||||||
|
_miss_tag = Fabricate(:tag, name: 'miss')
|
||||||
|
|
||||||
|
results = described_class.search_for(' match ')
|
||||||
|
|
||||||
|
expect(results)
|
||||||
|
.to contain_exactly(tag)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'handles nil query' do
|
||||||
|
results = described_class.search_for(nil)
|
||||||
|
|
||||||
|
expect(results)
|
||||||
|
.to be_empty
|
||||||
|
end
|
||||||
|
|
||||||
it 'finds the exact matching tag as the first item' do
|
it 'finds the exact matching tag as the first item' do
|
||||||
similar_tag = Fabricate(:tag, name: 'matchlater', reviewed_at: Time.now.utc)
|
similar_tag = Fabricate(:tag, name: 'matchlater', reviewed_at: Time.now.utc)
|
||||||
tag = Fabricate(:tag, name: 'match', reviewed_at: Time.now.utc)
|
tag = Fabricate(:tag, name: 'match', reviewed_at: Time.now.utc)
|
||||||
|
@ -306,5 +323,16 @@ RSpec.describe Tag do
|
||||||
|
|
||||||
expect(results).to eq [tag, unlisted_tag]
|
expect(results).to eq [tag, unlisted_tag]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'excludes non reviewed tags via option' do
|
||||||
|
tag = Fabricate(:tag, name: 'match', reviewed_at: 5.days.ago)
|
||||||
|
unreviewed_tag = Fabricate(:tag, name: 'matchreviewed', reviewed_at: nil)
|
||||||
|
|
||||||
|
results = described_class.search_for('match', 5, 0, exclude_unreviewed: true)
|
||||||
|
|
||||||
|
expect(results)
|
||||||
|
.to include(tag)
|
||||||
|
.and not_include(unreviewed_tag)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue
Block a user