Thursday 20 May 2010

Prawn can't dup Fixnum

Prawn has been and gone and updated itself - which is fair enough but damn it for eating up a good few hours while I tried to sort out a bug - that for once wasn't my fault.
Couple of things have changed
pdf.header has gone as has pdf.footer
Now replaced by pdf.repeat :all
That took a little bit of jiggery-pokery to sort out - but not too hard

Then the
can't dup Fixnum error kept cropping up.
This was simple - convert to a string
ie @job.id.to_s

Monday 17 May 2010

Why I love Rails

You get to write tests like this. What could be easier

it "should not allow timesheets to end before they start" do
@timesheet_item.start_time = 1.hour.from_now
@timesheet_item.should_not be_valid
end

Monday 10 May 2010

Rspec controller and respond_to with :js

Wanted to test that my controller was returning an array of ids when pinged by Ajax

respond_to do |format|
format.html # index.html.erb
format.js { render :text => @chart_entries.map {|x| '%' + x.id.to_s} }
format.xml { render :xml => @chart_entries }
end


After some googling and prodding it - came up withL

it "should return an array of ids if given the params[:co]" do
format = mock("format")
format.should_receive(:js).and_return(['%' + @chart_entry.id.to_s])
format.stub!(:html)
format.stub!(:xml)
controller.should_receive(:respond_to).and_yield(format)
get :index, :co=>1, :format=> 'js'
end

Wednesday 5 May 2010

Rspec and named routes

Have a job and a job has many pages. Setting that up for rspec was not as bad as I thought it was going to be

Here is the named route:
map.resources :jobs do |job|
job.resources :pages
end


Setting up the pages_controller_spec.rb was - after a bit of poking around - not too hard:

describe PagesController, " handling GET jobs/1/pages/1 for a job" do
before do
@job = mock_model(Job, {:id=>1})
@page = mock_model(Page)
Page.stub!(:find).and_return(@page)
Job.stub!(:find).and_return(@job)
end

Wednesday 14 April 2010

Using jQuery to ensure only one checkbox is checked at a time

have a slightly dodgy UI.
A bunch of checkboxes that only one can be checked. It should be a radio button, but the problem with radio buttons are that it has to have a value - so that means adding a "null" button which is kind of crappy.

How to make sure that only one checkbox is checked at a time

$(this).parent().siblings().children().filter(':checked').not(this).removeAttr('checked');

Thursday 21 January 2010

Search and replace in SQL

Have a situation where had to change a bunch of values on a one-off in a db. Could have written a script to do it, but found this really useful tip

UPDATE table_name SET field_name = REPLACE(field_name, 'old_string','new_string')

works to change:
/OLD/PATH/TO/SOMEDATA/A_FILES

to:
/NEW/PATH/TO/A_LOT/MORE/NEW_DATA/A_FILES

Wednesday 20 January 2010

Getting the id of the object when inside form_for or fields_for

This was a post I put on StackOverflow - and then found the answer about 20 mins later ... doh

I have the following code that is generating fields for an invoice

THis is in the edit.html.erb for the invoice class

<% f.fields_for(:invoice_items) do |f| %>
<%= render :partial => 'invoice_items/fields', :locals => {:f => f} %>
<% end %>


and I generate the invoice_items as part of the invoice object

@invoice = Invoice.find(params[:id], :include => :invoice_items, :order =>"invoice_items.display_order")

It works just fine, but I need to wrap each one in a div, and assign that object's id to the div. (div id=i_2345 - that kind of thing) so I can use jQuery wizardry.

Where I am stumbling like a new-born foal is how do I access the the id of the invoice_item that is being called?

And the answer is ...

f.object.id