1
0
Fork 0
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:
Joshua Clayton 2012-04-24 16:40:29 -05:00
parent a16e5eb22e
commit b84ccd9d09
4 changed files with 54 additions and 11 deletions

View file

@ -54,15 +54,15 @@ module FactoryGirl
end
def non_ignored_attribute_names
@attribute_list.non_ignored.map(&:name)
@attribute_list.non_ignored.names
end
def ignored_attribute_names
@attribute_list.ignored.map(&:name)
@attribute_list.ignored.names
end
def association_names
@attribute_list.associations.map(&:name)
@attribute_list.associations.names
end
def override_names
@ -70,7 +70,7 @@ module FactoryGirl
end
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
def alias_names_to_ignore

View file

@ -2,9 +2,9 @@ module FactoryGirl
class AttributeList
include Enumerable
def initialize(name = nil)
def initialize(name = nil, attributes = [])
@name = name
@attributes = []
@attributes = attributes
end
def define_attribute(attribute)
@ -18,16 +18,20 @@ module FactoryGirl
@attributes.each(&block)
end
def names
map(&:name)
end
def associations
@attributes.select(&:association?)
AttributeList.new(@name, select(&:association?))
end
def ignored
select(&:ignored)
AttributeList.new(@name, select(&:ignored))
end
def non_ignored
reject(&:ignored)
AttributeList.new(@name, reject(&:ignored))
end
def apply_attributes(attributes_to_apply)

View file

@ -73,7 +73,7 @@ describe FactoryGirl::AttributeList, "#associations" do
end
it "returns associations" do
subject.associations.should == [author_attribute, profile_attribute]
subject.associations.to_a.should == [author_attribute, profile_attribute]
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]
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

View file

@ -37,7 +37,7 @@ describe FactoryGirl::Factory do
factory.associations.each do |association|
association.should be_association
end
factory.associations.size.should == 3
factory.associations.to_a.length.should == 3
end
it "includes associations from the parent factory" do