Class: Card::LookupQuery

Inherits:
Object
  • Object
show all
Includes:
Filtering
Defined in:
lib/card/lookup_query.rb,
lib/card/lookup_query/filtering.rb,
lib/card/lookup_query/active_record_extension.rb

Overview

base class for query classes built on lookup tables

Defined Under Namespace

Modules: ActiveRecordExtension, Filtering

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Filtering

#filter_by_not_ids, #filter_card_id, #filter_exact_match, #filter_method, #normalize_filter_args, #process_filter_option, #process_filters

Constructor Details

#initialize(filter, sorting = {}, paging = {}) ⇒ LookupQuery

Returns a new instance of LookupQuery.



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/card/lookup_query.rb', line 9

def initialize filter, sorting={}, paging={}
  @filter_args = filter
  @sort_args = sorting
  @paging_args = paging

  @conditions = []
  @joins = []
  @values = []
  @restrict_to_ids = {}

  process_sort
  process_filters
end

Instance Attribute Details

#filter_argsObject

Returns the value of attribute filter_args.



6
7
8
# File 'lib/card/lookup_query.rb', line 6

def filter_args
  @filter_args
end

#paging_argsObject

Returns the value of attribute paging_args.



6
7
8
# File 'lib/card/lookup_query.rb', line 6

def paging_args
  @paging_args
end

#sort_argsObject

Returns the value of attribute sort_args.



6
7
8
# File 'lib/card/lookup_query.rb', line 6

def sort_args
  @sort_args
end

Instance Method Details

#condition_sql(conditions) ⇒ Object



33
34
35
# File 'lib/card/lookup_query.rb', line 33

def condition_sql conditions
  lookup_class.sanitize_sql_for_conditions conditions
end

#countArray

Returns:

  • (Array)


56
57
58
59
# File 'lib/card/lookup_query.rb', line 56

def count
  # we need the id because some joins distort the count
  @empty_result ? 0 : main_query.select("#{lookup_table}.id").distinct.count
end

#limitObject



61
62
63
# File 'lib/card/lookup_query.rb', line 61

def limit
  @paging_args[:limit]
end

#lookup_conditionsObject

Returns args for AR’s where method.

Returns:

  • args for AR’s where method



42
43
44
# File 'lib/card/lookup_query.rb', line 42

def lookup_conditions
  condition_sql([@conditions.join(" AND ")] + @values)
end

#lookup_queryObject



23
24
25
26
27
# File 'lib/card/lookup_query.rb', line 23

def lookup_query
  q = lookup_class.where lookup_conditions
  q = q.joins(@joins.uniq) if @joins.present?
  q
end

#lookup_relationObject



37
38
39
# File 'lib/card/lookup_query.rb', line 37

def lookup_relation
  sort_and_page { lookup_query }
end

#lookup_tableObject



29
30
31
# File 'lib/card/lookup_query.rb', line 29

def lookup_table
  @lookup_table ||= lookup_class.arel_table.name
end

#main_queryObject



65
66
67
# File 'lib/card/lookup_query.rb', line 65

def main_query
  @main_query ||= lookup_query
end

#main_resultsObject



69
70
71
72
# File 'lib/card/lookup_query.rb', line 69

def main_results
  # puts "SQL: #{lookup_relation.to_sql}"
  lookup_relation.map(&:card)
end

#runObject

Returns array of metric answer card objects if filtered by missing values then the card objects are newly instantiated and not in the database.

Returns:

  • array of metric answer card objects if filtered by missing values then the card objects are newly instantiated and not in the database



51
52
53
# File 'lib/card/lookup_query.rb', line 51

def run
  @empty_result ? [] : main_results
end