Skip to content

Commit

Permalink
Maintain abstraction consistency within definition_proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuaclayton committed Oct 15, 2011
1 parent 9e88eb1 commit 6e9baa7
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 30 deletions.
5 changes: 5 additions & 0 deletions lib/factory_girl/callback.rb
Expand Up @@ -18,6 +18,11 @@ def run(instance, proxy)
end
end

def ==(other)
name == other.name &&
block == other.block
end

private

def check_name
Expand Down
6 changes: 3 additions & 3 deletions lib/factory_girl/definition_proxy.rb
Expand Up @@ -139,15 +139,15 @@ def association(name, options = {})
end

def after_build(&block)
@factory.add_callback(:after_build, &block)
@factory.add_callback(Callback.new(:after_build, block))
end

def after_create(&block)
@factory.add_callback(:after_create, &block)
@factory.add_callback(Callback.new(:after_create, block))
end

def after_stub(&block)
@factory.add_callback(:after_stub, &block)
@factory.add_callback(Callback.new(:after_stub, block))
end

def to_create(&block)
Expand Down
6 changes: 1 addition & 5 deletions lib/factory_girl/factory.rb
Expand Up @@ -19,7 +19,7 @@ def initialize(name, options = {}) #:nodoc:
@compiled = false
end

delegate :overridable?, :declarations, :declare_attribute, :to => :@attribute_list
delegate :overridable?, :declarations, :declare_attribute, :add_callback, :to => :@attribute_list

def factory_name
$stderr.puts "DEPRECATION WARNING: factory.factory_name is deprecated; use factory.name instead."
Expand All @@ -44,10 +44,6 @@ def define_trait(trait)
@defined_traits << trait
end

def add_callback(name, &block)
@attribute_list.add_callback(Callback.new(name, block))
end

def run(proxy_class, overrides, &block) #:nodoc:
ensure_compiled
proxy = proxy_class.new(build_class)
Expand Down
9 changes: 3 additions & 6 deletions spec/factory_girl/definition_proxy_spec.rb
Expand Up @@ -128,20 +128,17 @@

context "#after_build" do
before { proxy.after_build(&callback) }

its(:build_callbacks) { should include(callback) }
it { should have_callback(:after_build).with_block(callback) }
end

context "#after_create" do
before { proxy.after_create(&callback) }

its(:create_callbacks) { should include(callback) }
it { should have_callback(:after_create).with_block(callback) }
end

context "#after_stub" do
before { proxy.after_stub(&callback) }

its(:stub_callbacks) { should include(callback) }
it { should have_callback(:after_stub).with_block(callback) }
end
end

Expand Down
9 changes: 9 additions & 0 deletions spec/support/matchers/callback.rb
@@ -0,0 +1,9 @@
RSpec::Matchers.define :have_callback do |callback_name|
match do |instance|
instance.callbacks.include?(FactoryGirl::Callback.new(callback_name, @block))
end

chain :with_block do |block|
@block = block
end
end
20 changes: 4 additions & 16 deletions spec/support/mock_factory.rb
@@ -1,10 +1,10 @@
class MockFactory
attr_reader :declarations, :traits
attr_reader :declarations, :traits, :callbacks

def initialize
@declarations = []
@traits = []
@callbacks = Hash.new([])
@callbacks = []
@to_create = nil
end

Expand All @@ -20,20 +20,8 @@ def declare_attribute(declaration)
@declarations << declaration
end

def build_callbacks
@callbacks[:after_build]
end

def create_callbacks
@callbacks[:after_create]
end

def stub_callbacks
@callbacks[:after_stub]
end

def add_callback(callback_type, &callback)
@callbacks[callback_type] << callback
def add_callback(callback)
@callbacks << callback
end

def define_trait(trait)
Expand Down

0 comments on commit 6e9baa7

Please sign in to comment.