Class: ActiveRecord::Extensions::DatetimeSupport

Inherits:
Object
  • Object
show all
Defined in:
lib/ar-extensions/extensions.rb

Constant Summary collapse

SUFFIX_MAP =
{ 'eq'=>'=', 'lt'=>'<', 'lte'=>'<=', 'gt'=>'>', 'gte'=>'>=', 'ne'=>'!=', 'not'=>'!=' }

Class Method Summary collapse

Class Method Details

.process(key, val, caller) ⇒ Object



460
461
462
463
# File 'lib/ar-extensions/extensions.rb', line 460

def self.process( key, val, caller )
  return unless val.is_a?( Time )
  process_without_suffix( key, val, caller ) || process_with_suffix( key, val, caller )
end

.process_with_suffix(key, val, caller) ⇒ Object



476
477
478
479
480
481
482
483
484
485
486
487
488
# File 'lib/ar-extensions/extensions.rb', line 476

def self.process_with_suffix( key, val, caller )
  SUFFIX_MAP.each_pair do |k,v|
    match_data = key.to_s.match( /(.+)_#{k}$/ )
    if match_data
      fieldname = match_data.captures[0]
      return nil unless caller.columns_hash.has_key?( fieldname )
      str = "#{caller.quoted_table_name}.#{caller.connection.quote_column_name( fieldname )} " +
        "#{v} #{caller.connection.quote( val.to_s(:db), caller.columns_hash[ fieldname ] )} "
      return Result.new( str, nil )
    end
  end
  nil
end

.process_without_suffix(key, val, caller) ⇒ Object



465
466
467
468
469
470
471
472
473
474
# File 'lib/ar-extensions/extensions.rb', line 465

def self.process_without_suffix( key, val, caller )
  return nil unless caller.columns_hash.has_key?( key )
  if val.nil?
    str = "#{caller.quoted_table_name}.#{caller.connection.quote_column_name( key )} IS NULL"
  else
    str = "#{caller.quoted_table_name}.#{caller.connection.quote_column_name( key )}=" +
      "#{caller.connection.quote( val.to_s(:db), caller.columns_hash[ key ] )} "
  end
  Result.new( str, nil )
end