Class: Trample::Condition

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

Instance Method Summary collapse

Constructor Details

#initialize(attrs) ⇒ Condition



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

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

Instance Method Details

#as_json(*opts) ⇒ Object



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

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



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

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

#runtime_query_nameObject



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

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



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

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