acts_as_commentable 3.0.1 → 4.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.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ ZDQyZmE5ZjZmMmNhMDE1MTk2YzcwOTA1ZGE2N2ViMjZlNTE4NzQ3NQ==
5
+ data.tar.gz: !binary |-
6
+ MDRjMzI1NTNkMjVkMDgxZTk5MzM5ZTNiMzU0ODlkZmM3MmFjOTRkYQ==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ ZjI5OWEyZmJkNjJjMTRhNmEzZGY2ODMwNTdjOGFmNjNiNGYxNjI3Y2EwODhj
10
+ YTVmZDkzOTRlMjcxMDU2NjVlMTQzZjdkYTE4MWQ0NTYxNGMyZDM2ZjM0MzQ3
11
+ MDdmY2Y4ZjBiZDZlY2UzNjlhM2VlMjdkNjNlNTZlYzg2NzY3YjM=
12
+ data.tar.gz: !binary |-
13
+ YWI1MzQ5NTQyZmYzZGNlMjFmOGM1ODIwMjA2ZGExYTI1NmI4NDNmOTU3MjQ0
14
+ NjE3Yzc3MDY3NDE3ZjI4ZTAyNjdlYWIyM2ZhYmFhMmYwNjdhM2E5N2Q1YjMw
15
+ OTBlZDA1NWE5ZjIzOGQwZmU5MGRmM2Q1ZDMzMzk1MmI2M2I1NzA=
data/README.rdoc CHANGED
@@ -1,48 +1,90 @@
1
- Acts As Commentable
2
- =================
1
+ = Acts As Commentable
3
2
 
4
- Allows for comments to be added to multiple and different models.
3
+ Provides a single Comment model that can be attached to any model(s) within your app. It creates
4
+ a Comment model and handles the plumbing between that model and any models that you declare to be
5
+ commentable models.
5
6
 
6
- == Resources
7
7
 
8
- Install
8
+ == Installation :
9
9
 
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"
10
+ Add the following line to your Gemfile
14
11
 
12
+ === Rails 4
15
13
 
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
14
+ gem 'acts_as_commentable'
15
+
16
+ === Rails 3
17
+
18
+ gem 'acts_as_commentable', '3.0.1'
19
+
20
+ === Rails 2
21
+
22
+ gem 'acts_as_commentable', git: 'git@github.com:jackdempsey/acts_as_commentable.git', branch: '2.x'
19
23
 
20
24
 
21
- Generate your comment model:
25
+ == Generator
26
+
22
27
 
23
- script/generate comment
28
+ === Rails 3+
24
29
 
25
- Then migrate your database:
30
+ rails g comment
26
31
 
27
- rake db:migrate
32
+ === Rails 2
33
+
34
+ script/generate comment
35
+
36
+ Then migrate your database to add the comments model:
37
+
38
+ rake db:migrate
28
39
 
29
40
  == Usage
41
+
42
+ Make the desired ActiveRecord model act as commentable:
30
43
 
31
- * Make your ActiveRecord model act as commentable.
32
-
33
- class Post < ActiveRecord::Base
34
- acts_as_commentable
35
- end
44
+ class Post < ActiveRecord::Base
45
+ acts_as_commentable
46
+ end
47
+
48
+ Add a comment to a model instance by adding the following to the controller:
36
49
 
37
- * Add a comment to a model instance
50
+ commentable = Post.create
51
+ comment = commentable.comments.create
52
+ comment.title = "First comment."
53
+ comment.comment = "This is the first comment."
54
+ comment.save
55
+
56
+
57
+ Fetch comments for the commentable model by adding the following to the view:
38
58
 
39
- commentable = Post.create
40
- commentable.comments.create(:title => "First comment.", :comment => "This is the first comment.")
59
+ commentable = Post.find(1)
60
+ comments = commentable.comments.recent.limit(10).all
41
61
 
42
- * Fetch comments for a commentable model:
62
+ You can also add different types of comments to a model:
43
63
 
44
- commentable = Post.find(1)
45
- comments = commentable.comments.recent.limit(10).all
64
+ class Todo < ActiveRecord::Base
65
+ acts_as_commentable :public, :private
66
+ end
67
+
68
+ *Note:* This feature is only available from version 4.0 and above
69
+
70
+ To fetch the different types of comments for this model:
71
+
72
+ public_comments = Todo.find(1).public_comments
73
+ private_comments = Todo.find(1).private_comments
74
+
75
+
76
+ By default, `acts_as_commentable` will assume you are using a `Comment` model.
77
+ To change this, or change any other join options, pass in an options hash:
78
+
79
+ class Todo < ActiveRecord::Base
80
+ acts_as_commentable { class_name: 'MyComment' }
81
+ end
82
+
83
+ This also works in conjunction with comment types:
84
+
85
+ class Todo < ActiveRecord::Base
86
+ acts_as_commentable :public, :private, { class_name: 'MyComment' }
87
+ end
46
88
 
47
89
 
48
90
  == Credits
@@ -51,7 +93,7 @@ Xelipe - This plugin is heavily influenced by Acts As Taggable.
51
93
 
52
94
  == Contributors
53
95
 
54
- Jack Dempsey, Chris Eppstein, Jim Ray, Matthew Van Horn, Ole Riesenberg, ZhangJinzhu, maddox, monocle
96
+ Jack Dempsey, Chris Eppstein, Jim Ray, Matthew Van Horn, Ole Riesenberg, ZhangJinzhu, maddox, monocle, mrzor, Michael Bensoussan
55
97
 
56
98
  == More
57
99
 
@@ -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.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
@@ -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
@@ -6,48 +6,83 @@ module Juixe
6
6
  module Commentable #:nodoc:
7
7
 
8
8
  def self.included(base)
