Sha256: bbe690f5af0286b39d09d8df861f16ca245d281030f52f3aa70bdfc5c87eb419

Contents?: true

Size: 1.12 KB

Versions: 1

Compression:

Stored size: 1.12 KB

Contents

module Kublog
  class Comment < ActiveRecord::Base
    belongs_to :user
    belongs_to :post
    
    validates_presence_of :body
    validate              :has_user_details
    
    default_scope   order('kublog_comments.created_at ASC')
    
    delegate  :admin?, :to => :user, :allow_nil => true
    
    def author
      return self.author_name if self.user.nil?
      return self.user.to_s
    end
    
    # Site access to the category
    def path
      Engine.routes.url_helpers.post_comment_path(self.post, self)
    end
    
    def as_json(options={})
      super(options.merge({:methods => [:path, :author, :ftime, :admin?]}))
    end
    
    def ftime
      I18n.l(self.created_at, :format => :short)
    end
    
    private
    
    def has_user_details
      if self.user.nil?
        errors.add(:author_name,  :blank) if author_name.blank?
        errors.add(:author_email, :blank) if author_email.blank?
        errors.add(:author_email, :invalid) if !valid_author_email?
      end
    end
    
    def valid_author_email?
      self.author_email.to_s.match(/\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i)
    end
    
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
kublog-0.0.1.1 app/models/kublog/comment.rb