Skip to content

Commit

Permalink
WIP - not sure if this should be scrapped or not
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuaclayton committed Sep 20, 2013
1 parent 9e491bf commit 04a3720
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 26 deletions.
81 changes: 56 additions & 25 deletions lib/factory_girl/definition.rb
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/factory_girl/trait.rb
Expand Up @@ -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]
Expand Down
20 changes: 20 additions & 0 deletions spec/acceptance/traits_spec.rb
Expand Up @@ -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

0 comments on commit 04a3720

Please sign in to comment.