Method: SolrQuery.solr_value

Defined in:
lib/solr_query.rb

.solr_value(object, opts = {}) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/solr_query.rb', line 49

def solr_value(object, opts={})
  downcase    = opts[:downcase]
  dont_escape = opts[:dont_escape]
  
  if object.is_a?(Array) # case when Array will break for has_manys
    if object.empty?
      string = "NIL" # an empty array should be equivalent to "don't match anything"
    else
      string = object.map do |element|
        solr_value(element, opts.merge(:dont_escape => true))
      end.join(" OR ")
      downcase = false # don't downcase the ORs
    end
  elsif object.is_a?(Hash) || object.is_a?(Range)
    return solr_range(object) # avoid escaping the *
  elsif defined?(ActiveRecord) && object.is_a?(ActiveRecord::Base)
    string = object.id.to_s
  elsif object.is_a?(String)
    if downcase && (bits = object.split(" OR ")) && bits.length > 1
      return "(#{solr_value(bits, opts)})"
    else
      string = object
    end
  else
    string = object.to_s
  end
  string.downcase!                    if downcase 
  string = escape_solr_string(string) unless dont_escape
  
  string
end