mirror of
https://github.com/thoughtbot/factory_bot.git
synced 2022-11-09 11:43:51 -05:00
WIP: initial cleanup
This commit is contained in:
parent
23ba0eb541
commit
a78bc2a0b9
7 changed files with 98 additions and 83 deletions
|
@ -52,7 +52,7 @@ module FactoryGirl
|
||||||
def self.sequence_by_name(name)
|
def self.sequence_by_name(name)
|
||||||
sequences.find(name)
|
sequences.find(name)
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.attribute_groups
|
def self.attribute_groups
|
||||||
@attribute_groups ||= Registry.new
|
@attribute_groups ||= Registry.new
|
||||||
end
|
end
|
||||||
|
@ -64,5 +64,4 @@ module FactoryGirl
|
||||||
def self.attribute_group_by_name(name)
|
def self.attribute_group_by_name(name)
|
||||||
attribute_groups.find(name)
|
attribute_groups.find(name)
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -4,17 +4,18 @@ module FactoryGirl
|
||||||
class AttributeGroup < Attribute
|
class AttributeGroup < Attribute
|
||||||
def initialize(name, factory)
|
def initialize(name, factory)
|
||||||
super(name)
|
super(name)
|
||||||
@factory=factory
|
@factory = factory
|
||||||
end
|
end
|
||||||
|
|
||||||
def add_to(proxy)
|
def add_to(proxy)
|
||||||
if @factory
|
attribute_group.attributes.each { |attr| attr.add_to(proxy) }
|
||||||
@factory.attribute_group_by_name(name).attributes.each { |attr| attr.add_to(proxy) }
|
end
|
||||||
else
|
|
||||||
FactoryGirl.attribute_group_by_name(name).attributes.each { |attr| attr.add_to(proxy) }
|
private
|
||||||
end
|
|
||||||
|
def attribute_group
|
||||||
|
(@factory || FactoryGirl).attribute_group_by_name(name)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -2,9 +2,9 @@ module FactoryGirl
|
||||||
class Attribute
|
class Attribute
|
||||||
|
|
||||||
class Implicit < Attribute
|
class Implicit < Attribute
|
||||||
def initialize(name, factory=nil)
|
def initialize(name, factory = nil)
|
||||||
super(name)
|
super(name)
|
||||||
@factory=factory
|
@factory = factory
|
||||||
end
|
end
|
||||||
|
|
||||||
def add_to(proxy)
|
def add_to(proxy)
|
||||||
|
|
|
@ -14,7 +14,7 @@ module FactoryGirl
|
||||||
class Factory
|
class Factory
|
||||||
attr_reader :name #:nodoc:
|
attr_reader :name #:nodoc:
|
||||||
attr_reader :attributes #:nodoc:
|
attr_reader :attributes #:nodoc:
|
||||||
|
|
||||||
def factory_name
|
def factory_name
|
||||||
puts "WARNING: factory.factory_name is deprecated. Use factory.name instead."
|
puts "WARNING: factory.factory_name is deprecated. Use factory.name instead."
|
||||||
name
|
name
|
||||||
|
@ -44,36 +44,15 @@ module FactoryGirl
|
||||||
@options[:class] ||= parent.class_name
|
@options[:class] ||= parent.class_name
|
||||||
@options[:default_strategy] ||= parent.default_strategy
|
@options[:default_strategy] ||= parent.default_strategy
|
||||||
|
|
||||||
new_attributes = []
|
apply_attributes(parent.attributes)
|
||||||
parent.attributes.each do |attribute|
|
sort_attributes!
|
||||||
if attribute_defined?(attribute.name)
|
|
||||||
@attributes.delete_if do |attrib|
|
|
||||||
new_attributes << attrib.clone if attrib.name == attribute.name
|
|
||||||
end
|
|
||||||
else
|
|
||||||
new_attributes << attribute.clone
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
@attributes.unshift *new_attributes
|
|
||||||
@attributes = @attributes.partition{|attr| attr.priority.zero? }.flatten
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def apply_attribute_groups(groups)
|
def apply_attribute_groups(groups)
|
||||||
groups.reverse.map{ |name| attribute_group_by_name(name) }.each do |group|
|
groups.reverse.map { |name| attribute_group_by_name(name) }.each do |group|
|
||||||
new_attributes=[]
|
apply_attributes(group.attributes)
|
||||||
group.attributes.each do |attribute|
|
|
||||||
if attribute_defined?(attribute.name)
|
|
||||||
@attributes.delete_if do |attrib|
|
|
||||||
new_attributes << attrib.clone if attrib.name == attribute.name
|
|
||||||
end
|
|
||||||
else
|
|
||||||
new_attributes << attribute.clone
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@attributes.unshift *new_attributes
|
|
||||||
end
|
end
|
||||||
@attributes = @attributes.partition{|attr| attr.priority.zero?}.flatten
|
sort_attributes!
|
||||||
end
|
end
|
||||||
|
|
||||||
def define_attribute(attribute)
|
def define_attribute(attribute)
|
||||||
|
@ -87,7 +66,7 @@ module FactoryGirl
|
||||||
end
|
end
|
||||||
@attributes << attribute
|
@attributes << attribute
|
||||||
end
|
end
|
||||||
|
|
||||||
def define_attribute_group(group)
|
def define_attribute_group(group)
|
||||||
attribute_groups.add group
|
attribute_groups.add group
|
||||||
end
|
end
|
||||||
|
@ -124,14 +103,14 @@ module FactoryGirl
|
||||||
|
|
||||||
def attribute_group_by_name(name)
|
def attribute_group_by_name(name)
|
||||||
return attribute_groups.find(name) if attribute_groups.registered?(name)
|
return attribute_groups.find(name) if attribute_groups.registered?(name)
|
||||||
|
|
||||||
if @parent.nil?
|
if @parent
|
||||||
FactoryGirl::attribute_group_by_name(name)
|
|
||||||
else
|
|
||||||
FactoryGirl.factory_by_name(@parent).attribute_group_by_name(name)
|
FactoryGirl.factory_by_name(@parent).attribute_group_by_name(name)
|
||||||
|
else
|
||||||
|
FactoryGirl.attribute_group_by_name(name)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Names for this factory, including aliases.
|
# Names for this factory, including aliases.
|
||||||
#
|
#
|
||||||
# Example:
|
# Example:
|
||||||
|
@ -231,7 +210,27 @@ module FactoryGirl
|
||||||
options
|
options
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def apply_attributes(attributes_to_apply)
|
||||||
|
new_attributes=[]
|
||||||
|
|
||||||
|
attributes_to_apply.each do |attribute|
|
||||||
|
if attribute_defined?(attribute.name)
|
||||||
|
@attributes.delete_if do |attrib|
|
||||||
|
new_attributes << attrib.clone if attrib.name == attribute.name
|
||||||
|
end
|
||||||
|
else
|
||||||
|
new_attributes << attribute.clone
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
@attributes.unshift *new_attributes
|
||||||
|
end
|
||||||
|
|
||||||
|
def sort_attributes!
|
||||||
|
@attributes = @attributes.partition {|attr| attr.priority.zero? }.flatten
|
||||||
|
end
|
||||||
|
|
||||||
def attribute_groups
|
def attribute_groups
|
||||||
@attribute_groups ||= Registry.new
|
@attribute_groups ||= Registry.new
|
||||||
end
|
end
|
||||||
|
|
|
@ -30,11 +30,11 @@ module FactoryGirl
|
||||||
factory(child_name, child_options.merge(:parent => name), &child_block)
|
factory(child_name, child_options.merge(:parent => name), &child_block)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def sequence(name, start_value = 1, &block)
|
def sequence(name, start_value = 1, &block)
|
||||||
FactoryGirl.register_sequence(Sequence.new(name, start_value, &block))
|
FactoryGirl.register_sequence(Sequence.new(name, start_value, &block))
|
||||||
end
|
end
|
||||||
|
|
||||||
def attribute_group(name, &block)
|
def attribute_group(name, &block)
|
||||||
FactoryGirl.register_attribute_group(AttributeGroup.new(name, &block))
|
FactoryGirl.register_attribute_group(AttributeGroup.new(name, &block))
|
||||||
end
|
end
|
||||||
|
|
|
@ -3,10 +3,17 @@ require "acceptance/acceptance_helper"
|
||||||
|
|
||||||
describe "an instance generated by a factory with multiple attribute groups" do
|
describe "an instance generated by a factory with multiple attribute groups" do
|
||||||
before do
|
before do
|
||||||
define_model("User", :name => :string, :admin => :boolean, :gender => :string, :email => :string)
|
define_model("User",
|
||||||
|
:name => :string,
|
||||||
|
:admin => :boolean,
|
||||||
|
:gender => :string,
|
||||||
|
:email => :string)
|
||||||
|
|
||||||
|
FactoryGirl.define do
|
||||||
|
factory :user_without_admin_scoping, :class => User do
|
||||||
|
admin_attribute_group
|
||||||
|
end
|
||||||
|
|
||||||
FactoryGirl.define do
|
|
||||||
|
|
||||||
factory :user do
|
factory :user do
|
||||||
name "John"
|
name "John"
|
||||||
|
|
||||||
|
@ -14,6 +21,10 @@ describe "an instance generated by a factory with multiple attribute groups" do
|
||||||
admin true
|
admin true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
attribute_group :admin_attribute_group do
|
||||||
|
admin true
|
||||||
|
end
|
||||||
|
|
||||||
attribute_group :male do
|
attribute_group :male do
|
||||||
name "Joe"
|
name "Joe"
|
||||||
gender "Male"
|
gender "Male"
|
||||||
|
@ -24,30 +35,33 @@ describe "an instance generated by a factory with multiple attribute groups" do
|
||||||
gender "Female"
|
gender "Female"
|
||||||
end
|
end
|
||||||
|
|
||||||
factory :admin, :attribute_groups=>[:admin]
|
factory :admin, :attribute_groups => [:admin]
|
||||||
|
|
||||||
factory :male_user do
|
factory :male_user do
|
||||||
male
|
male
|
||||||
end
|
end
|
||||||
|
|
||||||
factory :female, :attribute_groups => [:female] do
|
factory :female, :attribute_groups => [:female] do
|
||||||
attribute_group :admin do
|
attribute_group :admin do
|
||||||
admin true
|
admin true
|
||||||
name "Judy"
|
name "Judy"
|
||||||
end
|
end
|
||||||
factory :female_admin_judy, :attribute_groups=>[:admin]
|
|
||||||
|
factory :female_admin_judy, :attribute_groups => [:admin]
|
||||||
end
|
end
|
||||||
factory :female_admin, :attribute_groups => [:female, :admin]
|
|
||||||
|
factory :female_admin, :attribute_groups => [:female, :admin]
|
||||||
factory :female_after_male_admin, :attribute_groups => [:male, :female, :admin]
|
factory :female_after_male_admin, :attribute_groups => [:male, :female, :admin]
|
||||||
factory :male_after_female_admin, :attribute_groups => [:female, :male, :admin]
|
factory :male_after_female_admin, :attribute_groups => [:female, :male, :admin]
|
||||||
end
|
end
|
||||||
|
|
||||||
attribute_group :email do
|
attribute_group :email do
|
||||||
email { "#{name}@example.com" }
|
email { "#{name}@example.com" }
|
||||||
end
|
end
|
||||||
|
|
||||||
factory :user_with_email, :class=>User, :attribute_groups=>[:email] do
|
factory :user_with_email, :class => User, :attribute_groups => [:email] do
|
||||||
name "Bill"
|
name "Bill"
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -55,71 +69,73 @@ describe "an instance generated by a factory with multiple attribute groups" do
|
||||||
subject { FactoryGirl.create(:user) }
|
subject { FactoryGirl.create(:user) }
|
||||||
its(:name) { should == "John" }
|
its(:name) { should == "John" }
|
||||||
its(:gender) { should be_nil }
|
its(:gender) { should be_nil }
|
||||||
it { should_not be_admin }
|
it { should_not be_admin }
|
||||||
end
|
end
|
||||||
|
|
||||||
context "the child class with one attribute group" do
|
context "the child class with one attribute group" do
|
||||||
subject { FactoryGirl.create(:admin) }
|
subject { FactoryGirl.create(:admin) }
|
||||||
its(:name) { should == "John" }
|
its(:name) { should == "John" }
|
||||||
its(:gender) { should be_nil }
|
its(:gender) { should be_nil }
|
||||||
it { should be_admin }
|
it { should be_admin }
|
||||||
end
|
end
|
||||||
|
|
||||||
context "the other child class with one attribute group" do
|
context "the other child class with one attribute group" do
|
||||||
subject { FactoryGirl.create(:female) }
|
subject { FactoryGirl.create(:female) }
|
||||||
its(:name) { should == "Jane" }
|
its(:name) { should == "Jane" }
|
||||||
its(:gender) { should == "Female" }
|
its(:gender) { should == "Female" }
|
||||||
it { should_not be_admin }
|
it { should_not be_admin }
|
||||||
end
|
end
|
||||||
|
|
||||||
context "the child with multiple attribute groups" do
|
context "the child with multiple attribute groups" do
|
||||||
subject { FactoryGirl.create(:female_admin) }
|
subject { FactoryGirl.create(:female_admin) }
|
||||||
its(:name) { should == "Jane" }
|
its(:name) { should == "Jane" }
|
||||||
its(:gender) { should == "Female" }
|
its(:gender) { should == "Female" }
|
||||||
it { should be_admin }
|
it { should be_admin }
|
||||||
end
|
end
|
||||||
|
|
||||||
context "the child with multiple attribute groups and overridden attributes" do
|
context "the child with multiple attribute groups and overridden attributes" do
|
||||||
subject { FactoryGirl.create(:female_admin, :name => "Jill", :gender => nil) }
|
subject { FactoryGirl.create(:female_admin, :name => "Jill", :gender => nil) }
|
||||||
its(:name) { should == "Jill" }
|
its(:name) { should == "Jill" }
|
||||||
its(:gender) { should be_nil }
|
its(:gender) { should be_nil }
|
||||||
it { should be_admin }
|
it { should be_admin }
|
||||||
end
|
end
|
||||||
|
|
||||||
context "the child with multiple attribute groups who override the same attribute" do
|
context "the child with multiple attribute groups who override the same attribute" do
|
||||||
context "when the male assigns name after female" do
|
context "when the male assigns name after female" do
|
||||||
subject { FactoryGirl.create(:male_after_female_admin) }
|
subject { FactoryGirl.create(:male_after_female_admin) }
|
||||||
|
|
||||||
its(:name) { should == "Joe" }
|
its(:name) { should == "Joe" }
|
||||||
its(:gender) { should == "Male" }
|
its(:gender) { should == "Male" }
|
||||||
it { should be_admin }
|
it { should be_admin }
|
||||||
end
|
end
|
||||||
|
|
||||||
context "when the female assigns name after male" do
|
context "when the female assigns name after male" do
|
||||||
subject { FactoryGirl.create(:female_after_male_admin) }
|
subject { FactoryGirl.create(:female_after_male_admin) }
|
||||||
|
|
||||||
its(:name) { should == "Jane" }
|
its(:name) { should == "Jane" }
|
||||||
its(:gender) { should == "Female" }
|
its(:gender) { should == "Female" }
|
||||||
it { should be_admin }
|
it { should be_admin }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context "child class with scoped attribute group and inherited attribute group" do
|
context "child class with scoped attribute group and inherited attribute group" do
|
||||||
subject { FactoryGirl.create(:female_admin_judy) }
|
subject { FactoryGirl.create(:female_admin_judy) }
|
||||||
its(:name) { should == "Judy" }
|
its(:name) { should == "Judy" }
|
||||||
its(:gender) { should == "Female" }
|
its(:gender) { should == "Female" }
|
||||||
it { should be_admin }
|
it { should be_admin }
|
||||||
end
|
end
|
||||||
|
|
||||||
context "factory using global attribute group" do
|
context "factory using global attribute group" do
|
||||||
subject { FactoryGirl.create(:user_with_email) }
|
subject { FactoryGirl.create(:user_with_email) }
|
||||||
its(:name) { should == "Bill" }
|
its(:name) { should == "Bill" }
|
||||||
its(:email) { should == "Bill@example.com"}
|
its(:email) { should == "Bill@example.com"}
|
||||||
end
|
end
|
||||||
|
|
||||||
context "factory created with alternate syntax for specifying attribute group" do
|
context "factory created with alternate syntax for specifying attribute group" do
|
||||||
subject { FactoryGirl.create(:male_user) }
|
subject { FactoryGirl.create(:male_user) }
|
||||||
its(:gender) { should == "Male" }
|
its(:gender) { should == "Male" }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context "factory outside of scope" do
|
||||||
|
subject { FactoryGirl.create(:user_without_admin_scoping) }
|
||||||
|
it { expect { subject }.to raise_error(ArgumentError, "Not registered: admin_attribute_group") }
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -130,7 +130,7 @@ describe FactoryGirl::DefinitionProxy do
|
||||||
name = :user
|
name = :user
|
||||||
attr = 'attribute'
|
attr = 'attribute'
|
||||||
stub(attr).name { name }
|
stub(attr).name { name }
|
||||||
mock(FactoryGirl::Attribute::Implicit).new(name,factory) { attr }
|
mock(FactoryGirl::Attribute::Implicit).new(name, factory) { attr }
|
||||||
subject.send(name)
|
subject.send(name)
|
||||||
factory.attributes.should include(attr)
|
factory.attributes.should include(attr)
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue