module Fuzzily::Model::ClassMethods

Public Instance Methods

matches_for(text) click to toggle source
# File lib/fuzzily/model.rb, line 26
def matches_for(text)
  _matches_for_trigrams Fuzzily::String.new(text).trigrams
end

Private Instance Methods

_add_fuzzy_scopes() click to toggle source
# File lib/fuzzily/model.rb, line 40
def _add_fuzzy_scopes
  scope :for_model, lambda { |model|
    where(owner_type: model.kind_of?(Class) ? model.name : model)
  }
  scope :for_field,  lambda { |field_name| where(fuzzy_field: field_name) }
  scope :with_trigram, lambda { |trigrams| where(trigram: trigrams) }
end
_matches_for_trigrams(trigrams) click to toggle source
# File lib/fuzzily/model.rb, line 32
def _matches_for_trigrams(trigrams)
  self.
    select("owner_id, owner_type, count(*) AS matches, MAX(score) AS score").
    group("owner_id, owner_type").
    order("matches DESC, score ASC").
    with_trigram(trigrams)
end