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….
Jun 21, 2011 @ 01:45:08
This is really interesting and I think illustrates a number of key ways to customize AS. Would you mind elaborating the code above to explain what parameters and methods you are using to customize the column’s field_search? Those are “three steps” that aren’t as clear as many of your other examples.
I am trying to implement a more flexible search ability than the search_sql method because I need to search across different tables in my database, given a virtual column I have for one of my models. As you know, the sql string created by AS in the normal search bar doesn’t seem to allow cross-table searches. But I know there must be a way to achieve this fairly easily. I feel like your example above is extremely relevant, but I’m having a hard time parsing all the elements out. If you have any suggestions for me, I’d love to hear them.
Thanks a lot.
Jun 21, 2011 @ 11:25:54
Hm not sure what you mean:
you ve got three override methods
- one for formatting the html output
- one for formatting human conditions
- one for defining sql condition
nevertheless, coming back to your search_sql question.
Have you taken a look at that link as well:
https://github.com/activescaffold/active_scaffold/wiki/Search-on-second-order-association
Jun 22, 2011 @ 14:48:18
Thanks for the reply. I didn’t have a second-order search, it was a first-order search. The problem’s root cause was Rails’ thinking the default primary key was instance.id rather than instance.instance_id. Once I fixed that in the models, the association columns populated. The virtual column I had already set up was added to the search columns, and search_sql used to point to the correct table. Resolution.
Can’t thank you enough for the suggestions and help.
Aug 25, 2012 @ 23:19:23
Hi there,
trying to find out how to translate/override these two active record messages:
There were problems with the following fields:
% errors prohibited this zmluva from being saved
I can override activescaffold actons i I18n file, but not sure how text above would be represented there.
Will be great if you can help.
Thanks
Regards