Class: Comment

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
RubyLess, Zena::Use::QueryComment::ModelMethods
Defined in:
app/models/comment.rb

Overview

Comments can be added on a per discussion basis. There can be replies to other comments in the same discussion. Comments are signed by the user commenting. Public comments belong to the user anon (see #User) and must have the ‘athor_name’ field set.

If anonymous is moderated (User#moderated?), all public comments are set to ‘prop’ and are not directly seen on the site.

Instance Method Summary collapse

Methods included from Zena::Use::QueryComment::ModelMethods

included

Instance Method Details

#authorObject

Who wrote the comment (author’s Node model)



36
37
38
# File 'app/models/comment.rb', line 36

def author
  @author ||= (user ? user.node : nil)
end

#author_nameObject



44
45
46
# File 'app/models/comment.rb', line 44

def author_name
  self[:author_name] || (author ? author.title : nil)
end

#can_write?Boolean

TODO: test

Returns:

  • (Boolean)


78
79
80
# File 'app/models/comment.rb', line 78

def can_write?
  is_author? && discussion.open?
end

#nodeObject



40
41
42
# File 'app/models/comment.rb', line 40

def node
  @node ||= discussion.node
end

#parentObject



48
49
50
# File 'app/models/comment.rb', line 48

def parent
  @parent ||= self[:reply_to] ? secure(Comment) { Comment.find(self[:reply_to]) } : nil
end

#publishObject

Publish the comment (set it’s status to pub) TODO: test



59
60
61
# File 'app/models/comment.rb', line 59

def publish
  update_attributes( :status=> Zena::Status::Pub )
end

#removeObject

Remove the comment (set it’s status to rem)



53
54
55
# File 'app/models/comment.rb', line 53

def remove
  update_attributes( :status=> Zena::Status::Rem )
end

#replies(opt = {}) ⇒ Object



63
64
65
66
67
68
69
70
# File 'app/models/comment.rb', line 63

def replies(opt={})
  if opt[:with_prop]
    conditions = ["reply_to = ? AND status >= #{Zena::Status::Pub}", self[:id]]
  else
    conditions = ["reply_to = ? AND status = #{Zena::Status::Pub}", self[:id]]
  end
  secure(Comment) { Comment.find(:all, :conditions=>conditions, :order=>'created_at ASC') }
end

#userObject

Who wrote the comment (author’s User model)



31
32
33
# File 'app/models/comment.rb', line 31

def user
  @user ||= secure(User) { User.find(self[:user_id]) }
end

#zipObject

needed by zafu for ajaxy stuff



73
74
75
# File 'app/models/comment.rb', line 73

def zip
  self[:id]
end