Here is a little tip
have the following in my routes.rb file
map.resources :jobs do |job|
job.resources :pdfs
end
functional test was failing
Couldn't find Job without an ID
So add this to the functional test
get :show, :id =>@pdf, :job_id => @job.id
Passing the job_id puts it in the parameters hash, but not inside the array for the object.
Thursday, 27 August 2009
Monday, 24 August 2009
I am starting to hate Factory Girl
Why oh why is it not passing the associated id?
Clearly I am doing something wrong, but wtf is it?
Factory.define :output_type do |f|
f.sequence(:name) {|n| "proofer#{n}"}
f.live true
end
Factory.define :device_type do |f|
f.sequence(:name) {|n| "device_type#{n}"}
f.live true
f.association :output_type
end
Here is the controller test:
test "should create device_type" do
output_type = Factory(:output_type)
@device_types_attr = Factory.attributes_for(:device_type)
assert_difference('DeviceType.count') do
post :create, :device_type => @device_types_attr
end
assert_redirected_to device_type_path(assigns(:device_type))
end
When I run the actual script through a browser and watch the log I get this
Parameters: {"commit"=>"Create", "device_type"=>{"name"=>"testLaser", "live"=>"1", "output_type_id"=>"2"},
But when I run the test, I get this
Parameters: {"device_type"=>{"name"=>"device_type1", "live"=>true}}
ie - no output type is being sent.
Why?
(hmmm - just noticed the live=>true and live=>'1'. Don't suppose that is the problem, but worth checking it out)
Clearly I am doing something wrong, but wtf is it?
Factory.define :output_type do |f|
f.sequence(:name) {|n| "proofer#{n}"}
f.live true
end
Factory.define :device_type do |f|
f.sequence(:name) {|n| "device_type#{n}"}
f.live true
f.association :output_type
end
Here is the controller test:
test "should create device_type" do
output_type = Factory(:output_type)
@device_types_attr = Factory.attributes_for(:device_type)
assert_difference('DeviceType.count') do
post :create, :device_type => @device_types_attr
end
assert_redirected_to device_type_path(assigns(:device_type))
end
When I run the actual script through a browser and watch the log I get this
Parameters: {"commit"=>"Create", "device_type"=>{"name"=>"testLaser", "live"=>"1", "output_type_id"=>"2"},
But when I run the test, I get this
Parameters: {"device_type"=>{"name"=>"device_type1", "live"=>true}}
ie - no output type is being sent.
Why?
(hmmm - just noticed the live=>true and live=>'1'. Don't suppose that is the problem, but worth checking it out)
Wednesday, 19 August 2009
It works, but I am not sure it is right. Seems very kludgy
have an organisation class, and organisations can be clients, suppliers, printers or prospects.
Want to be able to control this through a form - using check boxes.
The models etc are:
class Client <>
belongs_to :organisation
class Printer <>
belongs_to :organisation
class Organisation <>
has_one :printer
has_one :client
But have had a nightmare sorting out the controller for handling all of this. I am sure there is a better way of doing this.
def update
#get the org type ids
params[:organisation_type_ids] ||= []
#check to se if we have clients etc for those org types
debugger
client = Client.find_by_organisation_id(@organisation.id)
printer = Printer.find_by_organisation_id(@organisation.id)
if params[:organisation_type_ids].length > 0
params[:organisation_type_ids].each do |ot|
case ot.to_i
when 1:
if client.nil?
@organisation.create_client()
end
when 2:
if printer.nil?
@organisation.create_printer()
end
else
end
end
else #none are checked - so get rid of any that exist
if client
@organisation.client.destroy
end
if printer
@organisation.printer.destroy
end
end
#now get rid of any that have been unchecked
#ie client/printer exist but are not in params[:organisation_type_ids]
unless client.nil?
unless params[:organisation_type_ids].include?('1')
@organisation.client.destroy
end
end
unless printer.nil?
unless params[:organisation_type_ids].include?('2')
@organisation.printer.destroy
end
end
It seems the very opposite of DRY (is that wet?) and very, very kludgy. Sure it works, but I am sure that I am failing to understand the power of associations to do the heavy lifting for me.
Want to be able to control this through a form - using check boxes.
The models etc are:
class Client <>
belongs_to :organisation
class Printer <>
belongs_to :organisation
class Organisation <>
has_one :printer
has_one :client
But have had a nightmare sorting out the controller for handling all of this. I am sure there is a better way of doing this.
def update
#get the org type ids
params[:organisation_type_ids] ||= []
#check to se if we have clients etc for those org types
debugger
client = Client.find_by_organisation_id(@organisation.id)
printer = Printer.find_by_organisation_id(@organisation.id)
if params[:organisation_type_ids].length > 0
params[:organisation_type_ids].each do |ot|
case ot.to_i
when 1:
if client.nil?
@organisation.create_client()
end
when 2:
if printer.nil?
@organisation.create_printer()
end
else
end
end
else #none are checked - so get rid of any that exist
if client
@organisation.client.destroy
end
if printer
@organisation.printer.destroy
end
end
#now get rid of any that have been unchecked
#ie client/printer exist but are not in params[:organisation_type_ids]
unless client.nil?
unless params[:organisation_type_ids].include?('1')
@organisation.client.destroy
end
end
unless printer.nil?
unless params[:organisation_type_ids].include?('2')
@organisation.printer.destroy
end
end
It seems the very opposite of DRY (is that wet?) and very, very kludgy. Sure it works, but I am sure that I am failing to understand the power of associations to do the heavy lifting for me.
Tuesday, 18 August 2009
Why testing rocks - and ignore the last one
The polymorphic thing was a dead-end.
Cleared it all out - replaced it with a much more simple arrangement.
K.I.S.S. was never so true.
Love testing. make all the changes, run the test suite and ...
Started
..................................................................
Finished in 5.134175 seconds.
66 tests, 170 assertions, 0 failures, 0 errors
Cleared it all out - replaced it with a much more simple arrangement.
K.I.S.S. was never so true.
Love testing. make all the changes, run the test suite and ...
Started
..................................................................
Finished in 5.134175 seconds.
66 tests, 170 assertions, 0 failures, 0 errors
Polymorphic associations make my head hurt
Oh man.
I have an organisation class which has a number of "sub" classes - clients, printers, prospects, suppliers etc. An organisation can have many sub classes (ie an organisation could be simultaneously a client and a supplier for example).
Because clients, printers and prospects are quite different beasts they have to be different classes - not just different instances of the same class - hence the need to have a polymorphic relationship.
Tried to follow Ryan Bates's railscast but it wasn't really working for me.
After playing around with the console to get my head around what is really going on I came up with another way of sorting them out.
Have got the create method to work.
The way I came up with works on the basis that having denoted two classes to have a polymorphic relationship with each other, you can assign an instance of one to the other through the join (and that makes perfect sense ...)
What do I mean by that? Here are the models
My rather kludgy create model uses this:
The create_class method takes a string and returns an instance of that class:
I have a table of classes in the OrganisationType class - which allows me to add new ones dynamically which was my concern with doing everything hardwired - if I want a new sort of polymorphic thing, I can add one quite easily.
Now to sort out the update method, and to write some proper tests to make sure this sucker works in the way I hope it does.
I have an organisation class which has a number of "sub" classes - clients, printers, prospects, suppliers etc. An organisation can have many sub classes (ie an organisation could be simultaneously a client and a supplier for example).
Because clients, printers and prospects are quite different beasts they have to be different classes - not just different instances of the same class - hence the need to have a polymorphic relationship.
Tried to follow Ryan Bates's railscast but it wasn't really working for me.
After playing around with the console to get my head around what is really going on I came up with another way of sorting them out.
Have got the create method to work.
The way I came up with works on the basis that having denoted two classes to have a polymorphic relationship with each other, you can assign an instance of one to the other through the join (and that makes perfect sense ...)
What do I mean by that? Here are the models
class Organisation < ActiveRecord::Baseif I instantiate an organisation object, o, and a client object, c, then by doing o.resource = c, I get the join I am looking for (sets the resource_id and resource_type) correctly.
belongs_to :resource, :polymorphic => true
class Client < ActiveRecord::Base
has_one :organisation, :as => :resource
class Printer < ActiveRecord::Base
has_one :organisation, :as => :resource
My rather kludgy create model uses this:
params[:orgtypes].each do |ot|
@organisation = Organisation.new(params[:organisation])
resource = create_class(OrganisationType.find(ot).name)
resource.save!
@organisation.resource = resource
The create_class method takes a string and returns an instance of that class:
def create_class var
Object.const_get(var).new
end
I have a table of classes in the OrganisationType class - which allows me to add new ones dynamically which was my concern with doing everything hardwired - if I want a new sort of polymorphic thing, I can add one quite easily.
Now to sort out the update method, and to write some proper tests to make sure this sucker works in the way I hope it does.
Friday, 14 August 2009
a sight for sore eyes
Started
................................................................
Finished in 3.983101 seconds.
64 tests, 166 assertions, 0 failures, 0 errors
Unit tests with Factory Girl
Getting the HABTM working was a tricky thing. In the end I had to do this:
def setup
@job = Factory.create(:job)
@version = Factory.build(:version, :job_id =>@job.id)
@printer_set_up = Factory.build(:printer_set_up)
3.times {
pf = Factory.create(:printer_format)
@printer_set_up.printer_formats << page_assembly =" Factory.create(:page_assembly," printer_id =""> @printer_set_up.printer_id, :job_id => @job.id)
end
PrinterFormat and PrinterSetUp both have HABTM relationships with each other.
................................................................
Finished in 3.983101 seconds.
64 tests, 166 assertions, 0 failures, 0 errors
Unit tests with Factory Girl
Getting the HABTM working was a tricky thing. In the end I had to do this:
def setup
@job = Factory.create(:job)
@version = Factory.build(:version, :job_id =>@job.id)
@printer_set_up = Factory.build(:printer_set_up)
3.times {
pf = Factory.create(:printer_format)
@printer_set_up.printer_formats << page_assembly =" Factory.create(:page_assembly," printer_id =""> @printer_set_up.printer_id, :job_id => @job.id)
end
PrinterFormat and PrinterSetUp both have HABTM relationships with each other.
Subscribe to:
Posts (Atom)