Module: FactoryGirlExtensions
- Defined in:
- lib/factory_girl_extensions/module.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
Class Method Summary collapse
-
.__method_missing(object, name, *args, &block) ⇒ Object
This is the actual (private) implementation.
Instance Method Summary collapse
-
#method_missing(name, *args, &block) ⇒ Object
This is the public implementation.
-
#underscore_string(string) ⇒ Object
I was relying on String#underscore being available and it’s not always.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
This is the public implementation. When you include FactoryGirlExtensions into a class, this is what gives you the #gen and other methods.
80 81 82 83 84 85 86 87 88 89 |
# File 'lib/factory_girl_extensions/module.rb', line 80 def method_missing name, *args, &block object = FactoryGirlExtensions.__method_missing(self, name, *args, &block) # If no extensions were found, call super if object == :no_extension_found super else object end end |
Class Method Details
.__method_missing(object, name, *args, &block) ⇒ Object
This is the actual (private) implementation
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/factory_girl_extensions/module.rb', line 43 def self.__method_missing object, name, *args, &block raise 'Factory is not defined. Have you required factory_girl?' unless defined? Factory = 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 = ( object.kind_of?(Symbol) || object.kind_of?(String) ) ? object.to_s.to_sym : underscore_string(object.name).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 :no_extension_found end end |
Instance Method Details
#underscore_string(string) ⇒ Object
I was relying on String#underscore being available and it’s not always. Copies this from ActiveSupport.
34 35 36 37 38 39 40 |
# File 'lib/factory_girl_extensions/module.rb', line 34 def underscore_string string string.to_s.gsub(/::/, '/'). gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2'). gsub(/([a-z\d])([A-Z])/,'\1_\2'). tr("-", "_"). downcase end |