Class: Trample::Condition

Inherits:
Object
  • Object
show all
Defined in:
lib/trample/condition.rb

Instance Method Summary collapse

Constructor Details

#initialize(attrs) ⇒ Condition

Returns a new instance of Condition.



24
25
26
27
# File 'lib/trample/condition.rb', line 24

def initialize(attrs)
  attrs.merge!(single: true) if attrs[:name] == :keywords
  super(attrs)
end

Instance Method Details

#as_json(*opts) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/trample/condition.rb', line 33

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

Returns:

  • (Boolean)


29
30
31
# File 'lib/trample/condition.rb', line 29

def blank?
  values.reject { |v| v == "" || v.nil? }.empty? && !range?
end

#runtime_query_nameObject



48
49
50
51
52
53
54
55
# File 'lib/trample/condition.rb', line 48

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_queryObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/trample/condition.rb', line 57

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