Add the has_many_polymorphs plugin
[cs356-p2-videostore.git] / vendor / plugins / has_many_polymorphs / generators / commenting / templates / commenting_extensions.rb
1 class ActiveRecord::Base
2   module CommentingExtensions
3
4     def comment_count
5       commentable?
6       self.comments.size
7     end
8
9     def comment_with(attributes)
10       commentable?(true)
11       begin
12         comment = Comment.create(attributes)
13         raise Comment::Error, "Comment could not be saved with" if comment.new_record?
14         comment.commentables << self
15       rescue
16       end
17     end
18
19     private
20     def commentable?(should_raise = false) #:nodoc:
21       unless flag = respond_to?(:<%= parent_association_name -%>s)
22         raise "#{self.class} is not a commentable model" if should_raise
23       end
24       flag
25     end
26   end
27
28   include CommentingExtensions
29 end
30