ActiveScaffold per Request Configuration for ActionColumns

7 Comments

If you take a look at Activescaffold per Request Configuration you will learn that you have to create a before_filter to be able to change column set per request.
Well, that does nt feel right, before_filters should be used for aspect oriented programming tasks such as authentication, logging,..

However, it was quite complicated to do it another way with Activescaffold.

I ve added a new feature to my fork which allows you to change column set configuration for a specific action without before_filters.

It s actually really easy, every action has a new method _columns, which you may override in your controller.

Let me give you some easy examples:

def update_columns
  columns = super
  if @record.id == 85
    columns.select {|column| column.name != :last_name}
  else
    columns
  end
end
def field_search_columns
  columns = super
  if sunny_weather
    columns.select {|column| column.name != :rain}
end

Wish you a great weekend.

Advertisements

ActiveScaffold: FieldSearch Column Overrides

4 Comments

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