Module: FactoryGirlExtensions
- Defined in:
- lib/factory_girl_extensions.rb
Overview
Adds helpful factory_girl methods to objects.
This uses method_missing just incase the given Object already responds to one of these methods.
Usage
User.generate! # this is equivalent to Factory(:user) or Factory.create(:user)
User.gen! # this is a shortcut alias for #generate
User.generate # this is equivalent to Factory.build(:user) and then #save
User.gen # this is equivalent to Factory.build(:user) and then #save
User.build # this is equivalent to Factory.build(:user)
User.gen! :name => 'Bob' # this is equivalent to Factory(:user, :name => 'Bob')
:email.next # this is equivalent to Factory.next(:email)
'email'.next # this will NOT work because String#next already exists
:admin_user.gen! # this is equivalent to Factory.gen(:admin_user)
'admin_user'.gen! # this is equivalent to Factory.gen(:admin_user)
User.attrs # this is equivalent to Factory.attributes_for(:user)
'user'.attrs # this is equivalent to Factory.attributes_for(:user)
:user.attrs # this is equivalent to Factory.attributes_for(:user)
TODO
-
properly implement
respond_to?
-
add syntax like User.gen_admin or User.gen(:admin) for generating an :admin_user
Instance Method Summary collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/factory_girl_extensions.rb', line 32 def method_missing name, *args = case name.to_s when /^gen(erate)?\!$/ [:build, :save!] when /^gen(erate)?$/ [:build, :save] when 'build' [:build] when 'next' [:next] when /^attr(ibute)?s(_for)?$/ [:attributes_for] end if # if this is an instance of String/Symbol use this instance as the factory name, else use the class name factory_name = ( kind_of?(Symbol) || kind_of?(String) ) ? self.to_s.to_sym : self.name.underscore.to_sym factory_method, instance_method = instance = Factory.send factory_method, factory_name, *args if instance_method instance.send instance_method if instance.respond_to? instance_method end instance else super end end |