| Class | ActiveRecord::Extensions::Comparison |
| In: |
lib/ar-extensions/extensions.rb
|
| Parent: | Object |
ActiveRecord::Extension to translate Hash keys which end in _lt, _lte, _gt, or _gte with the approriate <, <=, >, or >= symbols.
Model.find :all, :conditions=>{ 'number_gt'=>100 }
Model.find :all, :conditions=>{ 'number_lt'=>100 }
Model.find :all, :conditions=>{ 'number_gte'=>100 }
Model.find :all, :conditions=>{ 'number_lte'=>100 }
| SUFFIX_MAP | = | { 'eq'=>'=', 'lt'=>'<', 'lte'=>'<=', 'gt'=>'>', 'gte'=>'>=', 'ne'=>'!=', 'not'=>'!=' } |
| ACCEPTABLE_COMPARISONS | = | [ String, Numeric, Time, DateTime ] |
# File lib/ar-extensions/extensions.rb, line 211
211: def self.process( key, val, caller )
212: process_without_suffix( key, val, caller ) || process_with_suffix( key, val, caller )
213: end
# File lib/ar-extensions/extensions.rb, line 225
225: def self.process_with_suffix( key, val, caller )
226: return nil unless ACCEPTABLE_COMPARISONS.find{ |klass| val.is_a?(klass) }
227: SUFFIX_MAP.each_pair do |k,v|
228: match_data = key.to_s.match( /(.+)_#{k}$/ )
229: if match_data
230: fieldname = match_data.captures[0]
231: return nil unless caller.columns_hash.has_key?( fieldname )
232: str = "#{caller.table_name}.#{caller.connection.quote_column_name( fieldname )} " +
233: "#{v} #{caller.connection.quote( val, caller.columns_hash[ fieldname ] )} "
234: return Result.new( str, nil )
235: end
236: end
237: nil
238: end
# File lib/ar-extensions/extensions.rb, line 215
215: def self.process_without_suffix( key, val, caller )
216: return nil unless caller.columns_hash.has_key?( key )
217: if val.nil?
218: str = "#{caller.table_name}.#{caller.connection.quote_column_name( key )} IS NULL"
219: else
220: str = "#{caller.table_name}.#{caller.connection.quote_column_name( key )}=?"
221: end
222: Result.new( str, val )
223: end