acts_as_commentable 3.0.1 → 4.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -1,49 +1,67 @@
1
- Acts As Commentable
2
- =================
1
+ = Acts As Commentable
3
2
 
4
3
  Allows for comments to be added to multiple and different models.
5
4
 
6
- == Resources
5
+ == Installation :
7
6
 
8
- Install
7
+ Add the following line to your Gemfile
9
8
 
10
- * To install as a gem (recommended):
11
- ** gem install acts_as_commentable
12
- ** add the following line to your environment.rb: config.gem 'acts_as_commentable'
13
- ** If using bundler then just add "gem 'acts_as_commentable'" to your Gemfile and "bundle install"
9
+ === Rails 4
14
10
 
11
+ gem 'acts_as_commentable'
15
12
 
16
- * To install as a plugin
17
- ** Rails 3: script/plugin install http://github.com/jackdempsey/acts_as_commentable.git
18
- ** Rails 2: script/plugin install http://github.com/jackdempsey/acts_as_commentable.git -r 2.x
13
+ === Rails 3
14
+
15
+ gem 'acts_as_commentable', '3.0.1'
16
+
17
+ === Rails 2
18
+
19
+ gem 'acts_as_commentable', git: 'git@github.com:jackdempsey/acts_as_commentable.git' , branch: '2.x'
19
20
 
20
21
 
21
- Generate your comment model:
22
+ == Generator
23
+
24
+
25
+ === Rails 3+
22
26
 
23
- script/generate comment
27
+ rails g comment
28
+
29
+ === Rails 2
30
+
31
+ script/generate comment
24
32
 
25
33
  Then migrate your database:
26
34
 
27
- rake db:migrate
35
+ rake db:migrate
28
36
 
29
37
  == Usage
30
38
 
31
- * Make your ActiveRecord model act as commentable.
39
+ Make your ActiveRecord model act as commentable:
40
+
41
+ class Post < ActiveRecord::Base
42
+ acts_as_commentable
43
+ end
32
44
 
33
- class Post < ActiveRecord::Base
34
- acts_as_commentable
35
- end
45
+ Add a comment to a model instance:
36
46
 
37
- * Add a comment to a model instance
47
+ commentable = Post.create
48
+ commentable.comments.create(:title => "First comment.", :comment => "This is the first comment.")
49
+
50
+ Fetch comments for a commentable model:
38
51
 
39
- commentable = Post.create
40
- commentable.comments.create(:title => "First comment.", :comment => "This is the first comment.")
52
+ commentable = Post.find(1)
53
+ comments = commentable.comments.recent.limit(10).all
41
54
 
42
- * Fetch comments for a commentable model:
55
+ Add multiple type of comments to a model:
43
56
 
44
- commentable = Post.find(1)
45
- comments = commentable.comments.recent.limit(10).all
57
+ class Todo < ActiveRecord::Base
58
+ acts_as_commentable :public, :private
59
+ end
60
+
61
+ Fetch comments for a this model:
46
62
 
63
+ public_comments = Todo.find(1).public_comments
64
+ private_comments = Todo.find(1).private_comments
47
65
 
48
66
  == Credits
49
67
 
@@ -51,7 +69,7 @@ Xelipe - This plugin is heavily influenced by Acts As Taggable.
51
69
 
52
70
  == Contributors
53
71
 
54
- Jack Dempsey, Chris Eppstein, Jim Ray, Matthew Van Horn, Ole Riesenberg, ZhangJinzhu, maddox, monocle
72
+ Jack Dempsey, Chris Eppstein, Jim Ray, Matthew Van Horn, Ole Riesenberg, ZhangJinzhu, maddox, monocle, mrzor, Michael Bensoussan
55
73
 
56
74
  == More
57
75
 
@@ -6,24 +6,28 @@ module ActsAsCommentable
6
6
  # recent: Returns comments by how recently they were created (created_at DESC).
7
7
  # limit(N): Return no more than N comments.
8
8
  module Comment
9
-
9
+
10
10
  def self.included(comment_model)
11
11
  comment_model.extend Finders
