Skip to content
This repository
Browse code

Update README with more to_initialize info

  • Loading branch information...
commit ea89aad60a5f5a6131d8177c153719d96bafd766 1 parent 4aecfff
Joe Ferris authored

Showing 1 changed file with 17 additions and 4 deletions. Show diff stats Hide diff stats

  1. 21  GETTING_STARTED.md
21  GETTING_STARTED.md
Source Rendered
@@ -593,10 +593,10 @@ twenty_year_olds = FactoryGirl.build_list(:user, 25, :date_of_birth => 20.years.
593 593
 Custom Construction
594 594
 -------------------
595 595
 
596  
-Instantiating objects can be overridden in the case where you'd rather not
597  
-call `new` on your build class or you have some other factory method that
598  
-you'd prefer to use. Using custom construction also allows for your objects to
599  
-be instantiated with any number of arguments.
  596
+If you want to use factory_girl to construct an object where some attributes
  597
+are passed to `initialize` or if you want to do something other than simply
  598
+calling `new` on your build class, you can override the default behavior by
  599
+defining `to_initialize` on your factory. Example:
600 600
 
601 601
 ```ruby
602 602
 # user.rb
@@ -626,6 +626,19 @@ FactoryGirl.build(:user).name # Bob Hope
626 626
 Notice that I ignored the `name` attribute. If you don't want attributes
627 627
 reassigned after your object has been instantiated, you'll want to `ignore` them.
628 628
 
  629
+Although factory_girl is written to work with ActiveRecord out of the box, it
  630
+can also work with any Ruby class. For maximum compatibiltiy with ActiveRecord,
  631
+the default initializer builds all instances by calling new on your build class
  632
+without any arguments. It then calls attribute writer methods to assign all the
  633
+attribute values. While that works fine for ActiveRecord, it actually doesn't
  634
+work for almost any other Ruby class.
  635
+
  636
+You can override the initializer in order to:
  637
+
  638
+* Build non-ActiveRecord objects that require arguments to `initialize`
  639
+* Use a method other than `new` to instantiate the instance
  640
+* Do crazy things like decorate the instance after it's built
  641
+
629 642
 Cucumber Integration
630 643
 --------------------
631 644
 

0 notes on commit ea89aad

Please sign in to comment.
Something went wrong with that request. Please try again.