Module: Fuzzily::Model::ClassMethods::Rails2

Included in:
Fuzzily::Model::ClassMethods
Defined in:
lib/fuzzily/model.rb

Instance Method Summary collapse

Instance Method Details

#_add_fuzzy_scopesObject



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/fuzzily/model.rb', line 41

def _add_fuzzy_scopes
  named_scope :for_model,  lambda { |model| { 
    :conditions => { :owner_type => model.kind_of?(Class) ? model.name : model  } 
  }}
  named_scope :for_field,  lambda { |field_name| {
    :conditions => { :fuzzy_field => field_name }
  }}
  named_scope :with_trigram, lambda { |trigrams| {
    :conditions => { :trigram => trigrams }
  }}
  named_scope :limit, lambda { |count| { :limit => count }}
end

#_matches_for_trigrams(trigrams) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/fuzzily/model.rb', line 33

def _matches_for_trigrams(trigrams)
  self.
    scoped(:select => 'owner_id, owner_type, count(*) AS matches, MAX(score) AS score').
    scoped(:group => 'owner_id, owner_type').
    scoped(:order => 'matches DESC, score ASC').
    with_trigram(trigrams)
end