diff options
author | Star Rauchenberger <fefferburbia@gmail.com> | 2023-10-12 17:10:34 -0400 |
---|---|---|
committer | Star Rauchenberger <fefferburbia@gmail.com> | 2023-10-12 17:10:34 -0400 |
commit | 835af696703484208882a70cc5dd47c5838ecf58 (patch) | |
tree | 8174d5b8c6f2486da56569bad361dd10aa1183f8 /app/controllers | |
parent | b24e6f58a525fc576e23ef9d498745e64c16cd6c (diff) | |
download | thoughts-835af696703484208882a70cc5dd47c5838ecf58.tar.gz thoughts-835af696703484208882a70cc5dd47c5838ecf58.tar.bz2 thoughts-835af696703484208882a70cc5dd47c5838ecf58.zip |
Added blog post commenting
Diffstat (limited to 'app/controllers')
-rw-r--r-- | app/controllers/comments_controller.rb | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb new file mode 100644 index 0000000..60c8f6a --- /dev/null +++ b/app/controllers/comments_controller.rb | |||
@@ -0,0 +1,59 @@ | |||
1 | class CommentsController < ApplicationController | ||
2 | def create | ||
3 | @blog = Blog.find_by_slug(params[:slug]) | ||
4 | |||
5 | raise ActiveRecord::RecordNotFound unless @blog | ||
6 | raise ActiveRecord::RecordNotFound unless @blog.published | ||
7 | |||
8 | @comment = @blog.comments.new(comment_params) | ||
9 | |||
10 | unless @comment.valid? | ||
11 | flash.alert = "Error posting comment." | ||
12 | render "blogs/show" | ||
13 | return | ||
14 | end | ||
15 | |||
16 | akismet_vars = %w{ HTTP_ACCEPT HTTP_ACCEPT_ENCODING } | ||
17 | akismet_params = { | ||
18 | type: "comment", | ||
19 | text: @comment.body, | ||
20 | created_at: DateTime.now, | ||
21 | author: @comment.username, | ||
22 | author_email: @comment.email, | ||
23 | author_url: @comment.website, | ||
24 | post_url: url_for(@comment.blog), | ||
25 | post_modified_at: @comment.blog.updated_at, | ||
26 | referrer: request.referrer, | ||
27 | env: request.env.slice(*akismet_vars) | ||
28 | } | ||
29 | |||
30 | is_spam, is_blatant = Akismet.check(request.ip, request.user_agent, akismet_params) | ||
31 | |||
32 | if is_blatant | ||
33 | # I am lying. | ||
34 | flash.notice = "Comment submitted successfully! It will need to be moderated before it shows up on the blog." | ||
35 | else | ||
36 | if is_spam | ||
37 | @comment.status = :pending | ||
38 | flash_message = "Comment submitted successfully! It will need to be moderated before it shows up on the blog." | ||
39 | else | ||
40 | @comment.status = :published | ||
41 | flash_message = "Comment posted successfully!" | ||
42 | end | ||
43 | |||
44 | if @comment.save | ||
45 | flash.notice = flash_message | ||
46 | else | ||
47 | flash.alert = "Error posting comment." | ||
48 | end | ||
49 | end | ||
50 | |||
51 | redirect_to @comment.blog | ||
52 | end | ||
53 | |||
54 | private | ||
55 | |||
56 | def comment_params | ||
57 | params.require(:comment).permit(:username, :email, :website, :body) | ||
58 | end | ||
59 | end | ||