11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
# File 'lib/doppel/active_record/base.rb', line 11
def inherited(subclass)
super
return unless subclass.respond_to?(:scope)
subclass.class_eval do
scope :find_similar, -> (column, sensitivity: 2.0, count: 0, debug: false) do
at1 = arel_table
at2 = at1.alias
lev = Arel::Nodes::NamedFunction.new("levenshtein", [at1[column.to_sym], at2[column.to_sym]])
selects = [at1[Arel.star]]
selects << lev.clone.as('lev') if debug
select(selects).
where(at1[primary_key].not_eq(at2[primary_key])).
where(lev.lteq(sensitivity)).
from([at1, at2]).
group(at1[primary_key], at1[column.to_sym], at2[column.to_sym]).
having(at1[primary_key].count.gt(count)).distinct(at1[Arel.star])
end
scope :find_exact, -> (column, count: 0) do
find_similar(column, sensitivity: 0, count: count)
end
end
end
|