Module: FactoryGirl::Syntax::ObjectMethods

Defined in:
lib/factory_girl_extensions/core.rb

Overview

Extends any object to provide generation methods for factories.

Usage:

require 'factory_girl_extensions'

FactoryGirl.define do
  factory :user do
    name 'Bob Smith'
  end
end

# Creates a saved instance without raising (same as saving the result of FactoryGirl.build)
User.generate(:name => 'Johnny')

# Creates a saved instance and raises when invalid (same as FactoryGirl.create)
User.generate!

# Creates an unsaved instance (same as FactoryGirl.build)
User.build

# Creates an instance and yields it to the passed block
User.generate do |user|
  # ...do something with user...
end

# Creates and returns a Hash of attributes from this factory (same as FactoryGirl.attributes_for).
User.attributes

# A few short aliases are included for convenience.
User.gen
User.gen!
User.attrs

# Factories with custom prefix/suffixes are also supported.
FactoryGirl.define do
  factory :admin_user, :parent => :user do
    is_admin true
  end

  factory :user_with_profile, :parent => :user do
    profile_complete true
  end

  factory :admin_user_with_profile, :parent => :admin_user do
    profile_complete true
  end
end

# Generates the :admin_user factory
User.generate(:admin)
User.generate(:admin, :name => 'Cool Admin')

# Generates the :user_with_profile factory
User.generate(:with_profile)

# Generates the :admin_user_with_profile factory
User.generate(:admin, :with_profile)
User.generate(:admin, :with_profile, :name => 'Custom name')

# User.build and User.attributes also support these custom prefix/suffixes.

This syntax was derived from remi Taylor’s factory_girl_extensions.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.detect_factory(*possible_factory_names) ⇒ Object

Given any number of arguments representing the possible names for a factory that you want to find, this iterates over the given names and returns the first FactoryGirl factory object that FactoryGirl returns (skipping unregistered factory names).



134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/factory_girl_extensions/core.rb', line 134

def self.detect_factory(*possible_factory_names)
  factory = nil
  possible_factory_names.each do |factory_name|
    begin
      factory = FactoryGirl.factory_by_name(factory_name)
      break
    rescue ArgumentError => e
      raise e unless e.message =~ /Factory not registered/
      next
    end
  end
  factory
end

.factory_and_overrides(base_name, args) ⇒ Object

Helper to get the Factory object and overrides hash from the arguments passed to any ObjectMethods.

Raises:

  • (ArgumentError)


105
106
107
108
109
110
# File 'lib/factory_girl_extensions/core.rb', line 105

def self.factory_and_overrides(base_name, args)
  overrides = args.last.is_a?(Hash) ? args.pop : {}
  factory   = find_factory(base_name, args)
  raise ArgumentError.new("Could not find factory for #{base_name.inspect} with #{args.inspect}") unless factory
  [factory, overrides]
end

.find_factory(base_name, parts) ⇒ Object

Given the base_name for a class’s factory and 0-2 parts (prefix/suffixes), this finds the first matching factory that FactoryGirl has in its registry.



116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/factory_girl_extensions/core.rb', line 116

def self.find_factory(base_name, parts)
  case parts.length
  when 0
    detect_factory base_name
  when 1
    detect_factory "#{parts.first}_#{base_name}", "#{base_name}_#{parts.first}"
  when 2
    detect_factory "#{parts.first}_#{base_name}_#{parts.last}", "#{parts.last}_#{base_name}_#{parts.first}"
  else
    raise ArgumentError.new("Don't know how to find factory for #{base_name.inspect} with #{parts.inspect}")
  end
end

Instance Method Details

#attributes(*args, &block) ⇒ Object Also known as: attrs

Creates and returns a Hash of attributes from this factory (same as FactoryGirl.attributes_for)



92
93
94
95
96
# File 'lib/factory_girl_extensions/core.rb', line 92

def attributes(*args, &block)
  factory, overrides = ObjectMethods.factory_and_overrides(name.underscore, args)
  attrs = factory.run(Strategy::AttributesFor, overrides, &block)
  attrs
end

#build(*args, &block) ⇒ Object

Creates an unsaved instance (same as FactoryGirl.build)



70
71
72
73
74
# File 'lib/factory_girl_extensions/core.rb', line 70

def build(*args, &block)
  factory, overrides = ObjectMethods.factory_and_overrides(name.underscore, args)
  instance = factory.run(Strategy::Build, overrides, &block)
  instance
end

#generate(*args) {|instance| ... } ⇒ Object Also known as: gen

Creates a saved instance without raising (same as saving the result of FactoryGirl.build)

Yields:

  • (instance)


77
78
79
80
81
82
# File 'lib/factory_girl_extensions/core.rb', line 77

def generate(*args, &block)
  instance = build(*args)
  instance.save
  yield instance if block_given?
  instance
end

#generate!(*args, &block) ⇒ Object Also known as: gen!

Creates a saved instance and raises when invalid (same as FactoryGirl.create)



85
86
87
88
89
# File 'lib/factory_girl_extensions/core.rb', line 85

def generate!(*args, &block)
  factory, overrides = ObjectMethods.factory_and_overrides(name.underscore, args)
  instance = factory.run(Strategy::Create, overrides, &block)
  instance
end