Skip to content

Commit 20e11c8

Browse files
committed
Add AuthorDetails Concern
#TODO Tests
1 parent da84ed7 commit 20e11c8

File tree

5 files changed

+17
-7
lines changed

5 files changed

+17
-7
lines changed

app/controllers/comments_controller.rb

-4
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@ def create
2121
@comment = @protip.comments.build(create_comment_params)
2222

2323
@comment.user = current_user
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
2824
@comment.request_format = request.format.to_s
2925

3026
if @comment.save

app/models/comment.rb

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
class Comment < ActiveRecord::Base
1919
include ActsAsCommentable::Comment
20+
include AuthorDetails
2021
include SpamFilter
2122

2223
belongs_to :commentable, polymorphic: true

app/models/concerns/author_details.rb

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module AuthorDetails
2+
extend ActiveSupport::Concern
3+
4+
included do
5+
before_save do
6+
self.user_name = user.name
7+
self.user_email = user.email
8+
self.user_agent = user.last_ua
9+
self.user_ip = user.last_ip
10+
end
11+
end
12+
13+
end

app/models/concerns/spam_filter.rb

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ module SpamFilter
22
extend ActiveSupport::Concern
33

44
included do
5-
after_save :analyze_spam
65
has_one :spam_report, as: :spammable
76
include Rakismet::Model
87

@@ -13,9 +12,9 @@ module SpamFilter
1312
user_ip: :remote_ip,
1413
user_agent: :user_agent
1514

16-
def analyze_spam
15+
after_save do
1716
AnalyzeSpamJob.perform_async({ id: id, klass: self.class.name })
18-
end
17+
end
1918

2019
end
2120
end

app/models/protip.rb

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class Protip < ActiveRecord::Base
4343
acts_as_commentable
4444

4545
include ProtipMapping
46+
include AuthorDetails
4647
include SpamFilter
4748

4849
paginates_per(PAGESIZE = 18)

0 commit comments

Comments
 (0)