Class: Queries::AssertedDistribution::Filter

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

Overview

!! does not inherit from base query

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Filter

Returns a new instance of Filter



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

def initialize(params)
  @otu_id= params[:otu_id]
  @geographic_area_id = params[:geographic_area_id] 
  @recent = params[:recent] ? true : nil
end

Instance Attribute Details

#geographic_area_idObject

Returns the value of attribute geographic_area_id



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

def geographic_area_id
  @geographic_area_id
end

#otu_idObject

Returns the value of attribute otu_id



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

def otu_id
  @otu_id
end

#recentObject

Boolean



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

def recent
  @recent
end

Instance Method Details

#allActiveRecord::Relation

Returns:

  • (ActiveRecord::Relation)


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

def all
  a = and_clauses
  b = nil

  q = nil 
  if a && b
    q = b.where(a).distinct
  elsif a
    q = ::AssertedDistribution.where(a).distinct
  elsif b
    q = b.distinct
  else
    q = ::AssertedDistribution.all
  end

  q = q.order(updated_at: :desc) if recent
  q
end

#and_clausesActiveRecord::Relation

Returns:

  • (ActiveRecord::Relation)


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/queries/asserted_distribution/filter.rb', line 31

def and_clauses
  clauses = []
 
  clauses += [
    asserted_distribution_attribute_equals(:otu_id),
    asserted_distribution_attribute_equals(:geographic_area_id),
  ].compact
  
  return nil if clauses.empty?

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

#asserted_distribution_attribute_equals(attribute) ⇒ Object



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

def asserted_distribution_attribute_equals(attribute)
  a = send(attribute)
  a.blank? ? nil : table[attribute].eq(a)
end

#tableArel::Table

Returns:

  • (Arel::Table)


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

def table
  ::AssertedDistribution.arel_table
end