1
0
Fork 0
mirror of https://github.com/thoughtbot/factory_bot.git synced 2022-11-09 11:43:51 -05:00

Implement requested implicit syntax

This commit is contained in:
Thomas Walpole 2011-08-10 20:33:50 -07:00 committed by Joshua Clayton
parent b9e314981d
commit f9217b3631
6 changed files with 38 additions and 5 deletions

View file

@ -12,6 +12,7 @@ require 'factory_girl/attribute/association'
require 'factory_girl/attribute/callback'
require 'factory_girl/attribute/sequence'
require 'factory_girl/attribute/implicit'
require 'factory_girl/attribute/attribute_group'
require 'factory_girl/sequence'
require 'factory_girl/attribute_group'
require 'factory_girl/aliases'

View file

@ -0,0 +1,20 @@
module FactoryGirl
class Attribute
class AttributeGroup < Attribute
def initialize(name, factory)
super(name)
@factory=factory
end
def add_to(proxy)
if @factory
@factory.attribute_group_by_name(name).attributes.each { |attr| attr.add_to(proxy) }
else
FactoryGirl.attribute_group_by_name(name).attributes.each { |attr| attr.add_to(proxy) }
end
end
end
end
end

View file

@ -2,8 +2,9 @@ module FactoryGirl
class Attribute
class Implicit < Attribute
def initialize(name)
def initialize(name, factory=nil)
super(name)
@factory=factory
end
def add_to(proxy)
@ -27,8 +28,10 @@ module FactoryGirl
def resolve_name
if FactoryGirl.factories.registered?(name)
Attribute::Association.new(name, name, {})
else
elsif FactoryGirl.sequences.registered?(name)
Attribute::Sequence.new(name, name)
else
Attribute::AttributeGroup.new(name, @factory)
end
end
end

View file

@ -77,7 +77,7 @@ module FactoryGirl
# are equivalent.
def method_missing(name, *args, &block)
if args.empty? && block.nil?
@factory.define_attribute(Attribute::Implicit.new(name))
@factory.define_attribute(Attribute::Implicit.new(name, @factory))
elsif args.first.is_a?(Hash) && args.first.has_key?(:factory)
association(name, *args)
else

View file

@ -6,6 +6,7 @@ describe "an instance generated by a factory with multiple attribute groups" do
define_model("User", :name => :string, :admin => :boolean, :gender => :string, :email => :string)
FactoryGirl.define do
factory :user do
name "John"
@ -23,7 +24,9 @@ describe "an instance generated by a factory with multiple attribute groups" do
gender "Female"
end
factory :male, :attribute_groups => [:male]
factory :male_user do
male
end
factory :female, :attribute_groups => [:female] do
attribute_group :admin do
admin true
@ -117,4 +120,10 @@ describe "an instance generated by a factory with multiple attribute groups" do
subject { FactoryGirl.create(:admin) }
it { should be_admin }
end
context "factory created with alternate syntax for specifying attribute group" do
subject { FactoryGirl.create(:male_user) }
its(:gender) { should == "Male" }
end
end

View file

@ -130,7 +130,7 @@ describe FactoryGirl::DefinitionProxy do
name = :user
attr = 'attribute'
stub(attr).name { name }
mock(FactoryGirl::Attribute::Implicit).new(name) { attr }
mock(FactoryGirl::Attribute::Implicit).new(name,factory) { attr }
subject.send(name)
factory.attributes.should include(attr)
end