lib/fuzzily/searchable.rb in fuzzily-0.3.2 vs lib/fuzzily/searchable.rb in fuzzily-0.3.3

- old
+ new

@@ -38,11 +38,11 @@ end private def _find_by_fuzzy(_o, pattern, options={}) - options[:limit] ||= 10 + options[:limit] ||= 10 unless options.has_key? :limit options[:offset] ||= 0 trigrams = _o.trigram_class_name.constantize. limit(options[:limit]). offset(options[:offset]). @@ -110,11 +110,12 @@ _o = OpenStruct.new( :field => field, :trigram_class_name => options.fetch(:class_name, 'Trigram'), :trigram_association => "trigrams_for_#{field}".to_sym, - :update_trigrams_method => "update_fuzzy_#{field}!".to_sym + :update_trigrams_method => "update_fuzzy_#{field}!".to_sym, + :async => options.fetch(:async, false) ) _add_trigram_association(_o) singleton_class.send(:define_method,"find_by_fuzzy_#{field}".to_sym) do |*args| @@ -124,14 +125,19 @@ singleton_class.send(:define_method,"bulk_update_fuzzy_#{field}".to_sym) do _bulk_update_fuzzy(_o) end define_method _o.update_trigrams_method do - _update_fuzzy!(_o) + if _o.async && self.respond_to?(:delay) + self.delay._update_fuzzy!(_o) + else + _update_fuzzy!(_o) + end end after_save do |record| next unless record.send("#{field}_changed?".to_sym) + record.send(_o.update_trigrams_method) end class_variable_set(:"@@fuzzily_searchable_#{field}", true) self