Class: FactoryGirl::Factory

Inherits:
Object
  • Object
show all
Defined in:
lib/factory_girl/factory.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ Factory

:nodoc:



35
36
37
38
39
40
41
42
43
44
# File 'lib/factory_girl/factory.rb', line 35

def initialize(name, options = {}) #:nodoc:
  assert_valid_options(options)
  @name                     = factory_name_for(name)
  @parent                   = options[:parent]
  @options                  = options
  @traits                   = []
  @children                 = []
  @attribute_list           = AttributeList.new
  @inherited_attribute_list = AttributeList.new
end

Instance Attribute Details

#nameObject (readonly)

:nodoc:



15
16
17
# File 'lib/factory_girl/factory.rb', line 15

def name
  @name
end

#traitsObject (readonly)

:nodoc:



16
17
18
# File 'lib/factory_girl/factory.rb', line 16

def traits
  @traits
end

Instance Method Details

#add_callback(name, &block) ⇒ Object



92
93
94
# File 'lib/factory_girl/factory.rb', line 92

def add_callback(name, &block)
  @attribute_list.add_callback(name, &block)
end

#add_child(factory) ⇒ Object



66
67
68
# File 'lib/factory_girl/factory.rb', line 66

def add_child(factory)
  @children << factory unless @children.include?(factory)
end

#allow_overridesObject



46
47
48
49
50
# File 'lib/factory_girl/factory.rb', line 46

def allow_overrides
  @attribute_list.overridable
  @inherited_attribute_list.overridable
  self
end

#allow_overrides?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/factory_girl/factory.rb', line 52

def allow_overrides?
  @attribute_list.overridable?
end

#apply_attributes(attributes_to_apply) ⇒ Object



76
77
78
# File 'lib/factory_girl/factory.rb', line 76

def apply_attributes(attributes_to_apply)
  @attribute_list.apply_attributes(attributes_to_apply)
end

#apply_traits(traits) ⇒ Object

:nodoc:



70
71
72
73
74
# File 'lib/factory_girl/factory.rb', line 70

def apply_traits(traits) #:nodoc:
  traits.reverse.map { |name| trait_by_name(name) }.each do |trait|
    apply_attributes(trait.attributes)
  end
end

#associationsObject



123
124
125
# File 'lib/factory_girl/factory.rb', line 123

def associations
  attributes.select {|attribute| attribute.association? }
end

#attributesObject



96
97
98
99
100
101
# File 'lib/factory_girl/factory.rb', line 96

def attributes
  AttributeList.new.tap do |list|
    list.apply_attributes(@attribute_list)
    list.apply_attributes(@inherited_attribute_list)
  end.to_a
end

#build_classObject

:nodoc:



27
28
29
# File 'lib/factory_girl/factory.rb', line 27

def build_class #:nodoc:
  @build_class ||= class_for(class_name)
end

#class_nameObject

:nodoc:



23
24
25
# File 'lib/factory_girl/factory.rb', line 23

def class_name #:nodoc:
  @options[:class] || name
end

#default_strategyObject

:nodoc:



31
32
33
# File 'lib/factory_girl/factory.rb', line 31

def default_strategy #:nodoc:
  @options[:default_strategy] || :create
end

#define_attribute(attribute) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/factory_girl/factory.rb', line 80

def define_attribute(attribute)
  if attribute.respond_to?(:factory) && attribute.factory == self.name
    raise AssociationDefinitionError, "Self-referencing association '#{attribute.name}' in factory '#{self.name}'"
  end

  @attribute_list.define_attribute(attribute).tap { update_children }
end

#define_trait(trait) ⇒ Object



88
89
90
# File 'lib/factory_girl/factory.rb', line 88

def define_trait(trait)
  @traits << trait
end

#factory_nameObject



18
19
20
21
# File 'lib/factory_girl/factory.rb', line 18

def factory_name
  puts "WARNING: factory.factory_name is deprecated. Use factory.name instead."
  name
end

#human_namesObject



119
120
121
# File 'lib/factory_girl/factory.rb', line 119

def human_names
  names.map {|name| name.to_s.gsub('_', ' ') }
end

#inherit_from(parent) ⇒ Object

:nodoc:



56
57
58
59
60
61
62
63
64
# File 'lib/factory_girl/factory.rb', line 56

def inherit_from(parent) #:nodoc:
  @options[:class]            ||= parent.class_name
  @options[:default_strategy] ||= parent.default_strategy

  allow_overrides if parent.allow_overrides?
  parent.add_child(self)

  @inherited_attribute_list.apply_attributes(parent.attributes)
end

#namesObject

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


162
163
164
# File 'lib/factory_girl/factory.rb', line 162

def names
  [name] + (@options[:aliases] || [])
end

#run(proxy_class, overrides) ⇒ Object

:nodoc:



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/factory_girl/factory.rb', line 103

def run(proxy_class, overrides) #:nodoc:
  proxy = proxy_class.new(build_class)
  overrides = symbolize_keys(overrides)

  attributes.each do |attribute|
    factory_overrides = overrides.select { |attr, val| attribute.aliases_for?(attr) }
    if factory_overrides.empty?
      attribute.add_to(proxy)
    else
      factory_overrides.each { |attr, val| proxy.set(attr, val, attribute.ignored); overrides.delete(attr) }
    end
  end
  overrides.each { |attr, val| proxy.set(attr, val) }
  proxy.result(@to_create_block)
end

#to_create(&block) ⇒ Object



166
167
168
# File 'lib/factory_girl/factory.rb', line 166

def to_create(&block)
  @to_create_block = block
end

#trait_by_name(name) ⇒ Object



127
128
129
130
131
132
133
134
135
# File 'lib/factory_girl/factory.rb', line 127

def trait_by_name(name)
  if existing_attribute = trait_for(name)
    existing_attribute
  elsif @parent
    FactoryGirl.factory_by_name(@parent).trait_by_name(name)
  else
    FactoryGirl.trait_by_name(name)
  end
end