Module: FuzzySearch::FuzzyModelExtensions::ClassMethods

Defined in:
lib/fuzzy_model_extensions.rb

Instance Method Summary collapse

Instance Method Details

#fuzzy_searchable_on(*properties) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/fuzzy_model_extensions.rb', line 19

def fuzzy_searchable_on(*properties)
  # TODO: Complain if fuzzy_searchable_on is called more than once
  # TODO: Complain if no properties were given
  options = properties.last.is_a?(Hash) ? properties.pop : {}
  write_inheritable_attribute :fuzzy_search_properties, properties
  if options[:subset_on]
    write_inheritable_attribute :fuzzy_search_subset_property, options[:subset_on]
    options.delete(:subset_on)
  end

  unless options.empty?
    # TODO Test me
    raise "Invalid options: #{options.keys.join(",")}"
  end

  named_scope :fuzzy_search_scope, lambda { |words|
    fuzzy_search_scope_with_opts(words, {})
  }
  named_scope :fuzzy_search_scope_with_opts, lambda { |words, opts|
    self::FuzzySearchTrigram.params_for_search(words, opts)
  }
  extend FuzzySearchClassMethods
  include InstanceMethods
  after_save :update_fuzzy_search_trigrams!
  after_destroy :delete_fuzzy_search_trigrams!

  const_set(:FuzzySearchTrigram, Class.new(ActiveRecord::Base))
  self::FuzzySearchTrigram.extend TrigramModelExtensions
  self::FuzzySearchTrigram.set_target_class self
  self::FuzzySearchTrigram.table_name = "#{name.underscore}_fuzzy_search_trigrams"
end