Module: ActiveScaffold::Finder::ClassMethods

Defined in:
lib/six-updater-web/vendor/plugins/active_scaffold/lib/active_scaffold/finder.rb

Instance Method Summary collapse

Instance Method Details

#condition_for_column(column, value, like_pattern = '%?%') ⇒ Object

Generates an SQL condition for the given ActiveScaffold column based on that column’s database type (or form_ui … for virtual columns?). TODO: this should reside on the column, not the controller



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/six-updater-web/vendor/plugins/active_scaffold/lib/active_scaffold/finder.rb', line 30

def condition_for_column(column, value, like_pattern = '%?%')
  # we must check false or not blank because we want to search for false but false is blank
  return unless column and column.search_sql and not value.blank?
  # AST Begin - TODO - is this still needed?
  begin
  return if (value.is_a?(Array) and value.join.blank?)
  # AST End
  search_ui = column.search_ui || column.column.type
  if self.respond_to?("condition_for_#{column.name}_column")
    self.send("condition_for_#{column.name}_column", column, value, like_pattern)
  elsif self.respond_to?("condition_for_#{search_ui}_type")
    self.send("condition_for_#{search_ui}_type", column, value, like_pattern)
  else
    case search_ui
      when :boolean, :checkbox
      ["#{column.search_sql} = ?", column.column.type_cast(value)]
      when :select
      ["#{column.search_sql} = ?", value[:id]] unless value[:id].blank?
      when :multi_select
      ["#{column.search_sql} in (?)", value.values.collect{|hash| hash[:id]}]
      else
        if column.column.nil? || column.column.text?
          ["LOWER(#{column.search_sql}) LIKE ?", like_pattern.sub('?', value.downcase)]
        else
          ["#{column.search_sql} = ?", column.column.type_cast(value)]
        end
    end
  end
  rescue Exception => e
    logger.error Time.now.to_s + "#{e.inspect} -- on the ActiveScaffold column = :#{column.name}, search_ui = #{search_ui} in #{@controller.class}"
    raise e
end
end

#condition_for_datetime_type(column, value, like_pattern = nil) ⇒ Object Also known as: condition_for_date_type, condition_for_time_type, condition_for_timestamp_type

AST why require like_pattern



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/six-updater-web/vendor/plugins/active_scaffold/lib/active_scaffold/finder.rb', line 97

def condition_for_datetime_type(column, value, like_pattern = nil)
  conversion = value['from']['hour'].blank? && value['to']['hour'].blank? ? 'to_date' : 'to_time'
  from_value, to_value = ['from', 'to'].collect do |field|
    Time.zone.local(*['year', 'month', 'day', 'hour', 'minutes', 'seconds'].collect {|part| value[field][part].to_i}) rescue nil
  end

  if from_value.nil? and to_value.nil?
    nil
  elsif !from_value
    ["#{column.search_sql} <= ?", to_value.send(conversion).to_s(:db)]
  elsif !to_value
    ["#{column.search_sql} >= ?", from_value.send(conversion).to_s(:db)]
  else
    ["#{column.search_sql} BETWEEN ? AND ?", from_value.send(conversion).to_s(:db), to_value.send(conversion).to_s(:db)]
  end
end

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

AST Begin



118
119
120
# File 'lib/six-updater-web/vendor/plugins/active_scaffold/lib/active_scaffold/finder.rb', line 118

def condition_for_exact_type(column, value, like_pattern = nil)
  ["#{column.search_sql} = ?", value]
end

#condition_for_integer_type(column, value, like_pattern = nil) ⇒ Object Also known as: condition_for_decimal_type, condition_for_float_type, condition_for_usa_money_type

AST why require like_pattern



65
66
67
68
69
70
71
72
73
# File 'lib/six-updater-web/vendor/plugins/active_scaffold/lib/active_scaffold/finder.rb', line 65

def condition_for_integer_type(column, value, like_pattern = nil)
  if value['from'].blank? or not ActiveScaffold::Finder::NumericComparators.include?(value['opt'])
    nil
  elsif value['opt'] == 'BETWEEN'
    ["#{column.search_sql} BETWEEN ? AND ?", value['from'].to_f, value['to'].to_f]
  else
    ["#{column.search_sql} #{value['opt']} ?", value['from'].to_f]
  end
end

#condition_for_multi_select_type(column, value, like_pattern = nil) ⇒ Object Also known as: condition_for_usa_state_type



130
131
132
133
134
135
136
137
138
# File 'lib/six-updater-web/vendor/plugins/active_scaffold/lib/active_scaffold/finder.rb', line 130

def condition_for_multi_select_type(column, value, like_pattern = nil)
  case value
  when Hash
    values = value.values
  else
    values = value
  end
  ["#{column.search_sql} in (?)", values]
end

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



122
123
124
125
126
127
128
# File 'lib/six-updater-web/vendor/plugins/active_scaffold/lib/active_scaffold/finder.rb', line 122

def condition_for_record_select_type(column, value, like_pattern = nil)
  if value.is_a?(Array)
    ["#{column.search_sql} IN (?)", value]
  else
    ["#{column.search_sql} = ?", value]
  end
end

#condition_for_string_type(column, value, like_pattern = '%?%') ⇒ Object Also known as: condition_for_email_type, condition_for_text_type

AST condition_for_string_type



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/six-updater-web/vendor/plugins/active_scaffold/lib/active_scaffold/finder.rb', line 80

def condition_for_string_type(column, value, like_pattern = '%?%')
  if !value.is_a?(Hash)
    ["LOWER(#{column.search_sql}) LIKE ?", like_pattern.sub('?', value.downcase)]
  elsif value['from'].blank? or not ActiveScaffold::Finder::StringComparators.flatten.include?(value['opt'])
    nil
  elsif value['opt'] == 'BETWEEN'
    ["#{column.search_sql} BETWEEN ? AND ?", value['from'], value['to']]
  elsif value['opt'].include?('?')
    ["#{column.search_sql} LIKE ?", value['opt'].sub('?', value['from'].downcase)]
  else
    ["#{column.search_sql} #{value['opt']} ?", value['from']]
  end
end

#create_conditions_for_columns(tokens, columns, like_pattern = '%?%') ⇒ Object

Takes a collection of search terms (the tokens) and creates SQL that searches all specified ActiveScaffold columns. A row will match if each token is found in at least one of the columns.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/six-updater-web/vendor/plugins/active_scaffold/lib/active_scaffold/finder.rb', line 7

def create_conditions_for_columns(tokens, columns, like_pattern = '%?%')
  # if there aren't any columns, then just return a nil condition
  return unless columns.length > 0

  tokens = [tokens] if tokens.is_a? String

  where_clauses = []
  columns.each do |column|
    where_clauses << ((column.column.nil? || column.column.text?) ? "LOWER(#{column.search_sql}) LIKE ?" : "#{column.search_sql} = ?")
  end
  phrase = "(#{where_clauses.join(' OR ')})"

  sql = ([phrase] * tokens.length).join(' AND ')
  tokens = tokens.collect do |value|
    columns.collect {|column| (column.column.nil? || column.column.text?) ? like_pattern.sub('?', value.downcase) : column.column.type_cast(value)}
  end.flatten

  [sql, *tokens]
end