mirror of
https://github.com/thoughtbot/factory_bot.git
synced 2022-11-09 11:43:51 -05:00
Add AttributeList#names to map all names for attributes in the list
This commit is contained in:
parent
a16e5eb22e
commit
b84ccd9d09
4 changed files with 54 additions and 11 deletions
|
@ -54,15 +54,15 @@ module FactoryGirl
|
||||||
end
|
end
|
||||||
|
|
||||||
def non_ignored_attribute_names
|
def non_ignored_attribute_names
|
||||||
@attribute_list.non_ignored.map(&:name)
|
@attribute_list.non_ignored.names
|
||||||
end
|
end
|
||||||
|
|
||||||
def ignored_attribute_names
|
def ignored_attribute_names
|
||||||
@attribute_list.ignored.map(&:name)
|
@attribute_list.ignored.names
|
||||||
end
|
end
|
||||||
|
|
||||||
def association_names
|
def association_names
|
||||||
@attribute_list.associations.map(&:name)
|
@attribute_list.associations.names
|
||||||
end
|
end
|
||||||
|
|
||||||
def override_names
|
def override_names
|
||||||
|
@ -70,7 +70,7 @@ module FactoryGirl
|
||||||
end
|
end
|
||||||
|
|
||||||
def hash_instance_methods_to_respond_to
|
def hash_instance_methods_to_respond_to
|
||||||
@attribute_list.map(&:name) + override_names + @build_class.instance_methods
|
@attribute_list.names + override_names + @build_class.instance_methods
|
||||||
end
|
end
|
||||||
|
|
||||||
def alias_names_to_ignore
|
def alias_names_to_ignore
|
||||||
|
|
|
@ -2,9 +2,9 @@ module FactoryGirl
|
||||||
class AttributeList
|
class AttributeList
|
||||||
include Enumerable
|
include Enumerable
|
||||||
|
|
||||||
def initialize(name = nil)
|
def initialize(name = nil, attributes = [])
|
||||||
@name = name
|
@name = name
|
||||||
@attributes = []
|
@attributes = attributes
|
||||||
end
|
end
|
||||||
|
|
||||||
def define_attribute(attribute)
|
def define_attribute(attribute)
|
||||||
|
@ -18,16 +18,20 @@ module FactoryGirl
|
||||||
@attributes.each(&block)
|
@attributes.each(&block)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def names
|
||||||
|
map(&:name)
|
||||||
|
end
|
||||||
|
|
||||||
def associations
|
def associations
|
||||||
@attributes.select(&:association?)
|
AttributeList.new(@name, select(&:association?))
|
||||||
end
|
end
|
||||||
|
|
||||||
def ignored
|
def ignored
|
||||||
select(&:ignored)
|
AttributeList.new(@name, select(&:ignored))
|
||||||
end
|
end
|
||||||
|
|
||||||
def non_ignored
|
def non_ignored
|
||||||
reject(&:ignored)
|
AttributeList.new(@name, reject(&:ignored))
|
||||||
end
|
end
|
||||||
|
|
||||||
def apply_attributes(attributes_to_apply)
|
def apply_attributes(attributes_to_apply)
|
||||||
|
|
|
@ -73,7 +73,7 @@ describe FactoryGirl::AttributeList, "#associations" do
|
||||||
end
|
end
|
||||||
|
|
||||||
it "returns associations" do
|
it "returns associations" do
|
||||||
subject.associations.should == [author_attribute, profile_attribute]
|
subject.associations.to_a.should == [author_attribute, profile_attribute]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -102,3 +102,42 @@ describe FactoryGirl::AttributeList, "filter based on ignored attributes" do
|
||||||
subject.non_ignored.map(&:name).should == [:email, :first_name, :last_name]
|
subject.non_ignored.map(&:name).should == [:email, :first_name, :last_name]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe FactoryGirl::AttributeList, "generating names" do
|
||||||
|
def build_ignored_attribute(name)
|
||||||
|
FactoryGirl::Attribute::Static.new(name, "value", true)
|
||||||
|
end
|
||||||
|
|
||||||
|
def build_non_ignored_attribute(name)
|
||||||
|
FactoryGirl::Attribute::Static.new(name, "value", false)
|
||||||
|
end
|
||||||
|
|
||||||
|
def build_association(name)
|
||||||
|
FactoryGirl::Attribute::Association.new(name, :user, {})
|
||||||
|
end
|
||||||
|
|
||||||
|
before do
|
||||||
|
subject.define_attribute(build_ignored_attribute(:comments_count))
|
||||||
|
subject.define_attribute(build_ignored_attribute(:posts_count))
|
||||||
|
subject.define_attribute(build_non_ignored_attribute(:email))
|
||||||
|
subject.define_attribute(build_non_ignored_attribute(:first_name))
|
||||||
|
subject.define_attribute(build_non_ignored_attribute(:last_name))
|
||||||
|
subject.define_attribute(build_association(:avatar))
|
||||||
|
end
|
||||||
|
|
||||||
|
it "knows all its #names" do
|
||||||
|
subject.names.should == [:comments_count, :posts_count, :email, :first_name, :last_name, :avatar]
|
||||||
|
end
|
||||||
|
|
||||||
|
it "knows all its #names for #ignored attributes" do
|
||||||
|
subject.ignored.names.should == [:comments_count, :posts_count]
|
||||||
|
end
|
||||||
|
|
||||||
|
it "knows all its #names for #non_ignored attributes" do
|
||||||
|
subject.non_ignored.names.should == [:email, :first_name, :last_name, :avatar]
|
||||||
|
end
|
||||||
|
|
||||||
|
it "knows all its #names for #associations" do
|
||||||
|
subject.associations.names.should == [:avatar]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
|
@ -37,7 +37,7 @@ describe FactoryGirl::Factory do
|
||||||
factory.associations.each do |association|
|
factory.associations.each do |association|
|
||||||
association.should be_association
|
association.should be_association
|
||||||
end
|
end
|
||||||
factory.associations.size.should == 3
|
factory.associations.to_a.length.should == 3
|
||||||
end
|
end
|
||||||
|
|
||||||
it "includes associations from the parent factory" do
|
it "includes associations from the parent factory" do
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue