Module: Card::LookupFilterQuery::Filtering
- Included in:
- Card::LookupFilterQuery
- Defined in:
- lib/card/lookup_filter_query/filtering.rb
Overview
shared filtering methods for FilterQuery classes built on lookup tables
Instance Method Summary collapse
- #add_condition(condition, value) ⇒ Object
- #db_operator(operator, value) ⇒ Object
- #db_value(value) ⇒ Object
- #filter(field, value, operator = nil) ⇒ Object
- #filter_card_id(key, value) ⇒ Object
- #filter_exact_match(key, value) ⇒ Object
- #filter_method(key) ⇒ Object
- #filter_table(_field) ⇒ Object
- #normalize_filter_args ⇒ Object
- #not_ids_query(value) ⇒ Object
- #op_and_val(op, val) ⇒ Object
- #process_filter_option(key, value) ⇒ Object
- #process_filters ⇒ Object
- #restrict_by_cql(col, cql) ⇒ Object
- #restrict_lookup_ids(col, ids) ⇒ Object
- #restrict_to_ids(col, ids) ⇒ Object
- #to_card_id(value) ⇒ Object
Instance Method Details
#add_condition(condition, value) ⇒ Object
84 85 86 87 |
# File 'lib/card/lookup_filter_query/filtering.rb', line 84 def add_condition condition, value @conditions << condition @values << value end |
#db_operator(operator, value) ⇒ Object
89 90 91 |
# File 'lib/card/lookup_filter_query/filtering.rb', line 89 def db_operator operator, value operator || (value.is_a?(Array) ? "IN" : "=") end |
#db_value(value) ⇒ Object
93 94 95 |
# File 'lib/card/lookup_filter_query/filtering.rb', line 93 def db_value value value.is_a?(Array) ? "(?)" : "?" end |
#filter(field, value, operator = nil) ⇒ Object
71 72 73 74 |
# File 'lib/card/lookup_filter_query/filtering.rb', line 71 def filter field, value, operator=nil condition = "#{filter_table field}.#{field} #{op_and_val operator, value}" add_condition condition, value end |
#filter_card_id(key, value) ⇒ Object
37 38 39 40 41 |
# File 'lib/card/lookup_filter_query/filtering.rb', line 37 def filter_card_id key, value return unless (card_id = to_card_id value) filter card_id_map[key], card_id end |
#filter_exact_match(key, value) ⇒ Object
33 34 35 |
# File 'lib/card/lookup_filter_query/filtering.rb', line 33 def filter_exact_match key, value filter key, value if value.present? end |
#filter_method(key) ⇒ Object
24 25 26 27 28 29 30 31 |
# File 'lib/card/lookup_filter_query/filtering.rb', line 24 def filter_method key case key when *simple_filters :filter_exact_match when *card_id_filters :filter_card_id end end |
#filter_table(_field) ⇒ Object
76 77 78 |
# File 'lib/card/lookup_filter_query/filtering.rb', line 76 def filter_table _field lookup_table end |
#normalize_filter_args ⇒ Object
12 13 14 |
# File 'lib/card/lookup_filter_query/filtering.rb', line 12 def normalize_filter_args # override end |
#not_ids_query(value) ⇒ Object
43 44 45 |
# File 'lib/card/lookup_filter_query/filtering.rb', line 43 def not_ids_query value add_condition "#{lookup_class.card_column} not in (?)", value.split(",") end |
#op_and_val(op, val) ⇒ Object
80 81 82 |
# File 'lib/card/lookup_filter_query/filtering.rb', line 80 def op_and_val op, val "#{db_operator op, val} #{db_value val}" end |
#process_filter_option(key, value) ⇒ Object
16 17 18 19 20 21 22 |
# File 'lib/card/lookup_filter_query/filtering.rb', line 16 def process_filter_option key, value if (method = filter_method key) send method, key, value else try "#{key}_query", value end end |
#process_filters ⇒ Object
5 6 7 8 9 10 |
# File 'lib/card/lookup_filter_query/filtering.rb', line 5 def process_filters normalize_filter_args return if @empty_result @filter_args.each { |k, v| process_filter_option k, v if v.present? } @restrict_to_ids.each { |k, v| filter k, v } end |
#restrict_by_cql(col, cql) ⇒ Object
66 67 68 69 |
# File 'lib/card/lookup_filter_query/filtering.rb', line 66 def restrict_by_cql col, cql cql.reverse_merge! return: :id, limit: 0 @conditions << "#{filter_table col}.#{col} IN (#{Card::Query.new(cql).sql})" end |
#restrict_lookup_ids(col, ids) ⇒ Object
61 62 63 64 |
# File 'lib/card/lookup_filter_query/filtering.rb', line 61 def restrict_lookup_ids col, ids existing = @restrict_to_ids[col] @restrict_to_ids[col] = existing ? (existing & ids) : ids end |
#restrict_to_ids(col, ids) ⇒ Object
55 56 57 58 59 |
# File 'lib/card/lookup_filter_query/filtering.rb', line 55 def restrict_to_ids col, ids ids = Array(ids) @empty_result ||= ids.empty? restrict_lookup_ids col, ids end |
#to_card_id(value) ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/card/lookup_filter_query/filtering.rb', line 47 def to_card_id value if value.is_a? Array value.map(&:card_id) else value.card_id end end |