Sha256: b23cda6d1335c5a7c7491c25ac441e62387775cd4a4aa03a4ec73d4cb07b2bc4

Contents?: true

Size: 1.37 KB

Versions: 83

Compression:

Stored size: 1.37 KB

Contents

module Tenon
  class Comment < ActiveRecord::Base
    include Humanizer
    require_human_on :create, unless: :bypass_humanizer

    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_accessor :subscribe, :controller, :bypass_humanizer

    default_scope { order('created_at DESC') }
    scope :approved, -> { where(approved: true) }
    scope :unapproved, -> { where(approved: false) }

    # 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 toggle_approved!
      self.approved = self.approved? ? false : true
      save
    end

    def approve
      self.approved = true
    end

    private

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

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

Version data entries

83 entries across 83 versions & 1 rubygems

Version Path
tenon-1.1.8 app/models/tenon/comment.rb
tenon-1.1.7 app/models/tenon/comment.rb
tenon-1.1.6 app/models/tenon/comment.rb
tenon-1.1.5 app/models/tenon/comment.rb
tenon-1.1.4 app/models/tenon/comment.rb
tenon-1.1.3 app/models/tenon/comment.rb
tenon-1.1.2 app/models/tenon/comment.rb
tenon-1.1.1 app/models/tenon/comment.rb
tenon-1.0.76 app/models/tenon/comment.rb
tenon-1.0.75 app/models/tenon/comment.rb
tenon-1.0.74 app/models/tenon/comment.rb
tenon-1.0.73 app/models/tenon/comment.rb
tenon-1.0.72 app/models/tenon/comment.rb
tenon-1.0.71 app/models/tenon/comment.rb
tenon-1.0.70 app/models/tenon/comment.rb
tenon-1.0.69 app/models/tenon/comment.rb
tenon-1.0.68 app/models/tenon/comment.rb
tenon-1.0.67 app/models/tenon/comment.rb
tenon-1.0.66 app/models/tenon/comment.rb
tenon-1.0.65 app/models/tenon/comment.rb