public
Description: The open source social networking platform in Ruby on Rails from the author of RailsSpace
Homepage: http://insoshi.com
Clone URL: git://github.com/insoshi/insoshi.git
Polymorphic Comment model
Michael Hartl (author)
Mon Apr 07 21:42:22 -0700 2008
commit  c2a052f575adda8b1b3e57eaecfede71709fecda
tree    54710914dbf0d2979ae1a6c062cc40a8565b56ca
parent  989780e15cf52d7d15cd44ef9971643c18035dfa
...
15
16
17
18
 
 
 
 
19
20
...
15
16
17
 
18
19
20
21
22
23
0
@@ -15,6 +15,9 @@
0
 
0
 class Comment < ActiveRecord::Base
0
   include ActivityLogger
0
- validates_presence_of :body
0
+ validates_presence_of :body, :commenter
0
+ belongs_to :commentable, :polymorphic => true
0
+ belongs_to :commenter, :class_name => "Person",
0
+ :foreign_key => "commenter_id"
0
   has_many :activities, :foreign_key => "item_id", :dependent => :destroy
0
 end
...
41
42
43
44
45
46
 
 
 
47
48
49
...
41
42
43
 
 
 
44
45
46
47
48
49
0
@@ -41,9 +41,9 @@ class Person < ActiveRecord::Base
0
   NUM_RECENT = 8
0
   FEED_SIZE = 10
0
 
0
- has_one :blog
0
- has_many :comments, :class_name => "WallComment",
0
- :order => "created_at DESC", :limit => NUM_WALL_COMMENTS
0
+ has_one :blog
0
+ has_many :comments, :as => :commentable, :order => 'created_at DESC',
0
+ :limit => NUM_WALL_COMMENTS
0
   has_many :connections
0
   has_many :contacts, :through => :connections,
0
                       :conditions => "status = #{Connection::ACCEPTED}"
...
1
2
3
4
5
6
7
8
 
 
 
9
10
11
12
13
14
 
15
16
17
...
1
2
3
 
4
 
 
 
5
6
7
8
9
10
 
11
 
12
13
14
15
0
@@ -1,17 +1,15 @@
0
 class CreateComments < ActiveRecord::Migration
0
   def self.up
0
     create_table :comments do |t|
0
- t.integer :person_id
0
       t.integer :commenter_id
0
- t.integer :blog_post_id
0
- t.text :body
0
- t.string :type
0
+ t.integer :commentable_id
0
+ t.string :commentable_type, :default => "", :null => false
0
+ t.text :body
0
 
0
       t.timestamps
0
     end
0
- add_index :comments, :person_id
0
     add_index :comments, :commenter_id
0
- add_index :comments, :type
0
+ add_index :comments, [:commentable_id, :commentable_type]
0
   end
0
 
0
   def self.down

Comments

    No one has commented yet.