Class: CommentsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/comments_controller.rb

Overview

FIXME: rewrite !

Instance Method Summary collapse

Methods included from Zena::App

included

Instance Method Details

#createObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/controllers/comments_controller.rb', line 22

def create
  @discussion.save if @discussion.new_record? && @node.can_comment?

  @comment = secure(Comment) { Comment.new(filter_attributes(params[:comment])) }

  if should_save(@comment, params)
    @comment.save
  else
    #...
  end

  respond_to do |format|
    if @comment.errors.empty?
      flash.now[:notice] = _('Comment was successfully created.')
      format.html { redirect_to zen_path(@node) } # TODO: add ':sharp => ...'
      format.js
      format.xml  { head :created, :location => comment_path(@node) }
    else
      flash[:error] = error_messages_for(:comment, :object => @comment)
      format.html { redirect_to zen_path(@node) }
      format.js
      format.xml  { render :xml => @comment.errors.to_xml }
    end
  end
end

#editObject

TODO: test



49
50
51
52
53
54
55
56
57
# File 'app/controllers/comments_controller.rb', line 49

def edit
  @comment    = Comment.find(params[:id])
  @discussion = @comment.discussion
  @node = secure(Node) { Node.find(@discussion[:node_id]) }
  @edit = true
  unless @node && @node.can_comment? && @comment[:user_id] == visitor.id
    render :nothing=>true
  end
end

#empty_binObject

TODO: test



112
113
114
115
116
117
118
# File 'app/controllers/comments_controller.rb', line 112

def empty_bin
  bin_content.each do |c|
    c.destroy
  end
  # reset cached bin content
  @bin_content = nil
end

#indexObject

TODO:test



97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'app/controllers/comments_controller.rb', line 97

def index
  @node = visitor.node
  secure(Node) do
    # TODO: preload node
    @comments = Comment.paginate :all,
                      :select => "comments.*, nodes.zip AS node_zip",
                      :order => 'status DESC, comments.created_at DESC',
                      :joins => ['INNER JOIN discussions on discussions.id = comments.discussion_id', 'INNER JOIN nodes on nodes.id = discussions.node_id'],
                      :conditions=>"status > #{Zena::Status::Rem} AND (#{secure_scope('nodes')})",
                      :per_page => 100, :page => params[:page]
  end
  @comments ||= []
end

#publishObject

TODO: test



84
85
86
87
88
89
90
91
92
93
# File 'app/controllers/comments_controller.rb', line 84

def publish
  @comment    = Comment.find(params[:id])
  @discussion = @comment.discussion
  @node = secure_drive(Node) { Node.find(@discussion[:node_id]) }
  if @node
    @comment.publish
  else
    render :nothing=>true
  end
end

#removeObject

TODO: test



71
72
73
74
75
76
77
78
79
80
81
# File 'app/controllers/comments_controller.rb', line 71

def remove
  @comment    = Comment.find(params[:id])
  @discussion = @comment.discussion
  @node = secure(Node) { Node.find(@discussion[:node_id]) }
  if @node && visitor.is_admin? || @node.can_drive?
    Node.logger.error "\n\nremoving...\n\n"
    @comment.remove
  else
    render :nothing=>true
  end
end

#reply_toObject

TODO: test



10
11
12
13
14
15
16
17
18
19
# File 'app/controllers/comments_controller.rb', line 10

def reply_to
  @reply_to   = Comment.find(params[:id])
  @discussion = @reply_to.discussion
  @node = secure(Node) { Node.find(@discussion[:node_id]) }
  if @node && @node.can_comment?
    @comment = Comment.new(:reply_to=>@reply_to[:id], :discussion_id=>@discussion[:id])
  else
    render :nothing=>true
  end
end

#updateObject



60
61
62
63
64
65
66
67
68
# File 'app/controllers/comments_controller.rb', line 60

def update
  @comment.update_attributes(params[:comment])

  respond_to do |format|
    format.html { redirect_to zen_path(@node) }
    format.js
    format.xml  { head :updated, :location => comment_path(@node) }
  end
end