Class: Where::Clause

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

Overview

Used internally to Where. You shouldn’t have any reason to interact with this class.

Instance Method Summary collapse

Constructor Details

#initialize(criteria, conjuction = "AND") ⇒ Clause

:nodoc:



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/where.rb', line 177

def initialize(criteria, conjuction = "AND") # :nodoc:
  @conjuction=conjuction.upcase
  criteria = criteria.first if criteria.class==Array && criteria.length==1
    
  case criteria
  when Array # if it's an array, sanitize it
    @criteria = sanitize_sql_array(criteria)
  when Hash
    return nil if criteria.empty?
    @criteria = criteria.keys.sort_by { |v| v.to_s }.map do |field|
      if criteria[field].nil?
        "#{field} IS NULL"
      else
        sanitize_sql_array(["#{field} = ?", criteria[field]])
      end
    end.join(' AND ')
  else
    @criteria = criteria.to_s   # otherwise, run to_s.  If it's a recursive Where clause, it will return the sql we need
  end
end

Instance Method Details

#sanitize_sql_array(*args) ⇒ Object



198
199
200
# File 'lib/where.rb', line 198

def sanitize_sql_array(*args)
  ActiveRecord::Base.send(:sanitize_sql_array, *args)
end

#to_s(omit_conjuction = false) ⇒ Object

:nodoc:



202
203
204
205
206
207
208
209
# File 'lib/where.rb', line 202

def to_s(omit_conjuction=false) # :nodoc:
  if omit_conjuction
    output = @conjuction.include?("NOT") ? "NOT " : ""
    output << "(#{@criteria})"
  else
    " #{@conjuction} (#{@criteria})"
  end
end