Skip to content

Commit

Permalink
Moved aliases out of the definition body into the definition arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
jferris committed Nov 11, 2010
1 parent 3b5f90f commit ef29139
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 44 deletions.
33 changes: 0 additions & 33 deletions lib/factory_girl/definition_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,39 +132,6 @@ def association(name, options = {})
@factory.define_attribute(Attribute::Association.new(name, factory_name, options))
end

# Registers an alias for this factory using the given name.
#
# Arguments:
# * name: +Symbol+
# The name of the alias.
#
# Example:
#
# factory :user do
# aliased_as :author
# end
#
# Factory(: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 do
# aliased_as :author
# end
#
# factory :post do
# author
# end
#
# Factory(:post).author.class
# # => User
def aliased_as(name)
FactoryGirl.register_factory(@factory, :as => name)
end

def after_build(&block)
@factory.add_callback(:after_build, &block)
end
Expand Down
42 changes: 40 additions & 2 deletions lib/factory_girl/factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,19 @@ def self.factory_by_name(name)
end

def self.register_factory(factory, options = {})
name = options[:as] || factory.name
if options[:as]
name = options[:as]
else
name = factory.name
factory.aliases.each do |alias_name|
register_factory(factory, :as => alias_name)
end
end

if self.factories[name]
raise DuplicateDefinitionError, "Factory already defined: #{name}"
end

self.factories[name] = factory
end

Expand Down Expand Up @@ -110,6 +119,35 @@ def associations
attributes.select {|attribute| attribute.is_a?(Attribute::Association) }
end

# Alternate names for this factory.
#
# Example:
#
# factory :user, :aliases => [:author] do
# # ...
# end
#
# Factory(: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
#
# Factory(:post).author.class
# # => User
def aliases
@options[:aliases] || []
end

private

def class_for (class_or_to_s)
Expand All @@ -136,7 +174,7 @@ def attribute_defined? (name)
end

def assert_valid_options(options)
invalid_keys = options.keys - [:class, :parent, :default_strategy]
invalid_keys = options.keys - [:class, :parent, :default_strategy, :aliases]
unless invalid_keys == []
raise ArgumentError, "Unknown arguments: #{invalid_keys.inspect}"
end
Expand Down
4 changes: 1 addition & 3 deletions spec/acceptance/acceptance_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@
"somebody#{n}@example.com"
end

factory :user, :class => 'user' do
factory :user, :class => 'user', :aliases => [:author] do
first_name 'Jimi'
last_name 'Hendrix'
admin false
email { "#{first_name}.#{last_name}@example.com".downcase }

aliased_as :author
end

factory Post, :default_strategy => :attributes_for do
Expand Down
1 change: 1 addition & 0 deletions spec/acceptance/syntax/vintage_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
@factory = "factory"
@proxy = "proxy"
stub(@factory).name { @name }
stub(@factory).aliases { [] }
@options = { :class => 'magic' }
stub(FactoryGirl::Factory).new { @factory }
stub(FactoryGirl::DefinitionProxy).new { @proxy }
Expand Down
6 changes: 0 additions & 6 deletions spec/factory_girl/definition_proxy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,4 @@
factory.attributes.last.add_to(proxy)
proxy.should have_received.set(name, 'expected')
end

it "registers its factory for an alias" do
aliased_name = :guest
mock(FactoryGirl).register_factory(factory, :as => aliased_name)
subject.aliased_as aliased_name
end
end
17 changes: 17 additions & 0 deletions spec/factory_girl/factory_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
before do
@name = :user
@factory = "factory"
@aliases = [:one, :two]
stub(@factory).name { @name }
stub(@factory).aliases { @aliases }
end

it "should add the factory to the list of factories" do
Expand All @@ -17,6 +19,13 @@
2.times { FactoryGirl.register_factory(@factory) }
}.should raise_error(FactoryGirl::DuplicateDefinitionError)
end

it "registers aliases" do
FactoryGirl.register_factory(@factory)
@aliases.each do |name|
FactoryGirl.factory_by_name(name).should == @factory
end
end
end

describe FactoryGirl::Factory do
Expand Down Expand Up @@ -392,3 +401,11 @@
end
end


describe FactoryGirl::Factory, "with aliases" do
it "registers the aliases" do
aliased_name = :guest
factory = FactoryGirl::Factory.new("user", :aliases => [aliased_name])
factory.aliases.should == [aliased_name]
end
end

0 comments on commit ef29139

Please sign in to comment.