Permalink
Browse files

allow HATBM associations to be assigned values in blueprint

  • Loading branch information...
1 parent f44b674 commit f31cd75cc9222d5579f5f8d336b049ef27b3f428 @adzap adzap committed with Feb 26, 2009
Showing with 21 additions and 1 deletion.
  1. +1 −1 lib/machinist.rb
  2. +9 −0 spec/db/schema.rb
  3. +10 −0 spec/machinist_spec.rb
  4. +1 −0 spec/spec_helper.rb
View
@@ -44,7 +44,7 @@ def object
def method_missing(symbol, *args, &block)
if @assigned_attributes.has_key?(symbol)
@object.send(symbol)
- elsif @object.class.reflect_on_association(symbol) && !@object.send(symbol).nil?
+ elsif @object.class.reflect_on_association(symbol) && !@object.send(symbol).blank?
@object.send(symbol)
else
@object.send("#{symbol}=", generate_attribute(symbol, args, &block))
View
@@ -17,4 +17,13 @@
t.column :author_id, :integer
t.column :body, :text
end
+
+ create_table :tags, :force => true do |t|
+ t.column :name, :string
+ end
+
+ create_table :posts_tags, :force => true, :id => false do |t|
+ t.column :post_id, :integer
+ t.column :tag_id, :integer
+ end
end
View
@@ -8,13 +8,17 @@ class Person < ActiveRecord::Base
class Post < ActiveRecord::Base
has_many :comments
+ has_and_belongs_to_many :tags
end
class Comment < ActiveRecord::Base
belongs_to :post
belongs_to :author, :class_name => "Person"
end
+class Tag < ActiveRecord::Base
+ has_and_belongs_to_many :posts
+end
describe Machinist do
@@ -170,6 +174,12 @@ class Comment < ActiveRecord::Base
Comment.make.author.class.should == Person
end
+ it "should allow value to be assigned to has_and_belongs_to_many association" do
+ Tag.blueprint { }
+ Post.blueprint { tags [ Tag.make ] }
+ Post.make.tags.should have(1).instance_of(Tag)
+ end
+
describe "on a has_many association" do
before do
Post.blueprint { }
View
@@ -1,4 +1,5 @@
$LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib'
+require 'rubygems'
require 'test/unit'
require 'spec'
require 'active_record'

0 comments on commit f31cd75

Please sign in to comment.