Permalink
Browse files

fix calls to use polymorphic and add some basic specs

  • Loading branch information...
1 parent d63d0b4 commit 3f431bf0ed6a639e6f349875f8b1bb4921e55adf @jackdempsey committed Dec 9, 2008
Showing with 43 additions and 10 deletions.
  1. +6 −5 spec/sequel-setup.rb
  2. +37 −5 spec/sequel_polymorphic/sequel_polymorphic_spec.rb
View
@@ -8,7 +8,8 @@ class Asset < Sequel::Model
varchar :attachable_type
end
- is :polymorphic, :belongs_to => :attachable
+ is :polymorphic
+ belongs_to :attachable, :polymorphic => true
end
@@ -18,8 +19,8 @@ class Post < Sequel::Model
varchar :name
end
- is :polymorphic, :has_many => :assets, :as => :attachable
-
+ is :polymorphic
+ has_many :assets, :as => :attachable
end
@@ -29,8 +30,8 @@ class Note < Sequel::Model
varchar :name
end
- is :polymorphic, :has_many => :assets, :as => :attachable
-
+ is :polymorphic
+ has_many :assets, :as => :attachable
end
[Asset, Post, Note].each {|klass| klass.create_table!}
@@ -1,12 +1,44 @@
require File.dirname(__FILE__) + '/../spec_helper'
-# Models we have: Post(name), Note(name), Asset(name)
+# Schema here for reference
+# class Asset < Sequel::Model
+# set_schema { primary_key :id; varchar :name; integer :attachable_id; varchar :attachable_type }
+# is :polymorphic
+# belongs_to :attachable, :polymorphic => true
+# end
+#
+# class Post < Sequel::Model
+# set_schema { primary_key :id; varchar :name }
+# is :polymorphic
+# has_many :assets, :as => :attachable
+# end
+#
+# class Note < Sequel::Model
+# set_schema { primary_key :id; varchar :name; }
+# is :polymorphic
+# has_many :assets, :as => :attachable
+# end
+
describe Sequel::Plugins::Polymorphic do
- it "should add :as to has_many/one_to_many" do
-
+ before do
+ Post.delete_all
+ Asset.delete_all
end
- it "should add :polymorphic => true to belongs_to" do
-
+ it "should create an add_asset method which associates an Asset with a Post" do
+ post = Post.create(:name => 'test post')
+ asset = Asset.create(:name => "post's asset")
+ post.add_asset(asset)
+ asset.attachable_id == post.pk
+ asset.attachable_type == post.class
+ end
+
+ it "should return the list of related objects" do
+ post = Post.create(:name => 'test post')
+ asset1 = Asset.create(:name => "post's first asset")
+ asset2 = Asset.create(:name => "post's second asset")
+ post.add_asset(asset1)
+ post.add_asset(asset2)
+ post.assets.should == [asset1,asset2]
end
end

0 comments on commit 3f431bf

Please sign in to comment.