Skip to content

Commit

Permalink
Calling blueprint on associations now correctly calls association spe…
Browse files Browse the repository at this point in the history
…cific callbacks
  • Loading branch information
andriusch committed Feb 9, 2011
1 parent 5c81a6c commit 5a082f9
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
11 changes: 10 additions & 1 deletion lib/blueprints/extensions.rb
Expand Up @@ -109,7 +109,16 @@ def blueprint_attribute(attribute, value)
end
end

ActiveRecord::Base.send(:include, Blueprints::Extensions::Saveable) if defined?(ActiveRecord)
if defined?(ActiveRecord)
ActiveRecord::Base.send(:include, Blueprints::Extensions::Saveable)
class ActiveRecord::Associations::AssociationProxy
include Blueprints::Extensions::Blueprintable::ClassMethods

def blueprint_object(attrs)
build.tap { |object| object.blueprint(attrs) }
end
end
end
Mongoid::Document.send(:include, Blueprints::Extensions::DynamicSaveable) if defined?(Mongoid)
MongoMapper::Document.send(:append_inclusions, Blueprints::Extensions::DynamicSaveable) if defined?(MongoMapper)
DataMapper::Model.send(:append_inclusions, Blueprints::Extensions::SoftSaveable) if defined?(DataMapper)
5 changes: 4 additions & 1 deletion spec/support/active_record/initializer.rb
Expand Up @@ -4,7 +4,10 @@ class Fruit < ActiveRecord::Base

class Tree < ActiveRecord::Base
attr_protected :size
has_many :fruits
has_many :fruits, :after_add => :fruit_after_add

def fruit_after_add(_)
end
end

db_config = YAML::load(Root.join("spec/support/active_record/database.yml").read)
Expand Down
24 changes: 22 additions & 2 deletions spec/unit/active_record_spec.rb
Expand Up @@ -3,10 +3,30 @@
require File.dirname(__FILE__) + '/../support/active_record/initializer'

describe ActiveRecord::Base do
subject do
Tree.blueprint(:name => 'tree')
end

it "should allow calling blueprint on associations" do
tree = Tree.blueprint(:name => 'tree')
fruit = tree.fruits.blueprint(:species => 'fruit')
fruit = subject.fruits.blueprint(:species => 'fruit')
fruit.should be_instance_of(Fruit)
fruit.species.should == 'fruit'
fruit.tree.should == subject
end

it "should call associations specific callbacks when calling blueprint on association" do
class Tree
def fruit_after_add(fruit)
fruit.average_diameter = -1
end
end

fruit = subject.fruits.blueprint(:species => 'fruit')
fruit.average_diameter.should == -1

class Tree
def fruit_after_add(fruit)
end
end
end
end

0 comments on commit 5a082f9

Please sign in to comment.