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
end



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
-or-
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

http://www.flickr.com/photos/timferriss/2919230853/in/set-72157607796715778/
http://www.dolectures.com/brainfood/

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.

26 Sept 2009

My recent TV viewing

The Love of Money
Three part documnetary about the recent financial crisis:
1/3. Why the collapse of Lehman Brothers had disastrous consequences for the world's economy.
2/3. Examining the boom years before the global crash, with testimony from key decision makers.
3/3. The story of how politicians reacted to the crash, and what has been learnt from it.
http://www.bbc.co.uk/programmes/b00mqmjs/episodes/player

Alone in the Wild
http://www.channel4.com/programmes/alone-in-the-wild/4od

Nigel Slater's Simple Suppers
http://www.bbc.co.uk/programmes/b00mm51f/episodes/player

Economy Gastronomy
http://www.bbc.co.uk/programmes/b00m1c34

Jamie's American Road Trip
Jamie Oliver tours parts of America not often seen on TV and enjoys some of the unique food

Jonathan Meades: Off Kilter
1/3. Architecture critic Jonathan Meades takes a quixotic tour of Scotland.
2/3. On the isle of Lewis and Harris, Meades discovers serenity, Calvinism and peat bog bodies.
3/3. Meades celebrates an oil refinery and takes potshots at overpaid footballers.
http://www.bbc.co.uk/programmes/b00mrbdt

Peep Show - Season 6
http://www.channel4.com/programmes/peep-show/4od

7 Aug 2009

Book Recommendations

A few excellent books worth reading:

The Science of Cooking, by Peter Barham
If you watch a few TV cooking programmes you will hear often repeated rules without any explanantion, "sear your steak first", "always sieve flour". If this ever gives you the pestering thought of, "Why?", then this is the book for you.



The Unfolding of Language, by Guy Deutscher
Absolutely superb! Gives you a great insight into why language changes and how it develops; why irregularites come about, and shows how fundamental metaphor is to any language.


The Selfish Gene

14 Mar 2009

Running Rails 'on' Windows

I have been developing a Rails app on my aging Windows 2000 desktop PC since 2006. Luckily this PC has been trouble-free, so only ever had to get Ruby and Rails installed and working once.

Recently I decided that I should really move off an operating system that is nearly a decade old, and jump head-first into the modern world.

I dusted off my 7 year old Windows XP CD and popped it into my computer.

With my fresh new computer, I had a number of unsuccesful attempts at getting Rails working. I tried the Ruby One-Click Installer, but had a few problems when I updated to the latest version of Gems.

After some random searching I happened upon a great answer over at StackOverflow. The suggestion was to run Rails on a linux virtual machine. So that's what I did ...


1. Installed VirtualBox 2.1.4
Easy!

2. Installed virtual machine Ubuntu Server 8.10
This is pretty easy, you just download the ISO from the Ubuntu site, in VirtualBox you create a new machine, and point it at the ISO.

3. Install Ruby & Rails
This was trouble free, I just followed the steps of this screencast over at O'Reilly Blogs.


At this point I made a decision that I would continue to do my actual coding in Windows and with an IDE (shhh, don't tell anybody or I'll get kicked out of the Rails community). I had previously used Eclipse with Ruby Development Tools and Subclipse plugins. This had served me well, but I recently had the opportunity to try NetBeans and liked it, so decided to go with this.

This meant the next stage was to have some sort of common file area between the Host and the Virtual Machine. I could of setup Samba on Ubuntu, but VirtualBox provides the feature of Shared Folders, which allows the guest operating systems access to folders on the Host.


4a. Setup Shared Folders
(a little bit of pain, more details to follow)

4b. Installed the VirtualBox Guest Additons in the the Ubuntu Server.
(a little bit of pain, more details to follow)

5. Configured port forwarding
There are number of networking options with VirtualBox, the default is NAT with the guests inside their own private network and VirtualBox doing NAT. This gives the guest easy access to the Internet, however it's services won't be accessible by the Host. The easiest option seems to be stick with NAT and configure port forwarding as per the instructions here.