Sha256: b25a5d8bb31f3ec29570fdf7bc6942b161eabe69c8e32cb31708c7a685ad4d9c
Contents?: true
Size: 1.68 KB
Versions: 2
Compression:
Stored size: 1.68 KB
Contents
module Fuzzily module Model # Needs fields: trigram, owner_type, owner_id, score # Needs index on [owner_type, trigram] and [owner_type, owner_id] def self.included(by) by.ancestors.include?(ActiveRecord::Base) or raise 'Not included in an ActiveRecord subclass' by.class_eval do return if class_variable_defined?(:@@fuzzily_trigram_model) belongs_to :owner, :polymorphic => true validates_presence_of :owner validates_uniqueness_of :trigram, :scope => [:owner_type, :owner_id] validates_length_of :trigram, :is => 3 validates_presence_of :score validates_presence_of :fuzzy_field 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 } }} class_variable_set(:@@fuzzily_trigram_model, true) end by.extend(ClassMethods) end module ClassMethods # options: # - model (mandatory) # - field (mandatory) # - limit (default 10) def matches_for(text, options = {}) options[:limit] ||= 10 self. scoped(:select => 'owner_id, owner_type, SUM(score) AS total_score'). scoped(:group => :owner_id). scoped(:order => 'total_score DESC'). scoped(:limit => options[:limit]). with_trigram(text.extend(String).trigrams). map(&:owner) end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
fuzzily-0.0.3 | lib/fuzzily/model.rb |
fuzzily-0.0.2 | lib/fuzzily/model.rb |