Sha256: a0c2aa76459888ec98d6cc56d6d66bc6482cf241760a44bbfcfe423bcc5bf1ae

Contents?: true

Size: 1.6 KB

Versions: 1

Compression:

Stored size: 1.6 KB

Contents

module CamaleonCms
  class PostComment < ActiveRecord::Base
    include CamaleonCms::Metas

    self.table_name = "#{PluginRoutes.static_system_info['db_prefix']}comments"
    # attr_accessible :user_id, :post_id, :content, :author, :author_email, :author_url, :author_IP, :approved, :agent, :agent, :typee, :comment_parent, :is_anonymous
    attr_accessor :is_anonymous

    #default_scope order('comments.created_at ASC')
    #approved: approved | pending | spam

    cama_define_common_relationships('PostComment')
    has_many :children, class_name: 'CamaleonCms::PostComment', foreign_key: :comment_parent, dependent: :destroy
    belongs_to :post
    belongs_to :parent, class_name: 'CamaleonCms::PostComment', foreign_key: :comment_parent
    belongs_to :user, class_name: CamaManager.get_user_class_name, foreign_key: :user_id

    default_scope {order("#{CamaleonCms::PostComment.table_name}.created_at DESC")}

    scope :main, -> { where(comment_parent: nil) }
    scope :comment_parent, -> { where(comment_parent: 'is not null') }
    scope :approveds, -> { where(approved: 'approved') }

    validates :content, presence: true
    validates_presence_of :author, :author_email, if: Proc.new { |c| c.is_anonymous.present? }
    after_create :update_counter
    after_destroy :update_counter

    # return the owner of this comment
    def comment_user
      user
    end

    # check if this comments is already approved
    def is_approved?
      self.approved == 'approved'
    end

    private

    # update comment counter
    def update_counter
      post&.set_meta('comments_count', post.comments.count)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
camaleon_cms-2.6.0 app/models/camaleon_cms/post_comment.rb