-
Notifications
You must be signed in to change notification settings - Fork 0
Search Overrides
If you want to customize the search form interface for a column, you can define a specially named method in your helper file. The format is #{class_name}_#{column_name}_search_column
or #{column_name}_search_column
. So, for example, to customize the :username
column displayed on your search view of UsersController
, you would add a user_username_search_column
method to your UsersHelper
file. If you want the post to be handled by ActiveScaffold, you need to use the params[:search] namespace. With the helper override this is taken care of if you use the second argument: the options hash. See the example below for more details. Later name will override columns from all models (unless you use clear_helpers
method in ApplicationController
) or if you put it in ApplicationHelper
.
In v2.3 and previous versions format was only #{column_name}_search_column
, so method was named username_search_column
.
Example:
module UsersHelper
# display the "status" field as a dropdown with open and closed options
def user_status_search_column(record, options)
select :record, :status, options_for_select(['open', 'closed']), {:include_blank => as_('- select -')}, options
end
end
Until v2.3 the second argument was only the input name instead of a full options hash, so you would use check_box :record, :is_admin, :name => input_name
. See example below:
module UsersHelper
# display the "status" field as a dropdown with open and closed options, in v2.3 there is no class name prefix
def status_search_column(record, input_name)
select :record, :status, options_for_select(['open', 'closed']), {:include_blank => as_('- select -')}, :name => input_name
end
end
You can customize conditions for a specific column adding a class method to the controller named condition_for_#{column_name}_column
. That method should return a conditions array.
Example:
class UsersController < ApplicationController
def self.condition_for_status_column(column, value, like_pattern)
case value
when 'open'
["#{column.search_sql} IS NOT NULL"]
when 'closed'
["#{column.search_sql} IS NULL"]
end
end
active_scaffold do |config|
...
end
end
Also you can customize conditions for a seach_ui or type column, adding a class method to the controller named condition_for_#{search_ui}_type
. This method should return a conditions array, with query string to interpolate :search_sql, and required values to build the sql. %{search_sql}
will be replaced with each item in column.search_sql and resulting chunks will be OR’ed so an ActiveScaffold column can search in multiple DB columns.
class ApplicationController < ActionController::Base
def self.condition_for_time_range_type(column, value, like_pattern)
case value
when 'morning'
['%{search_sql} BETWEEN ? AND ?', 6, 12]
when 'afternoon'
['%{search_sql} BETWEEN ? AND ?', 13, 19]
when 'night'
['%{search_sql} BETWEEN ? AND ? OR %{search_sql} BETWEEN ? AND ?', 0, 5, 20, 23]
end
end
end