Category: Uncategorized

Standard RSpec tests for models

The create_bank method looks like this:

   def create_bank( options = {})
    Bank.create({
        :person_id     => 1,
        :sort_code     => ‘989898’,
        :name          => ‘Natlays Shanghai’,
        :branch        => ‘Fruit Exchange’,
        :address       => ‘Big Street’,
        :tow          => ‘Tow’,
        :county        => ‘Place’,
        :postcode      => ‘L1 5XX’,
        :account       => ‘123456789’,
      }.merge(options))
  end

RSpec tests look a bit like this 

  it “should be valid” do
    lambda {
      create_bank.should be_valid
    }.should change(Bank,:count).by(1)
  end

  it “should have a sort code” do
    lambda {
     bank = create_bank(:sort_code => nil)
     bank.should have_at_least(1).errors_on(:sort_code)
    }.should_not change(Bank,:count)
  end

  it “should belong to a perso” do
    Bank.reflect_on_association(:person).should_not be_nil
  end

 Testing that destroy is cascaded down to child records (Employer can have many addresses):

  it “should delete addresses” do
    address = Address.new
    employer = create_employer
    employer.addresses « address
    address.should_receive(:destroy).once
    employer.destroy
  end
 

Just running RSpec on one thing while you fix it

This is like an Autotest script but you have to tell it what files to watch, also can be used for anything that waits for when a file changes (assumes the file is called mywatch, the echo sh … line is all one line):

