Module: Shared::IsData::Levenshtein

Extended by:
ActiveSupport::Concern
Defined in:
app/models/concerns/shared/is_data/levenshtein.rb

Overview

Levenshtein function wrapper.

Instance Method Summary collapse

Instance Method Details

#nearest_by_levenshtein(compared_string = nil, column = nil, limit = 10) ⇒ Scope

Parameters:

  • (String, String, Integer)

Returns:

  • (Scope)


10
11
12
13
14
15
16
17
# File 'app/models/concerns/shared/is_data/levenshtein.rb', line 10

def nearest_by_levenshtein(compared_string = nil, column = nil, limit = 10)
  return self.class.none if compared_string.nil? || column.nil?
  order_str = self.class.send(:sanitize_sql_for_conditions, ["levenshtein(left(#{self.class.table_name}.#{column}, 255), ?)", compared_string[0..254] ])
  self.class.where('id <> ?', self.to_param).
    select("sources.*, #{order_str}").
    order(Arel.sql(order_str)).
    limit(limit)
end