Sha256: cb1a3e5c9a42713b4a70d23e59e19cc3d9091fc286bf2756d900d1874fe553a6

Contents?: true

Size: 887 Bytes

Versions: 2

Compression:

Stored size: 887 Bytes

Contents

class Comment < ActiveRecord::Base
  include Concerns::Parentable
  include Concerns::Locale

  include Concerns::Spammable
  spammable author_email: :email, content: :message

  STATUSES = %w(pending_moderation published)

  after_initialize :default_values

  belongs_to :commentable, polymorphic: true

  validates :author, :message, presence: true
  validates :status, inclusion: { in: STATUSES }
  validates :email, presence: true,
            format: { with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i }
  validates :website, url: true, allow_blank: true

  def published?
    status.to_s == 'published'
  end

  def publish!
    update_attribute :status, :published
  end

  def unpublish!
    update_attribute :status, :pending_moderation
  end

  private

  def default_values
    if self.new_record?
      self.status = 'pending_moderation' if status.nil?
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
udongo-1.0.0 app/models/comment.rb
udongo-0.1.0 app/models/comment.rb