digitalmoksha-acts_as_commentable 6.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +101 -0
- data/init.rb +1 -0
- data/install.rb +2 -0
- data/lib/acts_as_commentable.rb +2 -0
- data/lib/comment_methods.rb +41 -0
- data/lib/commentable_methods.rb +131 -0
- data/lib/generators/comment/USAGE +6 -0
- data/lib/generators/comment/comment_generator.rb +18 -0
- data/lib/generators/comment/templates/comment.rb +15 -0
- data/lib/generators/comment/templates/create_comments.rb +20 -0
- metadata +56 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 56dc4bcd0dd16afe10a81b998bf36267c8a8aedab7069d8e3d7e1d9d19b64f9d
|
4
|
+
data.tar.gz: 22fa27c7b332d124e602ca69a2703691ce32f8a91741c0ae014d4869631e2073
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e093162809f535495672ba6495e35fcd321378245295028777687a4c104eadb19c4c3a4be428ad8f093bacb64a7ceae25a57960ee2bc24cbc34320984f13a393
|
7
|
+
data.tar.gz: c25216c0cb22b50f57680e951b2c9ac955e274b77832de05e8fa7738fe6d0d0e4a28497fd2a5bc52a2defb90d3cb3cf7c06997303f2e2996c246fc1bbeafd34c
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2006 Cosmin Radoi
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
= Acts As Commentable
|
2
|
+
|
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.
|
6
|
+
|
7
|
+
|
8
|
+
== Installation :
|
9
|
+
|
10
|
+
Add the following line to your Gemfile
|
11
|
+
|
12
|
+
=== Rails 4
|
13
|
+
|
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'
|
23
|
+
|
24
|
+
|
25
|
+
== Generator
|
26
|
+
|
27
|
+
|
28
|
+
=== Rails 3+
|
29
|
+
|
30
|
+
rails g comment
|
31
|
+
|
32
|
+
=== Rails 2
|
33
|
+
|
34
|
+
script/generate comment
|
35
|
+
|
36
|
+
Then migrate your database to add the comments model:
|
37
|
+
|
38
|
+
rake db:migrate
|
39
|
+
|
40
|
+
== Usage
|
41
|
+
|
42
|
+
Make the desired ActiveRecord model act as commentable:
|
43
|
+
|
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:
|
49
|
+
|
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:
|
58
|
+
|
59
|
+
commentable = Post.find(1)
|
60
|
+
comments = commentable.comments.recent.limit(10).all
|
61
|
+
|
62
|
+
You can also add different types of comments to a model:
|
63
|
+
|
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
|
88
|
+
|
89
|
+
|
90
|
+
== Credits
|
91
|
+
|
92
|
+
Xelipe - This plugin is heavily influenced by Acts As Taggable.
|
93
|
+
|
94
|
+
== Contributors
|
95
|
+
|
96
|
+
Jack Dempsey, Chris Eppstein, Jim Ray, Matthew Van Horn, Ole Riesenberg, ZhangJinzhu, maddox, monocle, mrzor, Michael Bensoussan
|
97
|
+
|
98
|
+
== More
|
99
|
+
|
100
|
+
http://www.juixe.com/techknow/index.php/2006/06/18/acts-as-commentable-plugin/
|
101
|
+
http://www.juixe.com/projects/acts_as_commentable
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'rails', 'init')
|
data/install.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
module ActsAsCommentable
|
2
|
+
# including this module into your Comment model will give you finders and named scopes
|
3
|
+
# useful for working with Comments.
|
4
|
+
# The named scopes are:
|
5
|
+
# in_order: Returns comments in the order they were created (created_at ASC).
|
6
|
+
# recent: Returns comments by how recently they were created (created_at DESC).
|
7
|
+
# limit(N): Return no more than N comments.
|
8
|
+
module Comment
|
9
|
+
|
10
|
+
def self.included(comment_model)
|
11
|
+
comment_model.extend Finders
|
12
|
+
comment_model.scope :in_order, -> { order('created_at ASC') }
|
13
|
+
comment_model.scope :recent, -> { reorder('created_at DESC') }
|
14
|
+
end
|
15
|
+
|
16
|
+
def is_comment_type?(type)
|
17
|
+
type.to_s == role.singularize.to_s
|
18
|
+
end
|
19
|
+
|
20
|
+
module Finders
|
21
|
+
# Helper class method to lookup all comments assigned
|
22
|
+
# to all commentable types for a given user.
|
23
|
+
def find_comments_by_user(user, role = "comments")
|
24
|
+
where(["user_id = ? and role = ?", user.id, role]).order("created_at DESC")
|
25
|
+
end
|
26
|
+
|
27
|
+
# Helper class method to look up all comments for
|
28
|
+
# commentable class name and commentable id.
|
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")
|
31
|
+
end
|
32
|
+
|
33
|
+
# Helper class method to look up a commentable object
|
34
|
+
# given the commentable class name and id
|
35
|
+
def find_commentable(commentable_str, commentable_id)
|
36
|
+
model = commentable_str.constantize
|
37
|
+
model.respond_to?(:find_comments_for) ? model.find(commentable_id) : nil
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,131 @@
|
|
1
|
+
require 'active_record'
|
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 HelperMethods
|
13
|
+
private
|
14
|
+
def define_role_based_inflection(role)
|
15
|
+
send("define_role_based_inflection_#{Rails.version.first}", role)
|
16
|
+
end
|
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 })
|
21
|
+
end
|
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)
|
27
|
+
end
|
28
|
+
|
29
|
+
def define_role_based_inflection_5(role)
|
30
|
+
has_many "#{role.to_s}_comments".to_sym,
|
31
|
+
-> { where(role: role.to_s) },
|
32
|
+
has_many_options(role)
|
33
|
+
end
|
34
|
+
|
35
|
+
def define_role_based_inflection_6(role)
|
36
|
+
if RUBY_VERSION.first >= '3'
|
37
|
+
has_many "#{role.to_s}_comments".to_sym,
|
38
|
+
-> { where(role: role.to_s) },
|
39
|
+
**has_many_options(role)
|
40
|
+
else
|
41
|
+
has_many "#{role.to_s}_comments".to_sym,
|
42
|
+
-> { where(role: role.to_s) },
|
43
|
+
has_many_options(role)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def define_role_based_inflection_7(role)
|
48
|
+
if RUBY_VERSION.first >= '3'
|
49
|
+
has_many "#{role.to_s}_comments".to_sym,
|
50
|
+
-> { where(role: role.to_s) },
|
51
|
+
**has_many_options(role)
|
52
|
+
else
|
53
|
+
has_many "#{role.to_s}_comments".to_sym,
|
54
|
+
-> { where(role: role.to_s) },
|
55
|
+
has_many_options(role)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
|
60
|
+
def has_many_options(role)
|
61
|
+
{:class_name => "Comment",
|
62
|
+
:as => :commentable,
|
63
|
+
:dependent => :destroy,
|
64
|
+
:before_add => Proc.new { |x, c| c.role = role.to_s }
|
65
|
+
}
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
module ClassMethods
|
70
|
+
include HelperMethods
|
71
|
+
|
72
|
+
def acts_as_commentable(*args)
|
73
|
+
options = args.to_a.flatten.compact.partition{ |opt| opt.kind_of? Hash }
|
74
|
+
comment_roles = options.last.blank? ? nil : options.last.flatten.compact.map(&:to_sym)
|
75
|
+
|
76
|
+
join_options = options.first.blank? ? [{}] : options.first
|
77
|
+
throw 'Only one set of options can be supplied for the join' if join_options.length > 1
|
78
|
+
join_options = join_options.first
|
79
|
+
|
80
|
+
class_attribute :comment_types
|
81
|
+
self.comment_types = (comment_roles.blank? ? [:comments] : comment_roles)
|
82
|
+
|
83
|
+
if !comment_roles.blank?
|
84
|
+
comment_roles.each do |role|
|
85
|
+
define_role_based_inflection(role)
|
86
|
+
accepts_nested_attributes_for "#{role.to_s}_comments".to_sym, allow_destroy: true, reject_if: proc { |attributes| attributes['comment'].blank? }
|
87
|
+
end
|
88
|
+
if RUBY_VERSION.first >= '3'
|
89
|
+
has_many :all_comments, **{ :as => :commentable, :dependent => :destroy, class_name: 'Comment' }.merge(join_options)
|
90
|
+
else
|
91
|
+
has_many :all_comments, { :as => :commentable, :dependent => :destroy, class_name: 'Comment' }.merge(join_options)
|
92
|
+
end
|
93
|
+
else
|
94
|
+
if RUBY_VERSION.first >= '3'
|
95
|
+
has_many :comments, **{:as => :commentable, :dependent => :destroy}.merge(join_options)
|
96
|
+
else
|
97
|
+
has_many :comments, {:as => :commentable, :dependent => :destroy}.merge(join_options)
|
98
|
+
end
|
99
|
+
accepts_nested_attributes_for :comments, allow_destroy: true, reject_if: proc { |attributes| attributes['comment'].blank? }
|
100
|
+
end
|
101
|
+
|
102
|
+
comment_types.each do |role|
|
103
|
+
method_name = (role == :comments ? "comments" : "#{role.to_s}_comments").to_s
|
104
|
+
class_eval %{
|
105
|
+
def self.find_#{method_name}_for(obj)
|
106
|
+
commentable = self.base_class.name
|
107
|
+
Comment.find_comments_for_commentable(commentable, obj.id, "#{role.to_s}")
|
108
|
+
end
|
109
|
+
|
110
|
+
def self.find_#{method_name}_by_user(user)
|
111
|
+
commentable = self.base_class.name
|
112
|
+
Comment.where(["user_id = ? and commentable_type = ? and role = ?", user.id, commentable, "#{role.to_s}"]).order("created_at DESC")
|
113
|
+
end
|
114
|
+
|
115
|
+
def #{method_name}_ordered_by_submitted
|
116
|
+
Comment.find_comments_for_commentable(self.class.name, id, "#{role.to_s}").order("created_at")
|
117
|
+
end
|
118
|
+
|
119
|
+
def add_#{method_name.singularize}(comment)
|
120
|
+
comment.role = "#{role.to_s}"
|
121
|
+
#{method_name} << comment
|
122
|
+
end
|
123
|
+
}
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
ActiveRecord::Base.send(:include, Juixe::Acts::Commentable)
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'rails/generators/migration'
|
2
|
+
|
3
|
+
class CommentGenerator < Rails::Generators::Base
|
4
|
+
include Rails::Generators::Migration
|
5
|
+
|
6
|
+
def self.source_root
|
7
|
+
@_acts_as_commentable_source_root ||= File.expand_path("../templates", __FILE__)
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.next_migration_number(path)
|
11
|
+
Time.now.utc.strftime("%Y%m%d%H%M%S")
|
12
|
+
end
|
13
|
+
|
14
|
+
def create_model_file
|
15
|
+
template "comment.rb", "app/models/comment.rb"
|
16
|
+
migration_template "create_comments.rb", "db/migrate/create_comments.rb"
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
class Comment < ActiveRecord::Base
|
2
|
+
|
3
|
+
include ActsAsCommentable::Comment
|
4
|
+
|
5
|
+
belongs_to :commentable, :polymorphic => true
|
6
|
+
|
7
|
+
default_scope -> { order('created_at ASC') }
|
8
|
+
|
9
|
+
# NOTE: install the acts_as_votable plugin if you
|
10
|
+
# want user to vote on the quality of comments.
|
11
|
+
#acts_as_votable
|
12
|
+
|
13
|
+
# NOTE: Comments belong to a user
|
14
|
+
belongs_to :user
|
15
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
class CreateComments < ActiveRecord::Migration[6.0]
|
2
|
+
def self.up
|
3
|
+
create_table :comments do |t|
|
4
|
+
t.string :title, :limit => 50, :default => ""
|
5
|
+
t.text :comment
|
6
|
+
t.references :commentable, :polymorphic => true
|
7
|
+
t.references :user
|
8
|
+
t.string :role, :default => "comments"
|
9
|
+
t.timestamps
|
10
|
+
end
|
11
|
+
|
12
|
+
add_index :comments, :commentable_type
|
13
|
+
add_index :comments, :commentable_id
|
14
|
+
add_index :comments, :user_id
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.down
|
18
|
+
drop_table :comments
|
19
|
+
end
|
20
|
+
end
|
metadata
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: digitalmoksha-acts_as_commentable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 6.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Cosmin Radoi, Jack Dempsey, Xelipe, Chris Eppstein
|
8
|
+
- Diego Charles
|
9
|
+
autorequire: acts_as_commentable
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2023-09-23 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Plugin/gem that provides comment functionality
|
15
|
+
email: unknown@juixe.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files:
|
19
|
+
- README.rdoc
|
20
|
+
- MIT-LICENSE
|
21
|
+
files:
|
22
|
+
- MIT-LICENSE
|
23
|
+
- README.rdoc
|
24
|
+
- init.rb
|
25
|
+
- install.rb
|
26
|
+
- lib/acts_as_commentable.rb
|
27
|
+
- lib/comment_methods.rb
|
28
|
+
- lib/commentable_methods.rb
|
29
|
+
- lib/generators/comment/USAGE
|
30
|
+
- lib/generators/comment/comment_generator.rb
|
31
|
+
- lib/generators/comment/templates/comment.rb
|
32
|
+
- lib/generators/comment/templates/create_comments.rb
|
33
|
+
homepage: https://github.com/digitalmoksha/acts_as_commentable
|
34
|
+
licenses:
|
35
|
+
- MIT
|
36
|
+
metadata: {}
|
37
|
+
post_install_message:
|
38
|
+
rdoc_options: []
|
39
|
+
require_paths:
|
40
|
+
- lib
|
41
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: '0'
|
51
|
+
requirements: []
|
52
|
+
rubygems_version: 3.4.10
|
53
|
+
signing_key:
|
54
|
+
specification_version: 3
|
55
|
+
summary: Plugin/gem that provides comment functionality
|
56
|
+
test_files: []
|