Method: ActiveScaffold::Finder::ClassMethods#condition_for_column

Defined in:
lib/active_scaffold/finder.rb

#condition_for_column(column, value, text_search, session) ⇒ 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



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/active_scaffold/finder.rb', line 99

def condition_for_column(column, value, text_search, session)
  like_pattern = like_pattern(text_search)
  value = value.with_indifferent_access if value.is_a? Hash
  column_method = "condition_for_#{column.name}_column"
  if respond_to?(column_method)
    args = [column, value, like_pattern]
    args << session if method(column_method).arity == 4
    return send("condition_for_#{column.name}_column", *args)
  end
  return unless column&.search_sql && value.present?
  search_ui = column.search_ui || column.column_type
  begin
    sql, *values =
      if search_ui && respond_to?("condition_for_#{search_ui}_type")
        send("condition_for_#{search_ui}_type", column, value, like_pattern)
      elsif column.search_sql.instance_of? Proc
        column.search_sql.call(value)
      else
        condition_for_search_ui(column, value, like_pattern, search_ui)
      end
    return nil unless sql

    where_values = []
    sql_conditions = []
    column.search_sql.each do |search_sql|
      if search_sql.is_a?(Hash)
        subquery_sql, *subquery_values = subquery_condition(column, sql, search_sql, values)
        sql_conditions << subquery_sql
        where_values.concat subquery_values
      else
        sql_conditions << sql % {:search_sql => search_sql}
        where_values.concat values
      end
    end
    [sql_conditions.join(' OR '), *where_values]
  rescue StandardError => e
    Rails.logger.error "#{e.class.name}: #{e.message} -- on the ActiveScaffold column :#{column.name}, search_ui = #{search_ui} in #{name}"
    raise e
  end
end