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



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

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]
end

Instance Attribute Details

- (Object) first_name

Returns the value of attribute first_name



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

def first_name
  @first_name
end

- (Object) last_name

Returns the value of attribute last_name



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

def last_name
  @last_name
end

- (Object) last_name_starts_with

Returns the value of attribute last_name_starts_with



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

def last_name_starts_with
  @last_name_starts_with
end

- (Object) limit_to_roles

  • use similar/identical methods in IsData



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

def limit_to_roles
  @limit_to_roles
end

Instance Method Details

- (ActiveRecord::Relation) all

Returns:

  • (ActiveRecord::Relation)


65
66
67
68
69
70
71
# File 'lib/queries/person/filter.rb', line 65

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

- (ActiveRecord::Relation?) and_clauses

Returns:

  • (ActiveRecord::Relation, nil)


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

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) match_first_name



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

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

- (Object) match_last_name



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

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

- (Object) match_roles



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

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

- (Object) match_start_of_last_name



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

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

- (Arel::Table) roles_table

Returns:

  • (Arel::Table)


26
27
28
# File 'lib/queries/person/filter.rb', line 26

def roles_table
  ::Role.arel_table
end

- (Arel::Table) table

Returns:

  • (Arel::Table)


21
22
23
# File 'lib/queries/person/filter.rb', line 21

def table
  ::Person.arel_table
end