Class: Card::LookupQuery
- Inherits:
-
Object
- Object
- Card::LookupQuery
- 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
-
#filter_args ⇒ Object
Returns the value of attribute filter_args.
-
#paging_args ⇒ Object
Returns the value of attribute paging_args.
-
#sort_args ⇒ Object
Returns the value of attribute sort_args.
Instance Method Summary collapse
- #condition_sql(conditions) ⇒ Object
- #count ⇒ Array
-
#initialize(filter, sorting = {}, paging = {}) ⇒ LookupQuery
constructor
A new instance of LookupQuery.
- #limit ⇒ Object
-
#lookup_conditions ⇒ Object
Args for AR’s where method.
- #lookup_query ⇒ Object
- #lookup_relation ⇒ Object
- #lookup_table ⇒ Object
- #main_query ⇒ Object
- #main_results ⇒ Object
-
#run ⇒ Object
Array of metric answer card objects if filtered by missing values then the card objects are newly instantiated and not in the database.
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_args ⇒ Object
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_args ⇒ Object
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_args ⇒ Object
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 |
#count ⇒ 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 |
#limit ⇒ Object
61 62 63 |
# File 'lib/card/lookup_query.rb', line 61 def limit @paging_args[:limit] end |
#lookup_conditions ⇒ Object
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_query ⇒ Object
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_relation ⇒ Object
37 38 39 |
# File 'lib/card/lookup_query.rb', line 37 def lookup_relation sort_and_page { lookup_query } end |
#lookup_table ⇒ Object
29 30 31 |
# File 'lib/card/lookup_query.rb', line 29 def lookup_table @lookup_table ||= lookup_class.arel_table.name end |
#main_query ⇒ Object
65 66 67 |
# File 'lib/card/lookup_query.rb', line 65 def main_query @main_query ||= lookup_query end |
#main_results ⇒ Object
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 |
#run ⇒ Object
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 |