ActiveScaffold is quite powerful, but sometimes it might not fit your needs. That s why activescaffold offers you many options to extend/change activescaffold s features to your needs.
I would like to talk about FieldSearch Column Overrides today, as an example how you may extend activescaffold.
We will use my howto application as a starting point: howto installation.
Please activate field_search and human conditions in players controller:
active_scaffold :player do |conf| conf.actions.exclude :search conf.actions.add :field_search conf.field_search.columns = [:date_of_birth] conf.field_search.human_conditions = true end
If you start the application you may try fieldsearch. date_of_birth is using activescaffold’s default date search ui.
Let s assume we have a special requirement and we would like to show a select box showing all existing date_of_birth values.
How to do that? It s really quite simple and it takes as only three steps:
search column override
Responsible for rendering our specific search_ui.
players_helper.rb
def date_of_birth_search_column(record, html_options)
selected = html_options.delete :value
players = Player.select('distinct date_of_birth').except(:order).order('date_of_birth DESC').all
select_options = players.collect do |player|
[ l(player.date_of_birth), player.date_of_birth ]
end
options = { :selected => selected,
:include_blank => as_(:_select_)}
select(:record, :date_of_birth, select_options, options, html_options)
end
human condition column override
Responsible for rendering a special human condition.
players_helper.rb
def date_of_birth_human_condition_column(value, options)
"#{Player.human_attribute_name(:date_of_birth)} = #{I18n.l(controller.class.condition_value_for_datetime(value, :to_date))}"
end
condition_for column override
Responsible for generating our special sql condition.
players_controller.rb
def self.condition_for_date_of_birth_column(column, value, like_pattern)
["#{column.search_sql} = ?", column.column.type_cast(value)]
end
My example used an override for one specific column, however you may as well override an existing search_ui type (such as :select or :multiselect) or just create your own search_ui type….