module Fuzzily::Model

Public Class Methods

included(by) click to toggle source

Needs fields: trigram, owner_type, owner_id, score Needs index on [owner_type, trigram] and [owner_type, owner_id]

# File lib/fuzzily/model.rb, line 6
def self.included(by)
  by.ancestors.include?(ActiveRecord::Base) or raise 'Not included in an ActiveRecord subclass'
  by.extend(ClassMethods)

  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, :fuzzy_field]
    validates_length_of       :trigram, :is => 3
    validates_presence_of     :score
    validates_presence_of     :fuzzy_field

    _add_fuzzy_scopes
    class_variable_set(:@@fuzzily_trigram_model, true)
  end
end