ActiveScaffold: FieldSearch Column Overrides


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

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.

def date_of_birth_search_column(record, html_options)
  selected = html_options.delete :value
  players ='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 ]
  options = { :selected => selected,
                  :include_blank => as_(:_select_)}
  select(:record, :date_of_birth, select_options, options, html_options)

human condition column override

Responsible for rendering a special human condition.

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))}"

condition_for column override

Responsible for generating our special sql condition.

def self.condition_for_date_of_birth_column(column, value, like_pattern)
  ["#{column.search_sql} = ?", column.column.type_cast(value)]

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….


ActiveScaffold: singular polymorphic association inline actionlink


Quite a complicated subject for a post. 🙂 Hope at least some of you have an idea about the topic of this post.
Activescaffold supports concept of an inline actionlink, which is basically just a list column as a hyperlink. Out of the box it is used for associations to have a one click path to associated records. Unfortunetly, it was nt supported for singular polymorphic associations and in one of my projects I was missing this feature. I had to implement a log feature using a database as store. Each log statement should include a reference to the source. source was a perfect candidate for a polymorphic association.
I just had to extend activescaffold to support singular polymorphic assocation inline actionlinks.

I ve prepared a simple example app including invoice and applog model for those of you who are interesting in trying out.

Setup Example Application

  1. Download: one_step_active_scaffold
  2. Download: polymorphici_blog.rb and rename it to model_setup.rb
  3. Execute following command: ruby one_step_activescaffold.rb polymorphic_app jquery
  4. cd polymorphic_app
  5. rails s
  6. http://localhost:3000/invoices
  7. Create new invoice with a log entry
  8. http://localhost:3000/app_logs