When you have a really big application you need to be able to control which JavaScript files get loaded into which controller actions when they share the same layout. An example of this might be the admin section requires some JavaScript which uses the same layout but you do not want to expose any information about the admin site to your users. This is a quick method of being able to append a JavaScript file to the list loaded in the header of the HTML

ApplicationController

Create a little helper method to append the JavaScript files into an array

     
1
2
3
4
def append_javascript_file(javascript_file) 
  @javascript_files = [] unless @javascript_files 
@javascript_files << javascript_file 
end

ApplicationHelper

This helper converts the array of JavaScript files into HTML

     
1
2
3
4
5
6
7
def appended_javascript_tags 
  return unless @javascript_files 
  javascript_include_tags = @javascript_files.inject([]) do |tags, javascript_file| 
    tags << javascript_include_tag(javascript_file.to_s) 
  end  
  return javascript_include_tags.join("\n")
end

Layout

Inside the head tags of the layout include the HTML from the helper

     
1
<%= appended_javascript_tags %>

FooController

In the controller add the name of the JavaScript file.

     
1
append_javascript_file :javascript_file_name

This is clearly againt the idea of combining all your JavaScript into one file but when your application gets to a cetain size with enough developers it can be a little hard to maintain. This method should help get one step closer.

Its often really nice to just get a dump of your production database so that you can use it in development. I have been doing this with a Capistrano script. The idea is to clear the local production database then replace it with the version from live.

     
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
role :production_box, "your.host.name.here.com"

desc "Dump and get the production database"
task :dump_and_get_the_production_database, :roles => :production_box do
  # Clear local database
  `mysqladmin -f drop appname_production`
  `mysqladmin create appname_production`
  
  # Create dump, compress and get dump
  filename = "production_dump.#{Time.now.strftime '%Y-%m-%d_%H:%M:%S'}.sql"
  run "mysqldump -uroot appname_production > /tmp/#{filename}"
  run "bzip2 /tmp/#{filename}"
  get "/tmp/#{filename}.bz2", "/tmp/#{filename}.bz2"
  run "rm /tmp/#{filename}.bz2"
  
  # Decompress and import the database dump
  `bunzip2 /tmp/#{filename}.bz2`
  `mysql -uroot appname_production < /tmp/#{filename}`
  `rm /tmp/#{filename}`
end

I am still unable to get exec working on my OSX machine so I have just escaped to the shell for the local commands.

CodeRay try out

August 20th, 2008

I noticed that the Ruby code in the last post looked pretty bad so I have been taking a look at ways of getting it rendered. This is an example using CodeRay

1
2
3
4
5
6
result = foo and bar
(result = foo) and bar
result - foo && bar
if foo and bar
    result = foo && bar
end

CodeRay is pretty impressive to generate it you just paste you code in and run the file. To illustrate this I tried putting in the code used to generate it into CodeRay but it got a little confused.

1
2
3
4
5
6
7
8
9
require 'rubygems'
require 'coderay'
tokens = CodeRay.scan <<-'CODE', :ruby
result = foo and bar
(result = foo) and bar
result - foo && bar
if foo and bar
    result = foo && bar
end

All in all a nice little gem well done Murphy

So what is wrong with this Ruby code?
result = foo and bar
When you use the and in conjunction with an = in an expression the = has a higher order of precedence so it is the same as:
(result = foo) and bar
Clearly this isn't what was intended. You could solve this by introducing parentheses:
result = (foo and bar)
That just looks bad... The answer is to use the && operator which has a higher order of precedence than the =
result = foo && bar
You could use and/or when no assignment is in the expression and &&/|| when there is for example:
if foo and bar
    result = foo && bar
end
The problem with this is that you have to think about which to use in which context. What we have taken to doing is just using && and || everywhere in place of and/or so it just eliminates any confusion.
Currently Selenium doesn't support Firefox 3 which I tend to use as my main development browser. In order to be able to run the tests I downloaded Firefox 2 and then moved my current install of Firefox 3 to ~/Applications and installed version 2 in /Applications
This means that selenium will pick up version 2 and not 3 and your tests will run.

Mashed 2008

June 24th, 2008

It very much felt like the morning after the night before when I turned up at 10am on Sunday. There were a lot of bleary eyes after not much sleep and lots of people playing Rock Band!

Having been at Interesting yesterday I didn't have a hack and felt a little lame... The cool thing was that there were some awesome hacks and again like last year I was really impressed at what people had been able to come up with in 24 hours.
Highlights include:

Dylan subtitles

Dales current cost talk

and of course the rocket going off!

Turntable Zoetrope

June 22nd, 2008

This was one of the most awesome things at Interesting! A great modern twist on the Zoetrope

Interesting 2008

June 21st, 2008

Having learned of Interesting2008 I didn't quite know what to expect when I turned up.
It was certainly interesting in particular the talks on toilets around the world and the history of the hoover. What was nice though is to see people from all different sectors not talking about work and just things interesting.