#! /bin/sh
if [ “$#” -eq 0 ]
then echo Usage: $0 command files
  echo runs the command every time the files have changed
  echo put the command into quotes
  echo example:
  echo sh ./mywatch.sh ‘”ruby script/spec spec/models/article_spec.rb —format specdoc”’ spec/models/article_spec.rb app/models/article.rb
  echo ^C quits
else
  command=$1
  shift
  touch /tmp/old_ls.$$
  while :
  do
    ls -l $* > /tmp/new_ls.$$
    diff -q /tmp/new_ls.$$ /tmp/old_ls.$$ || eval $command
    mv /tmp/new_ls.$$ /tmp/old_ls.$$
    sleep 3
  done
fi

Footnote:

Found a Ruby way of doing this http://nubyonrails.com/articles/automation-with-rstakeout also added some code that will use the windows Snarl, rather than the Mac growl to send messages and a one line script  for people who are lazy enough to not want to type long commands, see this.

Railscon 2007 Berlin

I originally started a post about the conference but it got too old and Blog City deleted it. Never mind. Have another go.

The conference was fantastic and I enjoyed every minute of it. The presentations are here.

I went to the charity tutorial on testing, which was hard core but really good. If you want to see what it was like have a look at Ruby Hoedown and click through to the testing tutorial. I’ve watched all of the content from this conference – very good.

By far the worst talk of the whole show was the one by the inventor of REST Roy T. Fielding, Chief Scientist, Day Software, who just read his very dull powerpoint to us and did’t say anything new or useful in language I could understand. This was annoying because the ideas are really good – just expressed in a very closed, boring and inarticulate way.

The talk from Cyndi Mitchell of Thought Works was by far the most interesting visually, she presented using hand-drawn diagrams rather than the usual bullet point bullshit, and what she had to say was interesting and well thought out.

I was most moved by the Rails in Africa talk, and resolved to try and help them. Of course, one month or so later I’ve done nothing about it because I’ve been running around like a headless chicken. 

I met up with some of the guys from the North West Ruby User group, and also eventually got pointed to geekup (another North West England thing). I can recommend Sushi and German beer.

Need to post more on Geekup, the Manchester Ponto de Cultura , learning to use RSpec,  paddling the Washburn, using SVNRepository for my own projects  (only $4 a month for peace of mind – bargain) – just to name a few.

AND … getting into podcasts after being so sniffy about them. (list of good podcast sites to follow)

AND … Going to see my dharma teacher after several years (next week). But that’s pretty private.

Onward 

Diary entry

On 2nd week since the long holiday in August, came back last Tuesday.

We went up to Scotland and stayed near Dunbar. Cheated this time because the weather had been so bad so we stayed in a static caravan. I took Jon to see the Foo Fighters at the fringe in Edinburgh. Supported by a band who said their name too quick and Nine Inch Nails, who were very loud and impossible to understand the words. I enjoyed singing along with Dave G, but it did’t give me the excitement that I had hoped for. Jon found it a bit noisy.

We fitted in a day at North Berwick and walked around the headland at Dunbar golf course, which was really nice and the sun shone for once. Also bought Jon a skim board and he spent a lot of time falling off it.

Came back for just long enough to pack Deb off to Guide camp and go to Bala for a weekend with the Canoe Camping Club folks. Dan showed us is photos of his holiday in Norway. Said he was glad he had done it but did’t want to go back to somewhere where it rains 360 days a year!

Rosie spent both days on Bala lake, day 2 sailing back in Da’s canoe. I managed to get to the Tryweryn for one day and then took Jon to the beach at Barmouth, after spending over an hour failing to find anywhere to park we went about 2 miles north of the place and found a really nice public beach over some dunes. It was a bit of a slog to cart the gear but OK. Jon finally mastered his skim board (ish).

Tryweryn tired me out; still lacking the fitness I used to have. But never mind, it’s coming back slowly. 

Then back at work. The CEO has agreed that I can go to the Rails Europe conference in Berlin and I’m getting quite excited about it. I also feel like writing a white paper on how not to design an external interface because of some of the interfaces I’ve been working with over the last month or so. It begins with: Do not use SOAP to send XML – use it to send structured objects, goes by way of always return consistent outputs to your users and finishes with use the framework properly.

I wonder if their ears are burning?

Comment on BBC article about child molester’s “art”

Just give the proceeds to charity, like those from Mein Kampf are given to Jewish charities. It’s not hard. I also wonder if the books were written to get close to children, or simply because it is something he is good at and he wanted to make some money. It’s good that his awful, criminal life, has produced something of merit – the rest of us should take it from him and use it for what it is.

http://news.bbc.co.uk/1/hi/magazine/6979731.stm  

Extending/Mixin Ruby classes with Modules

module Mod
  def x
     puts “x”
  end
end

class C1
  extend Mod
end

class C2
  include Mod
end

C1.x – will work

C2.x – will not, but C2.new.x will

In essence:

  • extend – class level
  • include – instance level

None of the books explain this properly. They go into great detail with lots of pictures, but this simple thing is not explained anywhere, or is wrapped up in some stupid example that is really trying to make some other point. In the end I looked at the supplied code and worked out how to do it. Cost me a couple of hours and really very trivial.

You can also have private methods in your modules and they all go in nice ’n easy. 

Just a quick note about arrays of objects in a Rails web service server

If you want an array of something, e.g. past addresses:

class PersonWrapper < ActionWebService::Struct
member :title ,:integer
# … blah
member :addresses ,[AddressWrapper]
member :employers ,[EmployerWrapper]
end

This will give you an array of past addresses and one of employers. It’s one of those things that’s so obvious it is’t documented – so you can’t find it and don’t know it’s there … I will write a longer post/article/wiki about how easy it is to create a web service with complex objects whe I have time.

Response sent to Register Article about gmail

Here

It’s not free 

Cos you’re reading the ads which are generating cash. 

I don’t mind, because they’re text ads rather than really irritating Flash aminations (El Reg take note??).

I think Guy has a point – I also use MacMail, which is POP3, and it sends a mail when the mailbox starts to get full. It’s not rocket science.

By default POP3 does’t delete with gmail – but it does know that it’s been downloaded so you don’t get it again. That’s part of the point with it …

One thing that annoys me, as a developer, is that I can’t send and receive certain attachments but it’s not that big a deal, really.

My ruby on rails form select won’t work?

Got bit by this again. So, for the record …

If you’re selects are’t working (i.e. you’ve got and ID coming through but it is’t showing in a select box).

Check your types

I had:

 @admin.introducer_id = admin_args[:introducer_id]

and this in the rhtml:

 <% form_for(:admin, @admin, :url => admin_path(“index”), :id => ‘admi’ ) do |f| >
    <
= f.select :introducer_id, @introducer_list, {}, {:onChange => ‘go()’ }%>
<%end%>

The it just was’t working no matter what I did.

Then I noticed that a select that was a list of strings was … so … 

 @admin.introducer_id = admin_args[:introducer_id].to_i

and it started working. This has happened to me before – I had a lookup table that stored the lookup ID in a string (‘cos it could have been a string as well as an ID) and it did’t work then. Stupid, stupid boy. Ruby does’t coerce stuff.

Hope this helps the next person.