-
Notifications
You must be signed in to change notification settings - Fork 332
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. 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.
Warning: In v2.3 and previous versions format was #{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
Warning: 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
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, or 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