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
26
# 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
 # set_alternate_value(params)
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)


75
76
77
78
79
80
81
# File 'lib/queries/person/filter.rb', line 75

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)


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

def and_clauses
  clauses = [
    match_first_name,
    match_last_name,
    match_roles,
    match_start_of_last_name,
 #  matching_alternate_value_on_values(:last_name, [last_name]),
 #  matching_alternate_value_on_values(:first_name, [first_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



94
95
96
97
98
# File 'lib/queries/person/filter.rb', line 94

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



83
84
85
86
87
88
89
90
91
92
# File 'lib/queries/person/filter.rb', line 83

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



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

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

#match_last_nameObject



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

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

#match_rolesObject



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

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

#match_start_of_last_nameObject



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

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)


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

def roles_table
  ::Role.arel_table
end

#tableArel::Table

Returns:

  • (Arel::Table)


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

def table
  ::Person.arel_table
end