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



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/queries/asserted_distribution/filter.rb', line 15

def initialize(params)
  if p = params[:otu_id]
    if p.kind_of? Array
      @otu_id = params[:otu_id]
    else
      @otu_id = [params[:otu_id]]
    end
  end

  if p = params[:geographic_area_id]
    if p.kind_of? Array
      @geographic_area_id = params[:geographic_area_id]
    else
      @geographic_area_id = [params[:geographic_area_id]]
    end
  end

  @recent = params[:recent] ? true : nil
end

Instance Attribute Details

#geographic_area_idArray

Parameters:

  • otu_id (Array, Integer, String)

Returns:

  • (Array)


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

def geographic_area_id
  @geographic_area_id
end

#otu_idArray

Parameters:

  • otu_id (Array, Integer, String)

Returns:

  • (Array)


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

def otu_id
  @otu_id
end

Instance Method Details

#allActiveRecord::Relation

Returns:

  • (ActiveRecord::Relation)


67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/queries/asserted_distribution/filter.rb', line 67

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
end

#and_clausesActiveRecord::Relation

Returns:

  • (ActiveRecord::Relation)


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

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



40
41
42
43
44
45
46
# File 'lib/queries/asserted_distribution/filter.rb', line 40

def asserted_distribution_attribute_equals(attribute)
  a = send(attribute)
  if a
    return a.empty? ? nil : table[attribute].eq_any(a)
  end
  nil
end

#tableArel::Table

Returns:

  • (Arel::Table)


36
37
38
# File 'lib/queries/asserted_distribution/filter.rb', line 36

def table
  ::AssertedDistribution.arel_table
end