Skip to content

Commit 47e9a2a

Browse files
committed
save commenter details in the same comment record
1 parent 975dc60 commit 47e9a2a

File tree

3 files changed

+28
-14
lines changed

3 files changed

+28
-14
lines changed

app/controllers/comments_controller.rb

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,22 @@ def create
1818
create_comment_params = params.require(:comment).permit(:comment)
1919

2020
redirect_to_signup_if_unauthenticated(request.referer + "?" + (create_comment_params.try(:to_query) || ""), "You must signin/signup to add a comment") do
21-
@comment = @protip.comments.build(create_comment_params)
21+
@comment = @protip.comments.build(create_comment_params)
22+
2223
@comment.user = current_user
23-
respond_to do |format|
24-
if @comment.save
25-
record_event('created comment')
26-
format.html { redirect_to protip_path(@comment.commentable.try(:public_id)) }
27-
format.json { render json: @comment, status: :created, location: @comment }
28-
else
29-
format.html { redirect_to protip_path(@comment.commentable.try(:public_id)), error: "could not add your comment. try again" }
30-
format.json { render json: @comment.errors, status: :unprocessable_entity }
31-
end
24+
@comment.user_name = current_user.name
25+
@comment.user_email = current_user.email
26+
@comment.user_agent = request.user_agent
27+
@comment.user_ip = request.remote_ip
28+
@comment.request_format = request.format
29+
30+
if @comment.save
31+
record_event('created comment')
32+
format.html { redirect_to protip_path(@comment.commentable.try(:public_id)) }
33+
format.json { render json: @comment, status: :created, location: @comment }
34+
else
35+
format.html { redirect_to protip_path(@comment.commentable.try(:public_id)), error: "could not add your comment. try again" }
36+
format.json { render json: @comment.errors, status: :unprocessable_entity }
3237
end
3338
end
3439
end

app/models/comment.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ class Comment < ActiveRecord::Base
3333
alias_method :author, :user
3434
alias_attribute :body, :comment
3535

36-
rakismet_attrs author: proc { self.user.name },
37-
author_email: proc { self.user.email },
36+
rakismet_attrs author: -> { self.user_name },
37+
author_email: -> { self.user_email },
3838
content: :comment,
3939
blog: ENV['AKISMET_URL'],
40-
user_ip: proc { self.user.last_ip },
41-
user_agent: proc { self.user.last_ua }
40+
user_ip: -> { self.remote_ip },
41+
user_agent: -> { self.user_agent }
4242

4343
validates :comment, length: { minimum: 2 }
4444

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class AddUserdataToComment < ActiveRecord::Migration
2+
def change
3+
add_column :comments, :user_name, :string
4+
add_column :comments, :user_email, :string
5+
add_column :comments, :user_agent, :string
6+
add_column :comments, :remote_ip, :inet
7+
add_column :comments, :request_format, :string
8+
end
9+
end

0 commit comments

Comments
 (0)