elight-acts_as_commentable_with_threading 0.1.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/CHANGELOG +7 -0
- data/MIT-LICENSE +20 -0
- data/README +75 -0
- data/Rakefile +28 -0
- data/acts_as_commentable_with_threading.gemspec +25 -0
- data/init.rb +3 -0
- data/install.rb +1 -0
- data/lib/acts_as_commentable_with_threading.rb +69 -0
- data/lib/comment.rb +37 -0
- data/spec/comment_spec.rb +49 -0
- data/spec/commentable_spec.rb +7 -0
- data/spec/db/database.yml +18 -0
- data/spec/db/schema.rb +25 -0
- data/spec/spec_helper.rb +24 -0
- metadata +76 -0
data/CHANGELOG
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2008 Evan David Light
|
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,75 @@
|
|
1
|
+
Acts As Commentable
|
2
|
+
=================
|
3
|
+
|
4
|
+
Allows for threaded comments to be added to multiple and different models. Drop-in compatible for acts_as_commentable (however requiring a database schema change)
|
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
|
15
|
+
|
16
|
+
Merb/Rails
|
17
|
+
|
18
|
+
* To install as a gem:
|
19
|
+
|
20
|
+
rake install
|
21
|
+
|
22
|
+
* Create a new rails migration and add the following self.up and self.down methods
|
23
|
+
|
24
|
+
def self.up
|
25
|
+
create_table "comments", :force => true do |t|
|
26
|
+
t.integer "commentable_id", :default => 0
|
27
|
+
t.string "commentable_type", :limit => 15, :default => ""
|
28
|
+
t.string "title", :default => ""
|
29
|
+
t.text "body", :default => ""
|
30
|
+
t.string "subject", :default => ""
|
31
|
+
t.integer "user_id", :default => 0, :null => false
|
32
|
+
t.integer "parent_id"
|
33
|
+
t.integer "lft"
|
34
|
+
t.integer "rgt"
|
35
|
+
t.timestamps
|
36
|
+
end
|
37
|
+
|
38
|
+
add_index "comments", "user_id"
|
39
|
+
add_index "comments", "commentable_id"
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.down
|
43
|
+
drop_table :comments
|
44
|
+
end
|
45
|
+
|
46
|
+
== Usage
|
47
|
+
class Model < ActiveRecord::Base
|
48
|
+
acts_as_commentable_with_threading
|
49
|
+
end
|
50
|
+
|
51
|
+
* Add a comment to a model instance
|
52
|
+
|
53
|
+
model = Model.new
|
54
|
+
comment = Comment.new(:model => model, :body => "Your comment text here")
|
55
|
+
comment.save!
|
56
|
+
comment.move_to_child_of(the_desired_parent_comment)
|
57
|
+
|
58
|
+
# Following doesn't work/make sense to me. Leaving for historical sake -- Jack
|
59
|
+
# * Each comment reference commentable object
|
60
|
+
#
|
61
|
+
# model = Model.find(1)
|
62
|
+
# model.comments.get(0).commentable == model
|
63
|
+
|
64
|
+
== Credits
|
65
|
+
|
66
|
+
Jack Dempsey - This plugin/gem is heavily influenced/liberally borrowed/stole from acts_as_commentable
|
67
|
+
|
68
|
+
which in turn credits....
|
69
|
+
|
70
|
+
Xelipe - Because acts_as_commentable was heavily influenced by Acts As Taggable.
|
71
|
+
|
72
|
+
== More
|
73
|
+
|
74
|
+
http://triple-dog-dare.com
|
75
|
+
http://evan.tiggerpalace.com
|
data/Rakefile
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake/gempackagetask'
|
3
|
+
require 'spec'
|
4
|
+
require 'spec/rake/spectask'
|
5
|
+
|
6
|
+
PLUGIN = "acts_as_commentable_with_threading"
|
7
|
+
NAME = "acts_as_commentable_with_threading"
|
8
|
+
GEM_VERSION = "1.0.0"
|
9
|
+
AUTHOR = "Evan Light"
|
10
|
+
EMAIL = "evan@triple-dog-dare.com"
|
11
|
+
SUMMARY = "Plugin/gem that provides threaded comment functionality"
|
12
|
+
|
13
|
+
load 'acts_as_commentable_with_threading.gemspec'
|
14
|
+
spec = ACTS_AS_COMMENTABLE_WITH_THREADING
|
15
|
+
|
16
|
+
Rake::GemPackageTask.new(spec) do |pkg|
|
17
|
+
pkg.gem_spec = spec
|
18
|
+
end
|
19
|
+
|
20
|
+
Spec::Rake::SpecTask.new do |t|
|
21
|
+
t.spec_files = FileList['spec/**_spec.rb']
|
22
|
+
end
|
23
|
+
|
24
|
+
task :install => [:package] do
|
25
|
+
sh %{sudo gem install pkg/#{NAME}-#{GEM_VERSION}}
|
26
|
+
end
|
27
|
+
|
28
|
+
task :default => :spec
|
@@ -0,0 +1,25 @@
|
|
1
|
+
ACTS_AS_COMMENTABLE_WITH_THREADING = Gem::Specification.new do |s|
|
2
|
+
s.name = "acts_as_commentable_with_threading"
|
3
|
+
s.version = "0.1.1"
|
4
|
+
s.date = "2008-11-16"
|
5
|
+
s.summary = "Polymorphic comments Rails plugin"
|
6
|
+
s.email = "evan@tiggerpalace.com"
|
7
|
+
s.homepage = "http://github.com/elight/acts_as_commentable_with_threading"
|
8
|
+
s.description = "Polymorphic threaded comments Rails plugin/gem"
|
9
|
+
s.has_rdoc = true
|
10
|
+
s.authors = ["Evan Light", "Jack Dempsey", "Xelipe"]
|
11
|
+
s.files = [
|
12
|
+
"CHANGELOG",
|
13
|
+
"MIT-LICENSE",
|
14
|
+
"README",
|
15
|
+
"Rakefile",
|
16
|
+
"acts_as_commentable_with_threading.gemspec",
|
17
|
+
"init.rb",
|
18
|
+
"install.rb",
|
19
|
+
"lib/acts_as_commentable_with_threading.rb",
|
20
|
+
"lib/comment.rb"]
|
21
|
+
s.test_files = ["spec/commentable_spec.rb", "spec/comment_spec.rb", "spec/spec_helper.rb", "spec/db/database.yml", "spec/db/schema.rb"]
|
22
|
+
s.rdoc_options = ["--main", "README"]
|
23
|
+
|
24
|
+
s.add_dependency 'collectiveidea-awesome_nested_set'
|
25
|
+
end
|
data/init.rb
ADDED
data/install.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# Install hook code here
|
@@ -0,0 +1,69 @@
|
|
1
|
+
require 'activerecord'
|
2
|
+
require 'awesome_nested_set'
|
3
|
+
ActiveRecord::Base.class_eval do
|
4
|
+
include CollectiveIdea::Acts::NestedSet
|
5
|
+
end
|
6
|
+
require 'comment'
|
7
|
+
|
8
|
+
# ActsAsCommentableWithThreading
|
9
|
+
module Acts #:nodoc:
|
10
|
+
module CommentableWithThreading #:nodoc:
|
11
|
+
|
12
|
+
def self.included(base)
|
13
|
+
base.extend ClassMethods
|
14
|
+
end
|
15
|
+
|
16
|
+
module ClassMethods
|
17
|
+
def acts_as_commentable
|
18
|
+
has_many :comment_threads, :class_name => "Comment", :as => :commentable, :dependent => :destroy, :order => 'created_at ASC'
|
19
|
+
include Acts::CommentableWithThreading::InstanceMethods
|
20
|
+
extend Acts::CommentableWithThreading::SingletonMethods
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
# This module contains class methods
|
25
|
+
module SingletonMethods
|
26
|
+
# Helper method to lookup for comments for a given object.
|
27
|
+
# This method is equivalent to obj.comments.
|
28
|
+
def find_comments_for(obj)
|
29
|
+
commentable = ActiveRecord::Base.send(:class_name_of_active_record_descendant, self).to_s
|
30
|
+
|
31
|
+
Comment.find(:all,
|
32
|
+
:conditions => ["commentable_id = ? and commentable_type = ?", obj.id, commentable],
|
33
|
+
:order => "created_at DESC"
|
34
|
+
)
|
35
|
+
end
|
36
|
+
|
37
|
+
# Helper class method to lookup comments for
|
38
|
+
# the mixin commentable type written by a given user.
|
39
|
+
# This method is NOT equivalent to Comment.find_comments_for_user
|
40
|
+
def find_comments_by_user(user)
|
41
|
+
commentable = ActiveRecord::Base.send(:class_name_of_active_record_descendant, self).to_s
|
42
|
+
|
43
|
+
Comment.find(:all,
|
44
|
+
:conditions => ["user_id = ? and commentable_type = ?", user.id, commentable],
|
45
|
+
:order => "created_at DESC"
|
46
|
+
)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
# This module contains instance methods
|
51
|
+
module InstanceMethods
|
52
|
+
# Helper method to sort comments by date
|
53
|
+
def comments_ordered_by_submitted
|
54
|
+
Comment.find(:all,
|
55
|
+
:conditions => ["commentable_id = ? and commentable_type = ?", id, self.type.name],
|
56
|
+
:order => "created_at DESC"
|
57
|
+
)
|
58
|
+
end
|
59
|
+
|
60
|
+
# Helper method that defaults the submitted time.
|
61
|
+
def add_comment(comment)
|
62
|
+
comments << comment
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
ActiveRecord::Base.send(:include, Acts::CommentableWithThreading)
|
data/lib/comment.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
class Comment < ActiveRecord::Base
|
2
|
+
acts_as_nested_set :scope => [:commentable_id, :commentable_type]
|
3
|
+
|
4
|
+
validates_presence_of :body
|
5
|
+
validates_presence_of :user
|
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
|
+
# Helper class method to lookup all comments assigned
|
15
|
+
# to all commentable types for a given user.
|
16
|
+
def self.find_comments_by_user(user)
|
17
|
+
find(:all,
|
18
|
+
:conditions => ["user_id = ?", user.id],
|
19
|
+
:order => "created_at DESC"
|
20
|
+
)
|
21
|
+
end
|
22
|
+
|
23
|
+
# Helper class method to look up all comments for
|
24
|
+
# commentable class name and commentable id.
|
25
|
+
def self.find_comments_for_commentable(commentable_str, commentable_id)
|
26
|
+
find(:all,
|
27
|
+
:conditions => ["commentable_type = ? and commentable_id = ?", commentable_str, commentable_id],
|
28
|
+
:order => "created_at DESC"
|
29
|
+
)
|
30
|
+
end
|
31
|
+
|
32
|
+
# Helper class method to look up a commentable object
|
33
|
+
# given the commentable class name and id
|
34
|
+
def self.find_commentable(commentable_str, commentable_id)
|
35
|
+
commentable_str.constantize.find(commentable_id)
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "spec_helper")
|
2
|
+
|
3
|
+
# Specs some of the behavior of awesome_nested_set although does so to demonstrate the use of this gem
|
4
|
+
describe Comment do
|
5
|
+
before do
|
6
|
+
@user = User.create!
|
7
|
+
@comment = Comment.create!(:body => "Root comment", :user => @user)
|
8
|
+
end
|
9
|
+
|
10
|
+
describe "that is valid" do
|
11
|
+
it "should have a user" do
|
12
|
+
@comment.user.should_not be_nil
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should have a body" do
|
16
|
+
@comment.body.should_not be_nil
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should not have a parent if it is a root Comment" do
|
21
|
+
@comment.parent.should be_nil
|
22
|
+
end
|
23
|
+
|
24
|
+
it "can have see how child Comments it has" do
|
25
|
+
@comment.children.size.should == 0
|
26
|
+
end
|
27
|
+
|
28
|
+
it "can add child Comments" do
|
29
|
+
grandchild = Comment.new(:body => "This is a grandchild", :user => @user)
|
30
|
+
grandchild.save!
|
31
|
+
grandchild.move_to_child_of(@comment)
|
32
|
+
@comment.children.size.should == 1
|
33
|
+
end
|
34
|
+
|
35
|
+
describe "after having a child added" do
|
36
|
+
before do
|
37
|
+
@child = Comment.create!(:body => "Child comment", :user => @user)
|
38
|
+
@child.move_to_child_of(@comment)
|
39
|
+
end
|
40
|
+
|
41
|
+
it "can be referenced by its child" do
|
42
|
+
@child.parent.should == @comment
|
43
|
+
end
|
44
|
+
|
45
|
+
it "can see its child" do
|
46
|
+
@comment.children.first.should == @child
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
sqlite3:
|
2
|
+
adapter: sqlite3
|
3
|
+
dbfile: acts_as_commentable_with_threading.sqlite3.db
|
4
|
+
sqlite3mem:
|
5
|
+
:adapter: sqlite3
|
6
|
+
:dbfile: ":memory:"
|
7
|
+
postgresql:
|
8
|
+
:adapter: postgresql
|
9
|
+
:username: postgres
|
10
|
+
:password: postgres
|
11
|
+
:database: acts_as_commentable_with_threading_plugin_test
|
12
|
+
:min_messages: ERROR
|
13
|
+
mysql:
|
14
|
+
:adapter: mysql
|
15
|
+
:host: localhost
|
16
|
+
:username: root
|
17
|
+
:password:
|
18
|
+
:database: acts_as_commentable_with_threading_plugin_test
|
data/spec/db/schema.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
ActiveRecord::Schema.define(:version => 0) do
|
2
|
+
create_table "users", :force => true do |t|
|
3
|
+
t.timestamps
|
4
|
+
end
|
5
|
+
|
6
|
+
create_table "commentables", :force => true do |t|
|
7
|
+
t.timestamps
|
8
|
+
end
|
9
|
+
|
10
|
+
create_table "comments", :force => true do |t|
|
11
|
+
t.integer "commentable_id", :default => 0
|
12
|
+
t.string "commentable_type", :limit => 15, :default => ""
|
13
|
+
t.string "title", :default => ""
|
14
|
+
t.text "body", :default => ""
|
15
|
+
t.string "subject", :default => ""
|
16
|
+
t.integer "user_id", :default => 0, :null => false
|
17
|
+
t.integer "parent_id"
|
18
|
+
t.integer "lft"
|
19
|
+
t.integer "rgt"
|
20
|
+
t.timestamps
|
21
|
+
end
|
22
|
+
|
23
|
+
add_index "comments", "user_id"
|
24
|
+
add_index "comments", "commentable_id"
|
25
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
$:.unshift(File.dirname(__FILE__) + '/../lib')
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'active_record'
|
5
|
+
|
6
|
+
|
7
|
+
plugin_test_dir = File.dirname(__FILE__)
|
8
|
+
|
9
|
+
ActiveRecord::Base.logger = Logger.new(File.join(plugin_test_dir, "debug.log"))
|
10
|
+
|
11
|
+
ActiveRecord::Base.configurations = YAML::load(IO.read(File.join(plugin_test_dir, "db", "database.yml")))
|
12
|
+
ActiveRecord::Base.establish_connection(ENV["DB"] || "sqlite3mem")
|
13
|
+
ActiveRecord::Migration.verbose = false
|
14
|
+
load(File.join(plugin_test_dir, "db", "schema.rb"))
|
15
|
+
|
16
|
+
require File.join(plugin_test_dir, '..', 'init')
|
17
|
+
|
18
|
+
class User < ActiveRecord::Base
|
19
|
+
has_many :comments
|
20
|
+
end
|
21
|
+
|
22
|
+
class Commentable < ActiveRecord::Base
|
23
|
+
acts_as_commentable
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: elight-acts_as_commentable_with_threading
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Evan Light
|
8
|
+
- Jack Dempsey
|
9
|
+
- Xelipe
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
|
14
|
+
date: 2008-11-16 00:00:00 -08:00
|
15
|
+
default_executable:
|
16
|
+
dependencies:
|
17
|
+
- !ruby/object:Gem::Dependency
|
18
|
+
name: collectiveidea-awesome_nested_set
|
19
|
+
version_requirement:
|
20
|
+
version_requirements: !ruby/object:Gem::Requirement
|
21
|
+
requirements:
|
22
|
+
- - ">="
|
23
|
+
- !ruby/object:Gem::Version
|
24
|
+
version: "0"
|
25
|
+
version:
|
26
|
+
description: Polymorphic threaded comments Rails plugin/gem
|
27
|
+
email: evan@tiggerpalace.com
|
28
|
+
executables: []
|
29
|
+
|
30
|
+
extensions: []
|
31
|
+
|
32
|
+
extra_rdoc_files: []
|
33
|
+
|
34
|
+
files:
|
35
|
+
- CHANGELOG
|
36
|
+
- MIT-LICENSE
|
37
|
+
- README
|
38
|
+
- Rakefile
|
39
|
+
- acts_as_commentable_with_threading.gemspec
|
40
|
+
- init.rb
|
41
|
+
- install.rb
|
42
|
+
- lib/acts_as_commentable_with_threading.rb
|
43
|
+
- lib/comment.rb
|
44
|
+
has_rdoc: true
|
45
|
+
homepage: http://github.com/elight/acts_as_commentable_with_threading
|
46
|
+
post_install_message:
|
47
|
+
rdoc_options:
|
48
|
+
- --main
|
49
|
+
- README
|
50
|
+
require_paths:
|
51
|
+
- lib
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: "0"
|
57
|
+
version:
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: "0"
|
63
|
+
version:
|
64
|
+
requirements: []
|
65
|
+
|
66
|
+
rubyforge_project:
|
67
|
+
rubygems_version: 1.2.0
|
68
|
+
signing_key:
|
69
|
+
specification_version: 2
|
70
|
+
summary: Polymorphic comments Rails plugin
|
71
|
+
test_files:
|
72
|
+
- spec/commentable_spec.rb
|
73
|
+
- spec/comment_spec.rb
|
74
|
+
- spec/spec_helper.rb
|
75
|
+
- spec/db/database.yml
|
76
|
+
- spec/db/schema.rb
|