This repository has been archived by the owner on Apr 5, 2023. It is now read-only.
forked from fatfreecrm/fat_free_crm
/
comment_methods.rb
43 lines (39 loc) · 1.59 KB
/
comment_methods.rb
1
2
3
4
5
6
7
8
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
module ActsAsCommentable
# including this module into your Comment model will give you finders and named scopes
# useful for working with Comments.
# The named scopes are:
# in_order: Returns comments in the order they were created (created_at ASC).
# recent: Returns comments by how recently they were created (created_at DESC).
# limit(N): Return no more than N comments.
module Comment
def self.included(comment_model)
comment_model.extend Finders
comment_model.named_scope :in_order, {:order => 'created_at ASC'}
comment_model.named_scope :recent, {:order => "created_at DESC"}
comment_model.named_scope :limit, lambda {|limit| {:limit => limit}}
end
module Finders
# Helper class method to lookup all comments assigned
# to all commentable types for a given user.
def find_comments_by_user(user)
find(:all,
:conditions => ["user_id = ?", user.id],
:order => "created_at DESC"
)
end
# Helper class method to look up all comments for
# commentable class name and commentable id.
def find_comments_for_commentable(commentable_str, commentable_id)
find(:all,
:conditions => ["commentable_type = ? and commentable_id = ?", commentable_str, commentable_id],
:order => "created_at DESC"
)
end
# Helper class method to look up a commentable object
# given the commentable class name and id
def find_commentable(commentable_str, commentable_id)
commentable_str.constantize.find(commentable_id)
end
end
end
end