Class: Proclaim::CommentsController

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

Instance Method Summary collapse

Instance Method Details

#createObject



9
10
11
12
13
14
15
16
17
18
19
20
21
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
47
48
49
50
51
52
53
54
55
56
# File 'app/controllers/proclaim/comments_controller.rb', line 9

def create
	@comment = Comment.new(comment_params)

	begin
		authorize @comment

		if antispam_params[:answer] == antispam_params[:solution]
			subscription = nil
			params = subscription_params
			if params and params[:subscribe]
				subscription = Subscription.new(email: params[:email], post: @comment.post)
			end

			respond_to do |format|
				begin
					Comment.transaction do
						@comment.save!

						if subscription
							subscription.save!
						end

						format.json { render_comment_json(@comment) }
					end
				rescue ActiveRecord::RecordInvalid
					errorMessages = Array.new
					errorMessages += @comment.errors.full_messages

					if subscription
						errorMessages += subscription.errors.full_messages
					end

					format.json { render json: errorMessages, status: :unprocessable_entity }
				end
			end
		else
			respond_to do |format|
				format.json { render json: ["Antispam question wasn't answered correctly"], status: :unprocessable_entity }
			end
		end
	rescue Pundit::NotAuthorizedError
		respond_to do |format|
			# Don't leak that the post actually exists. Turn the
			# "unauthorized" into a "not found"
			format.json { render json: true, status: :not_found }
		end
	end
end

#destroyObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'app/controllers/proclaim/comments_controller.rb', line 76

def destroy
	begin
		authorize @comment

		respond_to do |format|
			@comment.destroy
			format.json { render json: true, status: :ok }
		end
	rescue Pundit::NotAuthorizedError
		respond_to do |format|
			format.json { render json: true, status: :unauthorized }
		end
	end
end

#updateObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/controllers/proclaim/comments_controller.rb', line 58

def update
	begin
		authorize @comment

		respond_to do |format|
			if @comment.update(comment_params)
				format.json { render_comment_json(@comment) }
			else
				format.json { render json: @comment.errors.full_messages, status: :unprocessable_entity }
			end
		end
	rescue Pundit::NotAuthorizedError
		respond_to do |format|
			format.json { render json: true, status: :unauthorized }
		end
	end
end