|
12 | 12 | # ActsAsCommentableWithThreading
|
13 | 13 | module Acts #:nodoc:
|
14 | 14 | module CommentableWithThreading #:nodoc:
|
15 |
| - extend ActiveSupport::Concern |
| 15 | + extend ActiveSupport::Concern |
16 | 16 |
|
17 |
| - module ClassMethods |
18 |
| - def acts_as_commentable |
19 |
| - has_many :comment_threads, :class_name => "Comment", :as => :commentable |
20 |
| - before_destroy { |record| record.root_comments.destroy_all } |
21 |
| - include Acts::CommentableWithThreading::LocalInstanceMethods |
22 |
| - extend Acts::CommentableWithThreading::SingletonMethods |
| 17 | + module ClassMethods |
| 18 | + def acts_as_commentable |
| 19 | + has_many :comment_threads, class_name: 'Comment', as: :commentable |
| 20 | + before_destroy { |record| record.root_comments.destroy_all } |
| 21 | + include Acts::CommentableWithThreading::LocalInstanceMethods |
| 22 | + extend Acts::CommentableWithThreading::SingletonMethods |
| 23 | + end |
23 | 24 | end
|
24 |
| - end |
25 | 25 |
|
26 |
| - # This module contains class methods |
27 |
| - module SingletonMethods |
28 |
| - # Helper method to lookup for comments for a given object. |
29 |
| - # This method is equivalent to obj.comments. |
30 |
| - def find_comments_for(obj) |
31 |
| - Comment.where(:commentable_id => obj.id, :commentable_type => obj.class.base_class.name).order('created_at DESC') |
32 |
| - end |
| 26 | + # This module contains class methods |
| 27 | + module SingletonMethods |
| 28 | + # Helper method to lookup for comments for a given object. |
| 29 | + # This method is equivalent to obj.comments. |
| 30 | + def find_comments_for(obj) |
| 31 | + Comment.where(commentable_id: obj.id, |
| 32 | + commentable_type: obj.class.base_class.name) |
| 33 | + .order('created_at DESC') |
| 34 | + end |
33 | 35 |
|
34 |
| - # Helper class method to lookup comments for |
35 |
| - # the mixin commentable type written by a given user. |
36 |
| - # This method is NOT equivalent to Comment.find_comments_for_user |
37 |
| - def find_comments_by_user(user) |
38 |
| - commentable = self.base_class.name.to_s |
39 |
| - Comment.where(:user_id => user.id, :commentable_type => commentable).order('created_at DESC') |
| 36 | + # Helper class method to lookup comments for |
| 37 | + # the mixin commentable type written by a given user. |
| 38 | + # This method is NOT equivalent to Comment.find_comments_for_user |
| 39 | + def find_comments_by_user(user) |
| 40 | + commentable = base_class.name.to_s |
| 41 | + Comment.where(user_id: user.id, commentable_type: commentable) |
| 42 | + .order('created_at DESC') |
| 43 | + end |
40 | 44 | end
|
41 |
| - end |
42 |
| - |
43 |
| - module LocalInstanceMethods |
44 | 45 |
|
45 |
| - # Helper method to display only root threads, no children/replies |
46 |
| - def root_comments |
47 |
| - self.comment_threads.where(:parent_id => nil) |
48 |
| - end |
| 46 | + module LocalInstanceMethods |
| 47 | + # Helper method to display only root threads, no children/replies |
| 48 | + def root_comments |
| 49 | + comment_threads.where(parent_id: nil) |
| 50 | + end |
49 | 51 |
|
50 |
| - # Helper method to sort comments by date |
51 |
| - def comments_ordered_by_submitted |
52 |
| - Comment.where(:commentable_id => id, :commentable_type => self.class.name).order('created_at DESC') |
53 |
| - end |
| 52 | + # Helper method to sort comments by date |
| 53 | + def comments_ordered_by_submitted |
| 54 | + Comment.where(commentable_id: id, commentable_type: self.class.name) |
| 55 | + .order('created_at DESC') |
| 56 | + end |
54 | 57 |
|
55 |
| - # Helper method that defaults the submitted time. |
56 |
| - def add_comment(comment) |
57 |
| - comments << comment |
| 58 | + # Helper method that defaults the submitted time. |
| 59 | + def add_comment(comment) |
| 60 | + comments << comment |
| 61 | + end |
58 | 62 | end
|
59 | 63 | end
|
60 |
| - |
61 |
| - end |
62 | 64 | end
|
63 | 65 |
|
64 | 66 | ActiveRecord::Base.send(:include, Acts::CommentableWithThreading)
|
0 commit comments