9
- base.extend ClassMethods
9
+ base.extend ClassMethods
10
10
  end
11
11
 
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
12
+ module HelperMethods
13
+ private
14
+ def define_role_based_inflection(role)
15
+ send("define_role_based_inflection_#{Rails.version.first}", role)
17
16
  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)
17
+
18
+ def define_role_based_inflection_3(role)
19
+ has_many "#{role.to_s}_comments".to_sym,
20
+ has_many_options(role).merge(:conditions => { role: role.to_s })
27
21
  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")
22
+
23
+ def define_role_based_inflection_4(role)
24
+ has_many "#{role.to_s}_comments".to_sym,
25
+ -> { where(role: role.to_s) },
26
+ has_many_options(role)
35
27
  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)
28
+
29
+ def has_many_options(role)
30
+ {:class_name => "Comment",
31
+ :as => :commentable,
32
+ :dependent => :destroy,
33
+ :before_add => Proc.new { |x, c| c.role = role.to_s }
34
+ }
43
35
  end
44
-
45
- # Helper method that defaults the submitted time.
46
- def add_comment(comment)
47
- comments << comment
36
+ end
37
+
38
+ module ClassMethods
39
+ include HelperMethods
40
+
41
+ def acts_as_commentable(*args)
42
+ options = args.to_a.flatten.compact.partition{ |opt| opt.kind_of? Hash }
43
+ comment_roles = options.last.blank? ? nil : options.last.flatten.compact.map(&:to_sym)
44
+
45
+ join_options = options.first.blank? ? [{}] : options.first
46
+ throw 'Only one set of options can be supplied for the join' if join_options.length > 1
47
+ join_options = join_options.first
48
+
49
+ class_attribute :comment_types
50
+ self.comment_types = (comment_roles.blank? ? [:comments] : comment_roles)
51
+
52
+ if !comment_roles.blank?
53
+ comment_roles.each do |role|
54
+ define_role_based_inflection(role)
55
+ end
56
+ has_many :all_comments, { :as => :commentable, :dependent => :destroy, class_name: 'Comment' }.merge(join_options)
57
+ else
58
+ has_many :comments, {:as => :commentable, :dependent => :destroy}.merge(join_options)
59
+ end
60
+
61
+ comment_types.each do |role|
62
+ method_name = (role == :comments ? "comments" : "#{role.to_s}_comments").to_s
63
+ class_eval %{
64
+ def self.find_#{method_name}_for(obj)
65
+ commentable = self.base_class.name
66
+ Comment.find_comments_for_commentable(commentable, obj.id, "#{role.to_s}")
67
+ end
68
+
69
+ def self.find_#{method_name}_by_user(user)
70
+ commentable = self.base_class.name
71
+ Comment.where(["user_id = ? and commentable_type = ? and role = ?", user.id, commentable, "#{role.to_s}"]).order("created_at DESC")
72
+ end
73
+
74
+ def #{method_name}_ordered_by_submitted
75
+ Comment.find_comments_for_commentable(self.class.name, id, "#{role.to_s}").order("created_at")
76
+ end
77
+
78
+ def add_#{method_name.singularize}(comment)
79
+ comment.role = "#{role.to_s}"
80
+ #{method_name} << comment
81
+ end
82
+ }
83
+ end
48
84
  end
49
85
  end
50
-
51
86
  end
52
87
  end
53
88
  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,76 +1,55 @@
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.2
10
5
  platform: ruby
11
- authors:
6
+ authors:
12
7
  - Cosmin Radoi, Jack Dempsey, Xelipe, Chris Eppstein
13
8
  autorequire: acts_as_commentable
14
9
  bindir: bin
15
10
  cert_chain: []
16
-
17
- date: 2010-10-28 00:00:00 -04:00
18
- default_executable:
11
+ date: 2014-10-02 00:00:00.000000000 Z
19
12
  dependencies: []
20
-
21
13
  description: Plugin/gem that provides comment functionality
22
14
  email: unknown@juixe.com
23
15
  executables: []
24
-
25
16
  extensions: []
26
-
27
- extra_rdoc_files:
17
+ extra_rdoc_files:
28
18
  - README.rdoc
29
19
  - MIT-LICENSE
30
- files:
20
+ files:
31
21
  - MIT-LICENSE
32
22
  - README.rdoc
23
+ - init.rb
24
+ - install.rb
33
25
  - lib/acts_as_commentable.rb
34
26
  - lib/comment_methods.rb
35
27
  - lib/commentable_methods.rb
28
+ - lib/generators/comment/USAGE
36
29
  - lib/generators/comment/comment_generator.rb
37
30
  - lib/generators/comment/templates/comment.rb
38
31
  - lib/generators/comment/templates/create_comments.rb
39
- - lib/generators/comment/USEGA
40
- - tasks/acts_as_commentable_tasks.rake
41
- - init.rb
42
- - install.rb
43
- has_rdoc: true
44
32
  homepage: http://www.juixe.com/techknow/index.php/2006/06/18/acts-as-commentable-plugin/
45
33
  licenses: []
46
-
34
+ metadata: {}
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
53
- 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
61
- none: false
62
- requirements:
63
- - - ">="
64
- - !ruby/object:Gem::Version
65
- segments:
66
- - 0
67
- version: "0"
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
68
49
  requirements: []
69
-
70
50
  rubyforge_project:
71
- rubygems_version: 1.3.7
51
+ rubygems_version: 2.2.2
72
52
  signing_key:
73
53
  specification_version: 3
74
54
  summary: Plugin/gem that provides comment functionality
75
55
  test_files: []
76
-
@@ -1,4 +0,0 @@
1
- # desc "Explaining what the task does"
2
- # task :acts_as_commentable do
3
- # # Task goes here
4
- # end
File without changes