Add the has_many_polymorphs plugin
[cs356-p2-videostore.git] / vendor / plugins / has_many_polymorphs / generators / commenting / templates / comment.rb
1
2 # The Comment model. This model is automatically generated and added to your app if you run the commenting generator.
3
4 class Comment < ActiveRecord::Base
5
6   # If database speed becomes an issue, you could remove these validations and rescue the ActiveRecord database constraint errors instead.
7   validates_presence_of :name, :email, :body
8   validates_format_of   :email, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
9
10   after_validation :prepend_url
11   
12   # Set up the polymorphic relationship.
13   has_many_polymorphs :commentables, 
14     :from => [<%= commentable_models.join(", ") %>], 
15     :through => :commentings, 
16     :dependent => :destroy,
17 <% if options[:self_referential] -%>    :as => :<%= parent_association_name -%>,
18 <% end -%>
19     :parent_extend => proc {
20     }
21     
22   # Tag::Error class. Raised by ActiveRecord::Base::TaggingExtensions if something goes wrong.
23   class Error < StandardError
24   end
25
26   protected
27   def prepend_url
28     return if self[:url].blank?
29     if self[:url] !~ /^http(s):\/\//i
30       self.url = 'http://' + self[:url]
31     end
32   end
33 end