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



463
464
465
466
# File 'lib/ar-extensions/extensions.rb', line 463

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



479
480
481
482
483
484
485
486
487
488
489
490
491
# File 'lib/ar-extensions/extensions.rb', line 479

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



468
469
470
471
472
473
474
475
476
477
# File 'lib/ar-extensions/extensions.rb', line 468

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