mattvanhorn-acts_as_commentable 2.0.1 → 2.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. data/lib/commentable_methods.rb +66 -0
  2. metadata +2 -1
@@ -0,0 +1,66 @@
1
+ require 'activerecord'
2
+
3
+ # ActsAsCommentable
4
+ module Juixe
5
+ module Acts #:nodoc:
6
+ module Commentable #:nodoc:
7
+
8
+ def self.included(base)
9
+ base.extend ClassMethods
10
+ end
11
+
12
+ module ClassMethods
13
+ def acts_as_commentable
14
+ has_many :comments, :as => :commentable, :dependent => :destroy, :order => 'created_at ASC'
15
+ include Juixe::Acts::Commentable::InstanceMethods
16
+ extend Juixe::Acts::Commentable::SingletonMethods
17
+ end
18
+ end
19
+
20
+ # This module contains class methods
21
+ module SingletonMethods
22
+ # Helper method to lookup for comments for a given object.
23
+ # This method is equivalent to obj.comments.
24
+ def find_comments_for(obj)
25
+ commentable = ActiveRecord::Base.send(:class_name_of_active_record_descendant, self).to_s
26
+
27
+ Comment.find(:all,
28
+ :conditions => ["commentable_id = ? and commentable_type = ?", obj.id, commentable],
29
+ :order => "created_at DESC"
30
+ )
31
+ end
32
+
33
+ # Helper class method to lookup comments for
34
+ # the mixin commentable type written by a given user.
35
+ # This method is NOT equivalent to Comment.find_comments_for_user
36
+ def find_comments_by_user(user)
37
+ commentable = ActiveRecord::Base.send(:class_name_of_active_record_descendant, self).to_s
38
+
39
+ Comment.find(:all,
40
+ :conditions => ["user_id = ? and commentable_type = ?", user.id, commentable],
41
+ :order => "created_at DESC"
42
+ )
43
+ end
44
+ end
45
+
46
+ # This module contains instance methods
47
+ module InstanceMethods
48
+ # Helper method to sort comments by date
49
+ def comments_ordered_by_submitted
50
+ Comment.find(:all,
51
+ :conditions => ["commentable_id = ? and commentable_type = ?", id, self.type.name],
52
+ :order => "created_at DESC"
53
+ )
54
+ end
55
+
56
+ # Helper method that defaults the submitted time.
57
+ def add_comment(comment)
58
+ comments << comment
59
+ end
60
+ end
61
+
62
+ end
63
+ end
64
+ end
65
+
66
+ ActiveRecord::Base.send(:include, Juixe::Acts::Commentable)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mattvanhorn-acts_as_commentable
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.1
4
+ version: 2.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cosmin Radoi, Jack Dempsey, Xelipe, Chris Eppstein
@@ -31,6 +31,7 @@ files:
31
31
  - generators/comment/templates/comment.rb
32
32
  - generators/comment/templates/create_comments.rb
33
33
  - lib/acts_as_commentable.rb
34
+ - lib/commentable_methods.rb
34
35
  - lib/comment_methods.rb
35
36
  - tasks/acts_as_commentable_tasks.rake
36
37
  - init.rb