acts_as_commentable 2.0.1 → 4.0.1

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.
data/README.rdoc ADDED
@@ -0,0 +1,79 @@
1
+ = Acts As Commentable
2
+
3
+ Allows for comments to be added to multiple and different models.
4
+
5
+ == Installation :
6
+
7
+ Add the following line to your Gemfile
8
+
9
+ === Rails 4
10
+
11
+ gem 'acts_as_commentable'
12
+
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'
20
+
21
+
22
+ == Generator
23
+
24
+
25
+ === Rails 3+
26
+
27
+ rails g comment
28
+
29
+ === Rails 2
30
+
31
+ script/generate comment
32
+
33
+ Then migrate your database:
34
+
35
+ rake db:migrate
36
+
37
+ == Usage
38
+
39
+ Make your ActiveRecord model act as commentable:
40
+
41
+ class Post < ActiveRecord::Base
42
+ acts_as_commentable
43
+ end
44
+
45
+ Add a comment to a model instance:
46
+
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:
51
+
52
+ commentable = Post.find(1)
53
+ comments = commentable.comments.recent.limit(10).all
54
+
55
+ Add multiple type of comments to a model:
56
+
57
+ class Todo < ActiveRecord::Base
58
+ acts_as_commentable :public, :private
59
+ end
60
+
61
+ *Note:* This feature is only available from version 4.0 and above
62
+
63
+ Fetch comments for a this model:
64
+
65
+ public_comments = Todo.find(1).public_comments
66
+ private_comments = Todo.find(1).private_comments
67
+
68
+ == Credits
69
+
70
+ Xelipe - This plugin is heavily influenced by Acts As Taggable.
71
+
72
+ == Contributors
73
+
74
+ Jack Dempsey, Chris Eppstein, Jim Ray, Matthew Van Horn, Ole Riesenberg, ZhangJinzhu, maddox, monocle, mrzor, Michael Bensoussan
75
+
76
+ == More
77
+
78
+ http://www.juixe.com/techknow/index.php/2006/06/18/acts-as-commentable-plugin/
79
+ http://www.juixe.com/projects/acts_as_commentable
@@ -6,30 +6,35 @@ 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.reorder('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
30
34
  # given the commentable class name and id
31
35
  def find_commentable(commentable_str, commentable_id)
32
- commentable_str.constantize.find(commentable_id)
36
+ model = commentable_str.constantize
37
+ model.respond_to?(:find_comments_for) ? model.find(commentable_id) : nil
33
38
  end
34
39
  end
35
40
  end
@@ -10,44 +10,53 @@ module Juixe
10
10
  end
11
11
 
12
12
  module ClassMethods
13
- def acts_as_commentable
14
- has_many :comments, :as => :commentable, :dependent => :destroy
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
- 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 = ActiveRecord::Base.send(:class_name_of_active_record_descendant, self).to_s
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
+ -> { where(role: role.to_s) },
25
+ {:class_name => "Comment",
26
+ :as => :commentable,
27
+ :dependent => :destroy,
28
+ :before_add => Proc.new { |x, c| c.role = role.to_s }}
29
+ end
30
+ has_many :all_comments, {:as => :commentable, :dependent => :destroy, class_name: "Comment"}
31
+ else
32
+ has_many :comments, {:as => :commentable, :dependent => :destroy}
33
+ end
34
+
35
+ comment_types.each do |role|
36
+ method_name = (role == :comments ? "comments" : "#{role.to_s}_comments").to_s
37
+ class_eval %{
38
+ def self.find_#{method_name}_for(obj)
39
+ commentable = self.base_class.name
40
+ Comment.find_comments_for_commentable(commentable, obj.id, "#{role.to_s}")
41
+ end
42
+
43
+ def self.find_#{method_name}_by_user(user)
44
+ commentable = self.base_class.name
45
+ Comment.where(["user_id = ? and commentable_type = ? and role = ?", user.id, commentable, "#{role.to_s}"]).order("created_at DESC")
46
+ end
47
+
48
+ def #{method_name}_ordered_by_submitted
49
+ Comment.find_comments_for_commentable(self.class.name, id, "#{role.to_s}").order("created_at")
50
+ end
51
+
52
+ def add_#{method_name.singularize}(comment)
53
+ comment.role = "#{role.to_s}"
54
+ #{method_name} << comment
55
+ end
56
+ }
57
+ end
48
58
  end
49
59
  end
50
-
51
60
  end
52
61
  end
53
62
  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.
@@ -2,9 +2,10 @@ class CreateComments < ActiveRecord::Migration
2
2
  def self.up
3
3
  create_table :comments do |t|
4
4
  t.string :title, :limit => 50, :default => ""
5
- t.text :comment, :default => ""
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,35 +1,26 @@
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
- - 2
7
- - 0
8
- - 1
9
- version: 2.0.1
3
+ version: !ruby/object:Gem::Version
4
+ version: 4.0.1
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-09-05 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:
28
- - README
18
+ extra_rdoc_files:
19
+ - README.rdoc
29
20
  - MIT-LICENSE
30
- files:
21
+ files:
31
22
  - MIT-LICENSE
32
- - README
23
+ - README.rdoc
33
24
  - lib/acts_as_commentable.rb
34
25
  - lib/comment_methods.rb
35
26
  - lib/commentable_methods.rb
@@ -37,40 +28,30 @@ files:
37
28
  - lib/generators/comment/templates/comment.rb
38
29
  - lib/generators/comment/templates/create_comments.rb
39
30
  - lib/generators/comment/USEGA
40
- - tasks/acts_as_commentable_tasks.rake
41
31
  - init.rb
42
32
  - install.rb
43
- has_rdoc: true
44
33
  homepage: http://www.juixe.com/techknow/index.php/2006/06/18/acts-as-commentable-plugin/
45
34
  licenses: []
46
-
47
35
  post_install_message:
48
36
  rdoc_options: []
49
-
50
- require_paths:
37
+ require_paths:
51
38
  - lib
52
- required_ruby_version: !ruby/object:Gem::Requirement
39
+ required_ruby_version: !ruby/object:Gem::Requirement
53
40
  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
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
46
  none: false
62
- requirements:
63
- - - ">="
64
- - !ruby/object:Gem::Version
65
- segments:
66
- - 0
67
- version: "0"
47
+ requirements:
48
+ - - ! '>='
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
68
51
  requirements: []
69
-
70
52
  rubyforge_project:
71
- rubygems_version: 1.3.7
53
+ rubygems_version: 1.8.24
72
54
  signing_key:
73
55
  specification_version: 3
74
56
  summary: Plugin/gem that provides comment functionality
75
57
  test_files: []
76
-
data/README DELETED
@@ -1,73 +0,0 @@
1
- Acts As Commentable
2
- =================
3
-
4
- Allows for comments to be added to multiple and different models.
5
-
6
- == Resources
7
-
8
- Install
9
-
10
- Rails
11
-
12
- * To install as a plugin:
13
-
14
- script/plugin install http://github.com/jackdempsey/acts_as_commentable.git
15
-
16
- * To install as a gem
17
- sudo gem install
18
-
19
- Merb/Rails
20
-
21
- * To install as a gem:
22
- Run the following if you haven't already:
23
- gem sources -a http://gems.github.com
24
-
25
- Install the gem(s):
26
- sudo gem install jackdempsey-acts_as_commentable
27
-
28
- add the folloowing line to your environment.rb
29
- config.gem 'jackdempsey-acts_as_commentable', :lib => 'acts_as_commentable', :source => "http://gems.github.com"
30
-
31
-
32
- Generate your comment model:
33
-
34
- script/generate comment
35
-
36
- Then migrate your database:
37
-
38
- rake db:migrate
39
-
40
- == Usage
41
- Merb Users:
42
- * add 'dependency "acts_as_commentable"' to your init.rb or dependencies.rb if using merb-stack
43
-
44
- * Make your ActiveRecord model act as commentable.
45
-
46
- class Model < ActiveRecord::Base
47
- acts_as_commentable
48
- end
49
-
50
- * Add a comment to a model instance
51
-
52
- commentable = Model.create
53
- commentable.comments.create(:title => "First comment.", :comment => "This is the first comment.")
54
-
55
- * Fetch comments for a commentable model:
56
-
57
- commentable = Model.find(1)
58
- comments = commentable.comments.recent.limit(10).all
59
-
60
- # Following doesn't work/make sense to me. Leaving for historical sake -- Jack
61
- # * Each comment reference commentable object
62
- #
63
- # model = Model.find(1)
64
- # model.comments.get(0).commentable == model
65
-
66
- == Credits
67
-
68
- Xelipe - This plugin is heavily influenced by Acts As Taggable.
69
-
70
- == More
71
-
72
- http://www.juixe.com/techknow/index.php/2006/06/18/acts-as-commentable-plugin/
73
- http://www.juixe.com/projects/acts_as_commentable
@@ -1,4 +0,0 @@
1
- # desc "Explaining what the task does"
2
- # task :acts_as_commentable do
3
- # # Task goes here
4
- # end