Class: Trample::Condition
- Inherits:
-
Object
- Object
- Trample::Condition
- Defined in:
- lib/trample/condition.rb
Instance Method Summary collapse
- #as_json(*opts) ⇒ Object
- #blank? ⇒ Boolean
-
#initialize(attrs) ⇒ Condition
constructor
A new instance of Condition.
- #lookup_autocomplete ⇒ Object
- #runtime_query_name ⇒ Object
- #to_query ⇒ Object
Constructor Details
#initialize(attrs) ⇒ Condition
Returns a new instance of Condition.
25 26 27 28 |
# File 'lib/trample/condition.rb', line 25 def initialize(attrs) attrs.merge!(single: true) if attrs[:name] == :keywords super(attrs) end |
Instance Method Details
#as_json(*opts) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/trample/condition.rb', line 52 def as_json(*opts) if single? values.first elsif range? {}.tap do |json| json[:from_eq] = from_eq if from_eq? json[:from] = from if from? json[:to_eq] = to_eq if to_eq? json[:to] = to if to? end else { values: values, and: and? } end end |
#blank? ⇒ Boolean
48 49 50 |
# File 'lib/trample/condition.rb', line 48 def blank? values.reject { |v| v == "" || v.nil? }.empty? && !range? end |
#lookup_autocomplete ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/trample/condition.rb', line 30 def lookup_autocomplete if require_autocomplete_lookup? = (lookup || {}).dup klass = .delete(:klass) || '::Trample::TextLookup' .assert_valid_keys(:key, :label) = .merge({ search_klass: search_klass, condition_name: name }) lookup_instance = klass.to_s.constantize.new() self.values = lookup_instance.load(values) end end |
#runtime_query_name ⇒ Object
67 68 69 70 71 72 73 74 |
# File 'lib/trample/condition.rb', line 67 def runtime_query_name name = query_name return "#{name}.text_start" if prefix? return "#{name}.text_middle" if any_text? return "#{name}.analyzed" if search_analyzed? return "#{name}.autocomplete" if autocomplete? name end |
#to_query ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/trample/condition.rb', line 76 def to_query if range? to_range_query else _values = values.dup.map { |v| v.is_a?(Hash) ? v.dup : v } user_queries = _values.select(&is_user_query) transformed = transform_values(_values - user_queries) user_query_clause = derive_user_query_clause(user_queries) main_clause = derive_main_clause(transformed) if user_query_clause.present? { or: [ main_clause, user_query_clause ] } else main_clause end end end |