12
- comment_model.scope :in_order, comment_model.order('created_at ASC')
13
- comment_model.scope :recent, comment_model.order('created_at DESC')
12
+ comment_model.scope :in_order, -> { comment_model.order('created_at ASC') }
13
+ comment_model.scope :recent, -> { comment_model.order('created_at DESC') }
14
+ end
15
+
16
+ def is_comment_type?(type)
17
+ type.to_s == role.singularize.to_s
14
18
  end
15
-
19
+
16
20
  module Finders
17
21
  # Helper class method to lookup all comments assigned
18
22
  # to all commentable types for a given user.
19
- def find_comments_by_user(user)
20
- where(["user_id = ?", user.id]).order("created_at DESC")
23
+ def find_comments_by_user(user, role = "comments")
24
+ where(["user_id = ? and role = ?", user.id, role]).order("created_at DESC")
21
25
  end
22
26
 
23
27
  # Helper class method to look up all comments for
24
28
  # commentable class name and commentable id.
25
- def find_comments_for_commentable(commentable_str, commentable_id)
26
- where(["commentable_type = ? and commentable_id = ?", commentable_str, commentable_id]).order("created_at DESC")
29
+ def find_comments_for_commentable(commentable_str, commentable_id, role = "comments")
30
+ where(["commentable_type = ? and commentable_id = ? and role = ?", commentable_str, commentable_id, role]).order("created_at DESC")
27
31
  end
28
32
 
29
33
  # Helper class method to look up a commentable object
@@ -31,7 +35,7 @@ module ActsAsCommentable
31
35
  def find_commentable(commentable_str, commentable_id)
32
36
  model = commentable_str.constantize
33
37
  model.respond_to?(:find_comments_for) ? model.find(commentable_id) : nil
34
- end
38
+ end
35
39
  end
36
40
  end
37
41
  end
@@ -10,44 +10,52 @@ module Juixe
10
10
  end
11
11
 
12
12
  module ClassMethods
13
- def acts_as_commentable(options={})
14
- has_many :comments, {:as => :commentable, :dependent => :destroy}.merge(options)
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 = self.base_class.name
26
- Comment.find_comments_for_commentable(commentable, obj.id)
27
- end
28
-
29
- # Helper class method to lookup comments for
30
- # the mixin commentable type written by a given user.
31
- # This method is NOT equivalent to Comment.find_comments_for_user
32
- def find_comments_by_user(user)
33
- commentable = self.base_class.name
34
- Comment.where(["user_id = ? and commentable_type = ?", user.id, commentable]).order("created_at DESC")
35
- end
36
- end
37
-
38
- # This module contains instance methods
39
- module InstanceMethods
40
- # Helper method to sort comments by date
41
- def comments_ordered_by_submitted
42
- Comment.find_comments_for_commentable(self.class.name, id)
43
- end
44
-
45
- # Helper method that defaults the submitted time.
46
- def add_comment(comment)
47
- comments << comment
13
+ def acts_as_commentable(*args)
14
+ comment_roles = args.to_a.flatten.compact.map(&:to_sym)
15
+
16
+ class_attribute :comment_types
17
+ self.comment_types = (comment_roles.blank? ? [:comments] : comment_roles)
18
+
19
+ options = ((args.blank? or args[0].blank?) ? {} : args[0])
20
+
21
+ if !comment_roles.blank?
22
+ comment_roles.each do |role|
23
+ has_many "#{role.to_s}_comments".to_sym,
24
+ {:class_name => "Comment",
25
+ :as => :commentable,
26
+ :dependent => :destroy,
27
+ :conditions => ["role = ?", role.to_s],
28
+ :before_add => Proc.new { |x, c| c.role = role.to_s }}
29
+ end
30
+ else
31
+ has_many :comments, {:as => :commentable, :dependent => :destroy}
32
+ end
33
+
34
+ comment_types.each do |role|
35
+ method_name = (role == :comments ? "comments" : "#{role.to_s}_comments").to_s
36
+ class_eval %{
37
+ def self.find_#{method_name}_for(obj)
38
+ commentable = self.base_class.name
39
+ Comment.find_comments_for_commentable(commentable, obj.id, "#{role.to_s}")
40
+ end
41
+
42
+ def self.find_#{method_name}_by_user(user)
43
+ commentable = self.base_class.name
44
+ Comment.where(["user_id = ? and commentable_type = ? and role = ?", user.id, commentable, "#{role.to_s}"]).order("created_at DESC")
45
+ end
46
+
47
+ def #{method_name}_ordered_by_submitted
48
+ Comment.find_comments_for_commentable(self.class.name, id, "#{role.to_s}")
49
+ end
50
+
51
+ def add_#{method_name.singularize}(comment)
52
+ comment.role = "#{role.to_s}"
53
+ #{method_name} << comment
54
+ end
55
+ }
56
+ end
48
57
  end
