Method: ActiveScaffold::Finder::ClassMethods#condition_for_range

Defined in:
lib/active_scaffold/finder.rb

#condition_for_range(column, value, like_pattern = nil) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/active_scaffold/finder.rb', line 198

def condition_for_range(column, value, like_pattern = nil)
  if !value.is_a?(Hash)
    if column.text?
      value = column.active_record? ? column.active_record_class.sanitize_sql_like(value) : value
      ["%<search_sql>s #{ActiveScaffold::Finder.like_operator} ?", like_pattern.sub('?', value)]
    else
      ['%<search_sql>s = ?', ActiveScaffold::Core.column_type_cast(value, column.column)]
    end
  elsif ActiveScaffold::Finder::NULL_COMPARATORS.include?(value[:opt])
    condition_for_null_type(column, value[:opt], like_pattern)
  elsif value[:from].blank?
    nil
  elsif ActiveScaffold::Finder::STRING_COMPARATORS.values.include?(value[:opt])
    text = column.active_record? ? column.active_record_class.sanitize_sql_like(value[:from]) : value[:from]
    [
      "%<search_sql>s #{'NOT ' if value[:opt].start_with?('not_')}#{ActiveScaffold::Finder.like_operator} ?",
      value[:opt].sub('not_', '').sub('?', text)
    ]
  elsif value[:opt] == 'BETWEEN'
    ['(%<search_sql>s BETWEEN ? AND ?)', value[:from], value[:to]]
  elsif ActiveScaffold::Finder::NUMERIC_COMPARATORS.include?(value[:opt])
    ["%<search_sql>s #{value[:opt]} ?", value[:from]]
  end
end