Class: CortexReaver::CommentController

Inherits:
Controller
  • Object
show all
Defined in:
lib/cortex_reaver/controller/comment.rb

Constant Summary collapse

MODEL =
Comment

Instance Method Summary collapse

Instance Method Details

#page(page) ⇒ Object

We only really care about tracking recent comments.



57
58
59
60
# File 'lib/cortex_reaver/controller/comment.rb', line 57

def page(page)
  @title = "Recent Comments"
  @comments = Comment.order(:created_on).reverse.limit(16)
end

#postObject

This action is referenced by public comment-posting forms.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/cortex_reaver/controller/comment.rb', line 63

def post
  unless request.post?
    flash[:error] = "No comment to post!"
    redirect_to MainController.r
  end
 
  # Check for robots
  unless request[:captcha].blank? and
         request[:comment].blank?
    # Robot!?
    flash[:error] = "Cortex Reaver is immune to your drivel, spambot."
    redirect MainController.r
  end 

  begin
    # Create comment
    CortexReaver.db.transaction do
      @comment = Comment.new

      @comment.body = request[:body]
      @comment.comment_id = request[:comment_id]
      @comment.journal_id = request[:journal_id]
      @comment.photograph_id = request[:photograph_id]
      @comment.page_id = request[:page_id]

      if session[:user]
        # A user is logged in. Use their account.
        @comment.creator = session[:user]
      else
        # Use anonymous info, if it won't conflict.
        if User.filter(:email => request[:email]).count > 0
          # Conflicts!
          flash[:error] = "Sorry, that email address belongs to a registered user. Would you like to <a href=\"/user/login\">log in</a>? Your comment will still be here when you get back."
          
          # Save comment and go back to the parent.
          session[:pending_comment] = @comment
          redirect @comment.parent.url
        else
          # Okay, set anonymous info
          @comment.name = request[:name] unless request[:name].blank?
          @comment.email = request[:email] unless request[:email].blank?
          @comment.http = request[:http] unless request[:http].blank?
        end
      end

      # Save
      raise unless @comment.save

      # Clear action cache
      Ramaze::Cache.action.clear

      flash[:notice] = "<a href=\"##{@comment.url.gsub(/.*#/, '')}\">Your comment</a> has been posted."
      redirect @comment.parent.url
    end
  rescue => e
    # An error occurred
    Ramaze::Log.error e.inspect + e.backtrace.join("\n")
    flash[:error] = "We couldn't post your comment."

    if @comment.parent
      session[:pending_comment] = @comment
      redirect @comment.parent.url + '#post-comment'
    else
      redirect MainController.r
    end
  end
end