diff options
author | Hiroyuki Sato <sathiroyuki@gmail.com> | 2017-08-29 01:14:41 +0300 |
---|---|---|
committer | Hiroyuki Sato <sathiroyuki@gmail.com> | 2017-08-29 01:14:41 +0300 |
commit | 87b51c5981db3b1b9831b01ca6e74127d57dc2d9 (patch) | |
tree | 711dcce0d9a49385cae82854ecbbbc13b09b9806 | |
parent | 866aab7f2a92f9929a5c5811d3d3c23c11184b26 (diff) |
Move the logic to a concern
-rw-r--r-- | app/models/user.rb | 3 | ||||
-rw-r--r-- | lib/gitlab/sql/pattern.rb | 39 | ||||
-rw-r--r-- | spec/lib/gitlab/sql/pattern_spec.rb | 16 |
3 files changed, 26 insertions, 32 deletions
diff --git a/app/models/user.rb b/app/models/user.rb index e5a84ce4080..70787de4b40 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -5,6 +5,7 @@ class User < ActiveRecord::Base include Gitlab::ConfigHelper include Gitlab::CurrentSettings + include Gitlab::SQL::Pattern include Avatarable include Referable include Sortable @@ -303,7 +304,7 @@ class User < ActiveRecord::Base # Returns an ActiveRecord::Relation. def search(query) table = arel_table - pattern = Gitlab::SQL::Pattern.new(query).to_sql + pattern = User.to_pattern(query) order = <<~SQL CASE diff --git a/lib/gitlab/sql/pattern.rb b/lib/gitlab/sql/pattern.rb index 46c973d8a11..26bfeeeee67 100644 --- a/lib/gitlab/sql/pattern.rb +++ b/lib/gitlab/sql/pattern.rb @@ -1,33 +1,26 @@ module Gitlab module SQL - class Pattern - MIN_CHARS_FOR_PARTIAL_MATCHING = 3 - - attr_reader :query + module Pattern + extend ActiveSupport::Concern - def initialize(query) - @query = query - end + MIN_CHARS_FOR_PARTIAL_MATCHING = 3 - def to_sql - if exact_matching? - sanitized_query - else - "%#{sanitized_query}%" + class_methods do + def to_pattern(query) + if exact_matching?(query) + sanitize_sql_like(query) + else + "%#{sanitize_sql_like(query)}%" + end end - end - def exact_matching? - !partial_matching? - end - - def partial_matching? - @query.length >= MIN_CHARS_FOR_PARTIAL_MATCHING - end + def exact_matching?(query) + query.length < MIN_CHARS_FOR_PARTIAL_MATCHING + end - def sanitized_query - # Note: ActiveRecord::Base.sanitize_sql_like is a protected method - ActiveRecord::Base.__send__(:sanitize_sql_like, query) + def partial_matching?(query) + query.length >= MIN_CHARS_FOR_PARTIAL_MATCHING + end end end end diff --git a/spec/lib/gitlab/sql/pattern_spec.rb b/spec/lib/gitlab/sql/pattern_spec.rb index d0412f37098..224336421be 100644 --- a/spec/lib/gitlab/sql/pattern_spec.rb +++ b/spec/lib/gitlab/sql/pattern_spec.rb @@ -1,14 +1,14 @@ require 'spec_helper' describe Gitlab::SQL::Pattern do - describe '#to_sql' do - subject(:to_sql) { described_class.new(query).to_sql } + describe '#to_pattern' do + subject(:to_pattern) { User.to_pattern(query) } context 'when a query is shorter than 3 chars' do let(:query) { '12' } it 'returns exact matching pattern' do - expect(to_sql).to eq('12') + expect(to_pattern).to eq('12') end end @@ -16,7 +16,7 @@ describe Gitlab::SQL::Pattern do let(:query) { '_2' } it 'returns sanitized exact matching pattern' do - expect(to_sql).to eq('\_2') + expect(to_pattern).to eq('\_2') end end @@ -24,7 +24,7 @@ describe Gitlab::SQL::Pattern do let(:query) { '123' } it 'returns partial matching pattern' do - expect(to_sql).to eq('%123%') + expect(to_pattern).to eq('%123%') end end @@ -32,7 +32,7 @@ describe Gitlab::SQL::Pattern do let(:query) { '_23' } it 'returns partial matching pattern' do - expect(to_sql).to eq('%\_23%') + expect(to_pattern).to eq('%\_23%') end end @@ -40,7 +40,7 @@ describe Gitlab::SQL::Pattern do let(:query) { '1234' } it 'returns partial matching pattern' do - expect(to_sql).to eq('%1234%') + expect(to_pattern).to eq('%1234%') end end @@ -48,7 +48,7 @@ describe Gitlab::SQL::Pattern do let(:query) { '_234' } it 'returns sanitized partial matching pattern' do - expect(to_sql).to eq('%\_234%') + expect(to_pattern).to eq('%\_234%') end end end |