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

- (Filter) initialize(params)

Returns a new instance of Filter



13
14
15
16
17
18
19
20
21
# File 'lib/queries/person/filter.rb', line 13

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

- (Object) first_name

Returns the value of attribute first_name



7
8
9
# File 'lib/queries/person/filter.rb', line 7

def first_name
  @first_name
end

- (Object) last_name

Returns the value of attribute last_name



7
8
9
# File 'lib/queries/person/filter.rb', line 7

def last_name
  @last_name
end

- (Object) last_name_starts_with

Returns the value of attribute last_name_starts_with



8
9
10
# File 'lib/queries/person/filter.rb', line 8

def last_name_starts_with
  @last_name_starts_with
end

- (Object) levenshtein_cuttoff

Returns the value of attribute levenshtein_cuttoff



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

def levenshtein_cuttoff
  @levenshtein_cuttoff
end

- (Object) limit_to_roles

  • use similar/identical methods in IsData



6
7
8
# File 'lib/queries/person/filter.rb', line 6

def limit_to_roles
  @limit_to_roles
end

Instance Method Details

- (ActiveRecord::Relation) all

Returns:

  • (ActiveRecord::Relation)


68
69
70
71
72
73
74
# File 'lib/queries/person/filter.rb', line 68

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

- (ActiveRecord::Relation?) and_clauses

Returns:

  • (ActiveRecord::Relation, nil)


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/queries/person/filter.rb', line 50

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

- (Object) levenshtein_distance(attribute, value)



87
88
89
# File 'lib/queries/person/filter.rb', line 87

def levenshtein_distance(attribute, value)
  Arel::Nodes::NamedFunction.new("levenshtein", [table[attribute], Arel::Nodes::SqlLiteral.new("'#{value}'")] )
end

- (Object) levenshtein_similar



76
77
78
79
80
81
82
83
84
85
# File 'lib/queries/person/filter.rb', line 76

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

- (Object) match_first_name



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

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

- (Object) match_last_name



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

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

- (Object) match_roles



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

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

- (Object) match_start_of_last_name



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

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

- (Arel::Table) roles_table

Returns:

  • (Arel::Table)


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

def roles_table
  ::Role.arel_table
end

- (Arel::Table) table

Returns:

  • (Arel::Table)


24
25
26
# File 'lib/queries/person/filter.rb', line 24

def table
  ::Person.arel_table
end