PROBLEM: will it be enough to have just before_save?
Other possible callbacks are after_build and after_create, but I
suspect they are not necessary.
I think it's good idea to avoid adding unnecessary features.
General possible workflow is following:
* new instance
* assign attributes
# after_build callback
* associate
# before_save callback - called when all associated but before any is
saved, order of instances or factories is not guaranteed
* save
# after_create callback - called when all instances are saved. order
is not guaranteed
DATA:
I looked through some callback use cases for factory girl:
http://groups.google.com/group/factory_girl/browse_thread/thread/6c37c59fa4d7ffc4
This case demonstrates need for before_save:
class Oui
belongs_to :phone
validates_something_of :value, :and => :phone
end
factory :oui do
before_save { model.value = f(model.phone) }
end
models { oui - phone }
http://stackoverflow.com/questions/1506556/has-many-while-respecting-build-strategy-in-factory-girl
This case demonstrates need for either after_build, or before_save,
additionally before_save should be called before any model is saved:
class Item
belongs_to :user
validates_presense_of :user_id
end
factory :user do
before_save { model.save! }
end
models { item - user }
http://groups.google.com/group/factory_girl/browse_thread/thread/f649d226bc4c0735/5ac1631fcd904081?lnk=gst&q=callback#5ac1631fcd904081
For this case any callback will work:
factory :verified_user, :class => User do
before_save do
u.register
u.verify
end
end
Max, I think for your case either before_save, or after_save would
work equally well:
factory :order do
before_save do
model.calculate_total_fee
end
end
class Order
def calculate_total_fee
unless @calculate_tatal_fee
@calculate_tatal_fee = true
calculate_fee
calculate_nested_orders_fee
end
end
end
Max, what do you think?