Tuesday, March 2, 2010

ActiveScaffold and Column Order

At its simplest column order can be defined in the current version of ActiveScaffold as of 2010-03-02 with just:
active_scaffold :some_model do |config|
  config.columns = [:my_first_column_name, :my_second_column_name, :my_third_column_name, :my_fourth_column_name]
  ...
end
You can also set column weights to specify order like:
config.columns[my_first_column_name].weight = 20
But, if after definition (perhaps in a before_filter), you would like to exclude or add columns, like:
active_scaffold_config.list.columns.exclude :my_first_column_name
and then later do:
active_scaffold_config.list.columns.add :my_first_column_name
Then if you have "config.cache_classes = false" in your config/environments/development.rb, test.rb, and/or production.rb, you will need to have defined column weights for it to display :my_first_column_name as the first column rather than the last.

However, if "config.cache_classes = true" config/environments/development.rb, test.rb, and/or production.rb, and you exclude and add that column back, it will disregard the weight and lose the column order (it will display :my_first_column_name as the last column rather than the first).

A workaround for this provided by Sergio Cambra in issue 732 that worked for us is to specify:

active_scaffold_config.list.columns = active_scaffold_config.columns._inheritable
If you do this, you only need to define config.columns once like:
active_scaffold :some_model do |config|
  config.columns = [:my_first_column_name, :my_second_column_name, :my_third_column_name, :my_fourth_column_name]
  ...
end
and you don't have to define weights. Basically it is resetting the columns to the original set defined. This also means that you don't need to add columns back or exclude columns you added that you want to remove. This worked for us with config.cache_classes set to true or false in development.rb/test.rb/production.rb.

However, if you need ActiveScaffold to keep track of multiple column excludes and adds historically through the session and you are using "config.cache_classes = true", you'd need to modify this workaround. If that is the case, and it is an option to use "config.cache_classes = false", you may just want to define weights and then add and exclude as much as you want.

No comments: