Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

WIP - not sure if this should be scrapped or not

  • Loading branch information...
commit 04a3720e1761d8e5aede661cd59f004586de502d 1 parent 9e491bf
@joshuaclayton joshuaclayton authored
View
81 lib/factory_girl/definition.rb
@@ -17,13 +17,18 @@ def initialize(name = nil, base_traits = [])
@attributes = nil
@compiled = false
@base_module = Module.new
+ @base_attributes = Class.new do
+ def attributes
+ AttributeList.new
+ end
+ end
end
delegate :declare_attribute, to: :declarations
def attributes
@attributes ||= AttributeList.new.tap do |attribute_list|
- attribute_lists = aggregate_from_traits_and_self(:attributes) { declarations.attributes }
+ attribute_lists = base_attributes.new.attributes
attribute_lists.each do |attributes|
attribute_list.apply_attributes attributes
end
@@ -41,22 +46,44 @@ def to_create(&block)
def modules
compile
- mods = []
- base_traits.each do |trait|
- mods << trait.modules
- end
+ [].tap do |mods|
+ base_traits.each do |trait|
+ mods << trait.modules
+ end
- generate_constructor_module
- generate_callbacks_module
- generate_to_create_module
- mods << @base_module
+ mods << base_module
+
+ additional_traits.each do |trait|
+ mods << trait.modules
+ end
+ end.compact.flatten
+ end
- additional_traits.each do |trait|
- mods << trait.modules
+ def base_attributes
+ attribute_modules.each do |mod|
+ @base_attributes.send :include, mod
end
- mods.compact.flatten
+
+ @base_attributes
end
+ def attribute_modules
+ compile
+
+ [].tap do |mods|
+ base_traits.each do |trait|
+ mods << trait.attributes_module
+ end
+
+ mods << attributes_module
+
+ additional_traits.each do |trait|
+ mods << trait.attributes_module
+ end
+ end.compact.flatten
+ end
+
+
def compile
unless @compiled
declarations.attributes
@@ -70,6 +97,18 @@ def compile
end
end
+ def attributes_module
+ if declarations.attributes.any?
+ Module.new.tap do |mod|
+ attributes = declarations.attributes
+
+ mod.send :define_method, :attributes do
+ super() + attributes
+ end
+ end
+ end
+ end
+
def overridable
declarations.overridable
self
@@ -138,19 +177,11 @@ def initialize_copy(source)
@compiled = false
end
- def aggregate_from_traits_and_self(method_name, &block)
- compile
- [].tap do |list|
- base_traits.each do |trait|
- list << trait.send(method_name)
- end
-
- list << instance_exec(&block)
-
- additional_traits.each do |trait|
- list << trait.send(method_name)
- end
- end.flatten.compact
+ def base_module
+ generate_constructor_module
+ generate_callbacks_module
+ generate_to_create_module
+ @base_module
end
def generate_constructor_module
View
2  lib/factory_girl/trait.rb
@@ -13,7 +13,7 @@ def initialize(name, &block)
end
delegate :add_callback, :declare_attribute, :to_create, :define_trait, :constructor,
- :callbacks, :attributes, :modules, to: :@definition
+ :callbacks, :attributes, :modules, :attributes_module, to: :@definition
def names
[@name]
View
20 spec/acceptance/traits_spec.rb
@@ -725,3 +725,23 @@ def initialize(name)
expect(creator.name).to eq 'Joe Creator'
end
end
+
+describe "testing" do
+ before do
+ define_model("User", admin: :boolean)
+
+ FactoryGirl.define do
+ factory :user do
+ admin false
+
+ trait :admin do
+ admin true
+ end
+ end
+ end
+ end
+
+ it "works" do
+ FactoryGirl.create(:user, :admin)
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.