Sha256: a4170f3215691ec3e54f2ea76cb9e07dd76e67b40db315e41fc68f289794f437

Contents?: true

Size: 1.65 KB

Versions: 5

Compression:

Stored size: 1.65 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, required: false
    belongs_to :parent, class_name: 'CamaleonCms::PostComment', foreign_key: :comment_parent, required: false
    belongs_to :user, class_name: CamaManager.get_user_class_name, foreign_key: :user_id, required: false

    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

5 entries across 5 versions & 1 rubygems

Version Path
camaleon_cms-2.6.4 app/models/camaleon_cms/post_comment.rb
camaleon_cms-2.6.3 app/models/camaleon_cms/post_comment.rb
camaleon_cms-2.6.2 app/models/camaleon_cms/post_comment.rb
camaleon_cms-2.6.1 app/models/camaleon_cms/post_comment.rb
camaleon_cms-2.6.0.1 app/models/camaleon_cms/post_comment.rb