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
|
@ -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
|
||||||
|
|
|
@ -8,13 +8,14 @@ module FactoryGirl
|
||||||
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) }
|
|
||||||
else
|
|
||||||
FactoryGirl.attribute_group_by_name(name).attributes.each { |attr| attr.add_to(proxy) }
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def attribute_group
|
||||||
|
(@factory || FactoryGirl).attribute_group_by_name(name)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -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
|
end
|
||||||
else
|
sort_attributes!
|
||||||
new_attributes << attribute.clone
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@attributes.unshift *new_attributes
|
|
||||||
end
|
|
||||||
@attributes = @attributes.partition{|attr| attr.priority.zero?}.flatten
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def define_attribute(attribute)
|
def define_attribute(attribute)
|
||||||
|
@ -125,10 +104,10 @@ 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
|
||||||
|
|
||||||
|
@ -232,6 +211,26 @@ module FactoryGirl
|
||||||
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
|
||||||
|
|
|
@ -3,9 +3,16 @@ 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
|
FactoryGirl.define do
|
||||||
|
factory :user_without_admin_scoping, :class => User do
|
||||||
|
admin_attribute_group
|
||||||
|
end
|
||||||
|
|
||||||
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"
|
||||||
|
@ -25,16 +36,20 @@ describe "an instance generated by a factory with multiple attribute groups" do
|
||||||
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]
|
||||||
|
@ -47,7 +62,6 @@ describe "an instance generated by a factory with multiple attribute groups" do
|
||||||
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
|
||||||
|
|
||||||
|
@ -89,7 +103,6 @@ describe "an instance generated by a factory with multiple attribute groups" do
|
||||||
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 }
|
||||||
|
@ -97,7 +110,6 @@ describe "an instance generated by a factory with multiple attribute groups" do
|
||||||
|
|
||||||
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 }
|
||||||
|
@ -122,4 +134,8 @@ describe "an instance generated by a factory with multiple attribute groups" do
|
||||||
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
|
||||||
|
|
Loading…
Reference in a new issue