49
58
  end
50
-
51
59
  end
52
60
  end
53
61
  end
@@ -4,7 +4,7 @@ class Comment < ActiveRecord::Base
4
4
 
5
5
  belongs_to :commentable, :polymorphic => true
6
6
 
7
- default_scope :order => 'created_at ASC'
7
+ default_scope -> { order('created_at ASC') }
8
8
 
9
9
  # NOTE: install the acts_as_votable plugin if you
10
10
  # want user to vote on the quality of comments.
@@ -5,6 +5,7 @@ class CreateComments < ActiveRecord::Migration
5
5
  t.text :comment
6
6
  t.references :commentable, :polymorphic => true
7
7
  t.references :user
8
+ t.string :role, :default => "comments"
8
9
  t.timestamps
9
10
  end
10
11
 
metadata CHANGED
@@ -1,33 +1,24 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: acts_as_commentable
3
- version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 3
7
- - 0
8
- - 1
9
- version: 3.0.1
3
+ version: !ruby/object:Gem::Version
4
+ version: 4.0.0
5
+ prerelease:
10
6
  platform: ruby
11
- authors:
7
+ authors:
12
8
  - Cosmin Radoi, Jack Dempsey, Xelipe, Chris Eppstein
13
9
  autorequire: acts_as_commentable
14
10
  bindir: bin
15
11
  cert_chain: []
16
-
17
- date: 2010-10-28 00:00:00 -04:00
18
- default_executable:
12
+ date: 2010-03-13 00:00:00.000000000 Z
19
13
  dependencies: []
20
-
21
14
  description: Plugin/gem that provides comment functionality
22
15
  email: unknown@juixe.com
23
16
  executables: []
24
-
25
17
  extensions: []
26
-
27
- extra_rdoc_files:
18
+ extra_rdoc_files:
28
19
  - README.rdoc
29
20
  - MIT-LICENSE
30
- files:
21
+ files:
31
22
  - MIT-LICENSE
32
23
  - README.rdoc
33
24
  - lib/acts_as_commentable.rb
@@ -40,37 +31,28 @@ files:
40
31
  - tasks/acts_as_commentable_tasks.rake
41
32
  - init.rb
42
33
  - install.rb
43
- has_rdoc: true
44
34
  homepage: http://www.juixe.com/techknow/index.php/2006/06/18/acts-as-commentable-plugin/
45
35
  licenses: []
46
-
47
36
  post_install_message:
48
37
  rdoc_options: []
49
-
50
- require_paths:
38
+ require_paths:
51
39
  - lib
52
- required_ruby_version: !ruby/object:Gem::Requirement
40
+ required_ruby_version: !ruby/object:Gem::Requirement
53
41
  none: false
54
- requirements:
55
- - - ">="
56
- - !ruby/object:Gem::Version
57
- segments:
58
- - 0
59
- version: "0"
60
- required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
47
  none: false
62
- requirements:
63
- - - ">="
64
- - !ruby/object:Gem::Version
65
- segments:
66
- - 0
67
- version: "0"
48
+ requirements:
49
+ - - ! '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
68
52
  requirements: []
69
-
70
53
  rubyforge_project:
71
- rubygems_version: 1.3.7
54
+ rubygems_version: 1.8.24
72
55
  signing_key:
73
56
  specification_version: 3
74
57
  summary: Plugin/gem that provides comment functionality
75
58
  test_files: []
76
-