class FactoryGirl::Factory

Public Instance Methods

associations() click to toggle source
# File lib/factory_girl/factory.rb, line 54
def associations
  attributes.select {|attribute| attribute.association? }
end
compile() click to toggle source
# File lib/factory_girl/factory.rb, line 87
def compile
  parent.defined_traits.each {|trait| define_trait(trait) }
  parent.compile
  @definition.compile
end
factory_name() click to toggle source
# File lib/factory_girl/factory.rb, line 23
def factory_name
  $stderr.puts "DEPRECATION WARNING: factory.factory_name is deprecated; use factory.name instead."
  name
end
human_names() click to toggle source
# File lib/factory_girl/factory.rb, line 50
def human_names
  names.map {|name| name.to_s.humanize.downcase }
end
names() click to toggle source

Names for this factory, including aliases.

Example:

factory :user, :aliases => [:author] do
  # ...
end

FactoryGirl.create(:author).class
# => User

Because an attribute defined without a value or block will build an association with the same name, this allows associations to be defined without factories, such as:

factory :user, :aliases => [:author] do
  # ...
end

factory :post do
  author
end

FactoryGirl.create(:post).author.class
# => User
# File lib/factory_girl/factory.rb, line 83
def names
  [name] + @aliases
end
with_traits(traits) click to toggle source
# File lib/factory_girl/factory.rb, line 93
def with_traits(traits)
  self.clone.tap do |factory_with_traits|
    factory_with_traits.inherit_traits traits
  end
end

Protected Instance Methods

attributes() click to toggle source
# File lib/factory_girl/factory.rb, line 105
def attributes
  compile
  AttributeList.new(@name).tap do |list|
    traits.each do |trait|
      list.apply_attributes(trait.attributes)
    end

    list.apply_attributes(@definition.attributes)
    list.apply_attributes(parent.attributes)
  end
end
callbacks() click to toggle source
# File lib/factory_girl/factory.rb, line 117
def callbacks
  [parent.callbacks, traits.map(&:callbacks).reverse, @definition.callbacks].flatten
end

Private Instance Methods

assert_valid_options(options) click to toggle source
# File lib/factory_girl/factory.rb, line 123
def assert_valid_options(options)
  options.assert_valid_keys(:class, :parent, :default_strategy, :aliases, :traits)

  if options[:default_strategy]
    Proxy.ensure_strategy_exists!(options[:default_strategy])
    $stderr.puts "DEPRECATION WARNING: default_strategy is deprecated."
    $stderr.puts "Override to_create if you need to prevent a call to #save!."
  end
end
initialize_copy(source) click to toggle source
Calls superclass method
# File lib/factory_girl/factory.rb, line 141
def initialize_copy(source)
  super
  @definition = @definition.clone
end
parent() click to toggle source
# File lib/factory_girl/factory.rb, line 133
def parent
  if @parent
    FactoryGirl.factory_by_name(@parent)
  else
    NullFactory.new
  end
end