Class: Queries::Person::Filter

Inherits:
Object
  • Object
show all
Defined in:
lib/queries/person/filter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Filter

Returns a new instance of Filter



18
19
20
21
22
23
24
25
# File 'lib/queries/person/filter.rb', line 18

def initialize(params)
  @limit_to_roles = params[:roles]
  @limit_to_roles ||= [] 
  @first_name = params[:first_name]
  @last_name = params[:last_name]
  @last_name_starts_with = params[:last_name_starts_with]
  @levenshtein_cuttoff = params[:levenshtein_cuttoff] || 4
end

Instance Attribute Details

#first_nameObject

Returns the value of attribute first_name



12
13
14
# File 'lib/queries/person/filter.rb', line 12

def first_name
  @first_name
end

#last_nameObject

Returns the value of attribute last_name



12
13
14
# File 'lib/queries/person/filter.rb', line 12

def last_name
  @last_name
end

#last_name_starts_withObject

Returns the value of attribute last_name_starts_with



13
14
15
# File 'lib/queries/person/filter.rb', line 13

def last_name_starts_with
  @last_name_starts_with
end

#levenshtein_cuttoffObject

Returns the value of attribute levenshtein_cuttoff



15
16
17
# File 'lib/queries/person/filter.rb', line 15

def levenshtein_cuttoff
  @levenshtein_cuttoff
end

#limit_to_rolesObject

  • use similar/identical methods in IsData



11
12
13
# File 'lib/queries/person/filter.rb', line 11

def limit_to_roles
  @limit_to_roles
end

Instance Method Details

#allActiveRecord::Relation

Returns:

  • (ActiveRecord::Relation)


72
73
74
75
76
77
78
# File 'lib/queries/person/filter.rb', line 72

def all
  if c = and_clauses
    ::Person.includes(:roles).where(and_clauses.to_sql).references(:roles).distinct
  else
    ::Person.all
  end
end

#and_clausesActiveRecord::Relation?

Returns:

  • (ActiveRecord::Relation, nil)


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/queries/person/filter.rb', line 54

def and_clauses
  clauses = [
    match_first_name,
    match_last_name,
    match_roles,
    match_start_of_last_name
  ].compact

  return nil if clauses.empty?

  a = clauses.shift
  clauses.each do |b|
    a = a.and(b)
  end
  a
end

#levenshtein_distance(attribute, value) ⇒ Object



91
92
93
94
95
# File 'lib/queries/person/filter.rb', line 91

def levenshtein_distance(attribute, value)
  value = "'" + value.gsub(/'/, "''") + "'"
  a = ApplicationRecord.sanitize_sql(value) 
  Arel::Nodes::NamedFunction.new("levenshtein", [table[attribute], Arel::Nodes::SqlLiteral.new(a) ] )
end

#levenshtein_similarObject



80
81
82
83
84
85
86
87
88
89
# File 'lib/queries/person/filter.rb', line 80

def levenshtein_similar
  clauses = []
  clauses.push levenshtein_distance(:last_name, last_name).lt(levenshtein_cuttoff) if !last_name.blank?
  clauses.push levenshtein_distance(:first_name, first_name).lt(levenshtein_cuttoff) if !first_name.blank?

  a = clauses.shift
  a = a.and(clauses.first) if clauses.any?

  ::Person.where(a.to_sql)
end

#match_first_nameObject



45
46
47
# File 'lib/queries/person/filter.rb', line 45

def match_first_name
  first_name.blank? ? nil : table[:first_name].matches('%' + first_name + '%')
end

#match_last_nameObject



37
38
39
# File 'lib/queries/person/filter.rb', line 37

def match_last_name
  last_name.blank? ? nil : table[:last_name].matches('%' + last_name + '%')
end

#match_rolesObject



49
50
51
# File 'lib/queries/person/filter.rb', line 49

def match_roles
  limit_to_roles.empty? ? nil : roles_table[:type].eq_any(limit_to_roles)
end

#match_start_of_last_nameObject



41
42
43
# File 'lib/queries/person/filter.rb', line 41

def match_start_of_last_name
  last_name_starts_with.blank? ? nil : table[:last_name].matches(last_name_starts_with + '%')
end

#roles_tableArel::Table

Returns:

  • (Arel::Table)


33
34
35
# File 'lib/queries/person/filter.rb', line 33

def roles_table
  ::Role.arel_table
end

#tableArel::Table

Returns:

  • (Arel::Table)


28
29
30
# File 'lib/queries/person/filter.rb', line 28

def table
  ::Person.arel_table
end