24 Oct 2009

Adding multiple column calculations to ActiveScaffold

Recently I have been making great use of the excellent ActiveScaffold for a Ruby on Rails project I have been hired to create.

ActiveScaffold (AS) makes it really easily to do a calculation on the columns, for example displaying the average of a column. In your controller you just need to do:

active_scaffold :sale do |config|
config.columns[:product_profit].calculate = :avg

However my client wanted both averages and totals at the bottom, so From reading the documentation and a quick web search it didn't look to be a pre-built way to do this, so I had to look at customising the ActiveScaffold.

I wanted to be able to set calculate the standard way with a single option, or have an array of options:

config.columns[:chassis_profit].calculate = :avg
config.columns[:chassis_profit].calculate = [:sum, :avg]

The answer was to be found in vendor/plugins/active_scaffold/frontends/default/views/_list_calculations.html.erb. This file was responsible for calling the caluation and displaying the result. The chosen calculation option was accessable via column.calculate. I needed a way to handle both single values and arrays. The following code allowed me to iterate over an array, but still works if column.calculate was a single value:

<% for calculation_type in [*column.calculate] -%>
column.calculate = calculation_type
<% end -%>

As I looped over the array I set column.calculate to each single value, so I did not affect the way AS worked.

23 Oct 2009

Things to look at


Some thoughts to flesh out

  1. Adding win states and game-like mechanics into stuff to postively encourage peoples actions (for example: visible energy usage meters, displaying current MPG in cars, points & badges in software). This idea seems to have the potential to be very powerful.
  2. If you yourself want to make a positive impact and a meaningful footprint on the world, you need to recruit people that will also take action. You need to be offering those people a compelling goal, one that will also offer them the oppurtunity to be part of history. Your goal, your message needs to be clear.
  3. Retail anchor tenants (i.e. you have a new shopping centre and literally pay a big brand to move in). Again seems a powerful idea if transposed into other fields.

10 Oct 2009

More books

There seems to be a theme with many of the books I really enjoy. They give you some insight into things really are; just science and figures, no fluff.