jackdempsey-acts_as_commentable 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README +63 -0
- data/generators/comment/comment_generator.rb +13 -0
- data/generators/comment/templates/comment.rb +14 -0
- data/generators/comment/templates/create_comments.rb +19 -0
- data/init.rb +4 -0
- data/install.rb +2 -0
- data/lib/acts_as_commentable.rb +66 -0
- data/lib/comment_methods.rb +43 -0
- data/tasks/acts_as_commentable_tasks.rake +4 -0
- metadata +65 -0
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
ADDED
@@ -0,0 +1,63 @@
|
|
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
|
+
Merb/Rails
|
17
|
+
|
18
|
+
* To install as a gem:
|
19
|
+
|
20
|
+
rake install
|
21
|
+
|
22
|
+
Generate your comment model:
|
23
|
+
|
24
|
+
script/generate comment
|
25
|
+
|
26
|
+
Then migrate your database:
|
27
|
+
|
28
|
+
rake db:migrate
|
29
|
+
|
30
|
+
== Usage
|
31
|
+
Merb Users:
|
32
|
+
* add 'dependency "acts_as_commentable"' to your init.rb or dependencies.rb if using merb-stack
|
33
|
+
|
34
|
+
* Make your ActiveRecord model act as commentable.
|
35
|
+
|
36
|
+
class Model < ActiveRecord::Base
|
37
|
+
acts_as_commentable
|
38
|
+
end
|
39
|
+
|
40
|
+
* Add a comment to a model instance
|
41
|
+
|
42
|
+
commentable = Model.create
|
43
|
+
commentable.comments.create(:title => "First comment.", :comment => "This is the first comment.")
|
44
|
+
|
45
|
+
* Fetch comments for a commentable model:
|
46
|
+
|
47
|
+
commentable = Model.find(1)
|
48
|
+
comments = commentable.comments.recent.limit(10).all
|
49
|
+
|
50
|
+
# Following doesn't work/make sense to me. Leaving for historical sake -- Jack
|
51
|
+
# * Each comment reference commentable object
|
52
|
+
#
|
53
|
+
# model = Model.find(1)
|
54
|
+
# model.comments.get(0).commentable == model
|
55
|
+
|
56
|
+
== Credits
|
57
|
+
|
58
|
+
Xelipe - This plugin is heavily influenced by Acts As Taggable.
|
59
|
+
|
60
|
+
== More
|
61
|
+
|
62
|
+
http://www.juixe.com/techknow/index.php/2006/06/18/acts-as-commentable-plugin/
|
63
|
+
http://www.juixe.com/projects/acts_as_commentable
|
@@ -0,0 +1,13 @@
|
|
1
|
+
class CommentGenerator < Rails::Generator::Base
|
2
|
+
def manifest
|
3
|
+
record do |m|
|
4
|
+
m.directory 'app/models'
|
5
|
+
m.file 'comment.rb', 'app/models/comment.rb'
|
6
|
+
m.migration_template "create_comments.rb", "db/migrate"
|
7
|
+
end
|
8
|
+
end
|
9
|
+
# ick what a hack.
|
10
|
+
def file_name
|
11
|
+
"create_comments"
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
class Comment < ActiveRecord::Base
|
2
|
+
|
3
|
+
include ActsAsCommentable::Comment
|
4
|
+
|
5
|
+
belongs_to :commentable, :polymorphic => true
|
6
|
+
|
7
|
+
# NOTE: install the acts_as_votable plugin if you
|
8
|
+
# want user to vote on the quality of comments.
|
9
|
+
#acts_as_voteable
|
10
|
+
|
11
|
+
# NOTE: Comments belong to a user
|
12
|
+
belongs_to :user
|
13
|
+
|
14
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
class CreateComments < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
create_table :comments do |t|
|
4
|
+
t.string :title, :limit => 50, :default => ""
|
5
|
+
t.text :comment, :default => ""
|
6
|
+
t.references :commentable, :polymorphic => true
|
7
|
+
t.references :user
|
8
|
+
t.timestamps
|
9
|
+
end
|
10
|
+
|
11
|
+
add_index :comments, :commentable_type
|
12
|
+
add_index :comments, :commentable_id
|
13
|
+
add_index :comments, :user_id
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.down
|
17
|
+
drop_table :comments
|
18
|
+
end
|
19
|
+
end
|
data/init.rb
ADDED
data/install.rb
ADDED
@@ -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)
|
@@ -0,0 +1,43 @@
|
|
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.named_scope :in_order, {:order => 'created_at ASC'}
|
13
|
+
comment_model.named_scope :recent, {:order => "created_at DESC"}
|
14
|
+
comment_model.named_scope :limit, lambda {|limit| {:limit => limit}}
|
15
|
+
end
|
16
|
+
|
17
|
+
module Finders
|
18
|
+
# Helper class method to lookup all comments assigned
|
19
|
+
# to all commentable types for a given user.
|
20
|
+
def find_comments_by_user(user)
|
21
|
+
find(:all,
|
22
|
+
:conditions => ["user_id = ?", user.id],
|
23
|
+
:order => "created_at DESC"
|
24
|
+
)
|
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)
|
30
|
+
find(:all,
|
31
|
+
:conditions => ["commentable_type = ? and commentable_id = ?", commentable_str, commentable_id],
|
32
|
+
:order => "created_at DESC"
|
33
|
+
)
|
34
|
+
end
|
35
|
+
|
36
|
+
# Helper class method to look up a commentable object
|
37
|
+
# given the commentable class name and id
|
38
|
+
def find_commentable(commentable_str, commentable_id)
|
39
|
+
commentable_str.constantize.find(commentable_id)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
metadata
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jackdempsey-acts_as_commentable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 2.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Cosmin Radoi, Jack Dempsey, Xelipe, Chris Eppstein
|
8
|
+
autorequire: acts_as_commentable
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-05-22 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Plugin/gem that provides comment functionality
|
17
|
+
email: unknown@juixe.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README
|
24
|
+
- MIT-LICENSE
|
25
|
+
files:
|
26
|
+
- MIT-LICENSE
|
27
|
+
- README
|
28
|
+
- generators/comment
|
29
|
+
- generators/comment/comment_generator.rb
|
30
|
+
- generators/comment/templates
|
31
|
+
- generators/comment/templates/comment.rb
|
32
|
+
- generators/comment/templates/create_comments.rb
|
33
|
+
- lib/acts_as_commentable.rb
|
34
|
+
- lib/comment_methods.rb
|
35
|
+
- tasks/acts_as_commentable_tasks.rake
|
36
|
+
- init.rb
|
37
|
+
- install.rb
|
38
|
+
has_rdoc: false
|
39
|
+
homepage: http://www.juixe.com/techknow/index.php/2006/06/18/acts-as-commentable-plugin/
|
40
|
+
post_install_message:
|
41
|
+
rdoc_options: []
|
42
|
+
|
43
|
+
require_paths:
|
44
|
+
- lib
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: "0"
|
50
|
+
version:
|
51
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: "0"
|
56
|
+
version:
|
57
|
+
requirements: []
|
58
|
+
|
59
|
+
rubyforge_project:
|
60
|
+
rubygems_version: 1.2.0
|
61
|
+
signing_key:
|
62
|
+
specification_version: 3
|
63
|
+
summary: Plugin/gem that provides comment functionality
|
64
|
+
test_files: []
|
65
|
+
|