Sha256: ffb50df46d3a7a99a5b82b9e0a2be3e70639e7c7ecbb32f5878241b5c3d22cc6

Contents?: true

Size: 1.94 KB

Versions: 11

Compression:

Stored size: 1.94 KB

Contents

class Comment < ActiveRecord::Base
  # TODO: put this back in
  # has_rakismet
  def spam?; false; end

  validates_presence_of :commentable, :author, :author_email, :content
  validates_format_of :author_email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i

  belongs_to :commentable, :polymorphic => true

  attr_protected :approved
  attr_accessor :subscribe, :controller

  default_scope { order("created_at DESC") }
  scope :approved, -> { where(:approved => true).order("created_at ASC") }

  # open up everything for mass assignment
  attr_protected

  # Comment/uncomment this to turn off/on moderation
  # before_save :approve
  
  after_save :handle_subscribers

  def self.create_comment(object, session)
    session.blank? ? Comment.new(:commentable_type => object.class.to_s, :commentable_id => object.id) : Comment.new(session)
  end

  def email
    self.author_email
  end

  def approve!
    self.approved = true
    return self.save
  end

  def unapprove!
    self.approved = false
    return self.save
  end

  def approve
    self.approved = true
  end

  def author_url
    if super.blank? || super.match('http://') || super.match('https://')
      return super
    else
      return 'http://' + super
    end
  end

  def short?
    content.length > 100 ? true : false
  end

  def short_comment
    "#{self.content[0..100]}..."
  end

  def error_message
    errors = []
    self.errors.full_messages.each { |error| errors << "<li>#{error}</li>" }
    return "<div class='errorExplanation' id='errorExplanation'>There were problems with the following fields:<ul>#{errors}<ul></div>"
  end

  private

    def handle_subscribers
      # create subscriber
      CommentSubscriber.create(:commentable => self.commentable, :email => self.author_email) if self.subscribe.to_i == 1

      # notify subscribers
      self.commentable.subscribers.each {|subscriber| CommentMailer.comment_notification(subscriber.email, self).deliver} if self.approved?
    end

end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
forge-cli-0.1.10 lib/forge/app/models/comment.rb
forge-cli-0.1.9 lib/forge/app/models/comment.rb
forge-cli-0.1.8 lib/forge/app/models/comment.rb
forge-cli-0.1.7 lib/forge/app/models/comment.rb
forge-cli-0.1.6 lib/forge/app/models/comment.rb
forge-cli-0.1.5 lib/forge/app/models/comment.rb
forge-cli-0.1.4 lib/forge/app/models/comment.rb
forge-cli-0.1.3 lib/forge/app/models/comment.rb
forge-cli-0.1.2 lib/forge/app/models/comment.rb
forge-cli-0.1.1 lib/forge/app/models/comment.rb
forge-cli-0.1.0 lib/forge/app/models/comment.rb