jimiray-acts_as_commentable 1.0.0
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 +64 -0
- data/Rakefile +35 -0
- data/acts_as_commentable.gemspec +22 -0
- data/init.rb +3 -0
- data/install.rb +1 -0
- data/lib/acts_as_commentable.rb +64 -0
- data/lib/comment.rb +34 -0
- data/test/acts_as_commentable_test.rb +8 -0
- metadata +63 -0
data/CHANGELOG
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
* revision 8: Changed has_many :dependent => true to :dependent => :destroy for Rails 1.2.2
|
2
|
+
+ Thanks Josh Martin
|
3
|
+
Added an order clause in the has_many relationship.
|
4
|
+
Made comment column type to text from string in migration example in README
|
5
|
+
+ Thanks Patrick Crowley
|
6
|
+
Added this CHANGELOG file.
|
7
|
+
|
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,64 @@
|
|
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
|
+
* To install as a plugin:
|
11
|
+
|
12
|
+
script/plugin install http://juixe.com/svn/acts_as_commentable
|
13
|
+
|
14
|
+
* To install as a gem:
|
15
|
+
|
16
|
+
rake install
|
17
|
+
|
18
|
+
* Create a new rails migration and add the following self.up and self.down methods
|
19
|
+
|
20
|
+
def self.up
|
21
|
+
create_table "comments", :force => true do |t|
|
22
|
+
t.column "title", :string, :limit => 50, :default => ""
|
23
|
+
t.column "comment", :text, :default => ""
|
24
|
+
t.column "created_at", :datetime, :null => false
|
25
|
+
t.column "commentable_id", :integer, :default => 0, :null => false
|
26
|
+
t.column "commentable_type", :string, :limit => 15, :default => "", :null => false
|
27
|
+
t.column "user_id", :integer, :default => 0, :null => false
|
28
|
+
end
|
29
|
+
|
30
|
+
add_index "comments", ["user_id"], :name => "fk_comments_user"
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.down
|
34
|
+
drop_table :comments
|
35
|
+
end
|
36
|
+
|
37
|
+
== Usage
|
38
|
+
|
39
|
+
* Make you ActiveRecord model act as commentable.
|
40
|
+
|
41
|
+
class Model < ActiveRecord::Base
|
42
|
+
acts_as_commentable
|
43
|
+
end
|
44
|
+
|
45
|
+
* Add a comment to a model instance
|
46
|
+
|
47
|
+
model = Model.new
|
48
|
+
comment = Comment.new
|
49
|
+
comment.comment = 'Some comment'
|
50
|
+
model.comments << comment
|
51
|
+
|
52
|
+
* Each comment reference commentable object
|
53
|
+
|
54
|
+
model = Model.find(1)
|
55
|
+
model.comments.get(0).commtable == model
|
56
|
+
|
57
|
+
== Credits
|
58
|
+
|
59
|
+
Xelipe - This plugin is heavily influced by Acts As Tagglable.
|
60
|
+
|
61
|
+
== More
|
62
|
+
|
63
|
+
http://www.juixe.com/techknow/index.php/2006/06/18/acts-as-commentable-plugin/
|
64
|
+
http://www.juixe.com/projects/acts_as_commentable
|
data/Rakefile
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake/gempackagetask'
|
3
|
+
|
4
|
+
PLUGIN = "acts_as_commentable"
|
5
|
+
NAME = "acts_as_commentable"
|
6
|
+
VERSION = "1.0.0"
|
7
|
+
AUTHOR = "Cosmin Radoi"
|
8
|
+
EMAIL = "unknown@juixe.com"
|
9
|
+
HOMEPAGE = "http://www.juixe.com/techknow/index.php/2006/06/18/acts-as-commentable-plugin/"
|
10
|
+
SUMMARY = "Plugin/gem that provides comment functionality"
|
11
|
+
|
12
|
+
spec = Gem::Specification.new do |s|
|
13
|
+
s.name = NAME
|
14
|
+
s.version = VERSION
|
15
|
+
s.platform = Gem::Platform::RUBY
|
16
|
+
s.has_rdoc = true
|
17
|
+
s.extra_rdoc_files = ["README", "MIT-LICENSE"]
|
18
|
+
s.summary = SUMMARY
|
19
|
+
s.description = s.summary
|
20
|
+
s.author = AUTHOR
|
21
|
+
s.email = EMAIL
|
22
|
+
s.homepage = HOMEPAGE
|
23
|
+
s.add_dependency('merb', '>= 0.5.0')
|
24
|
+
s.require_path = 'lib'
|
25
|
+
s.autorequire = PLUGIN
|
26
|
+
s.files = %w(MIT-LICENSE README Rakefile) + Dir.glob("{lib,specs}/**/*")
|
27
|
+
end
|
28
|
+
|
29
|
+
Rake::GemPackageTask.new(spec) do |pkg|
|
30
|
+
pkg.gem_spec = spec
|
31
|
+
end
|
32
|
+
|
33
|
+
task :install => [:package] do
|
34
|
+
sh %{sudo gem install pkg/#{NAME}-#{VERSION}}
|
35
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = "acts_as_commentable"
|
3
|
+
s.version = "1.0.0"
|
4
|
+
s.date = "2008-10-30"
|
5
|
+
s.summary = "Polymorphic comments Rails plugin"
|
6
|
+
s.email = "jimiray@mac.com"
|
7
|
+
s.homepage = "http://github.com/jimiray/acts_as_commentable"
|
8
|
+
s.description = "Polymorphic comments Rails plugin"
|
9
|
+
s.has_rdoc = true
|
10
|
+
s.authors = ["Jack Dempsey", "Xelipe"]
|
11
|
+
s.files = ["CHANGELOG",
|
12
|
+
"MIT-LICENSE",
|
13
|
+
"README",
|
14
|
+
"Rakefile",
|
15
|
+
"acts_as_commentable.gemspec",
|
16
|
+
"init.rb",
|
17
|
+
"install.rb",
|
18
|
+
"lib/acts_as_commentable.rb",
|
19
|
+
"lib/comment.rb"]
|
20
|
+
s.test_files = ["test/acts_as_commentable_test.rb"]
|
21
|
+
s.rdoc_options = ["--main", "README"]
|
22
|
+
end
|
data/init.rb
ADDED
data/install.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# Install hook code here
|
@@ -0,0 +1,64 @@
|
|
1
|
+
# ActsAsCommentable
|
2
|
+
module Juixe
|
3
|
+
module Acts #:nodoc:
|
4
|
+
module Commentable #:nodoc:
|
5
|
+
|
6
|
+
def self.included(base)
|
7
|
+
base.extend ClassMethods
|
8
|
+
end
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
def acts_as_commentable
|
12
|
+
has_many :comments, :as => :commentable, :dependent => :destroy, :order => 'created_at ASC'
|
13
|
+
include Juixe::Acts::Commentable::InstanceMethods
|
14
|
+
extend Juixe::Acts::Commentable::SingletonMethods
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
# This module contains class methods
|
19
|
+
module SingletonMethods
|
20
|
+
# Helper method to lookup for comments for a given object.
|
21
|
+
# This method is equivalent to obj.comments.
|
22
|
+
def find_comments_for(obj)
|
23
|
+
commentable = ActiveRecord::Base.send(:class_name_of_active_record_descendant, self).to_s
|
24
|
+
|
25
|
+
Comment.find(:all,
|
26
|
+
:conditions => ["commentable_id = ? and commentable_type = ?", obj.id, commentable],
|
27
|
+
:order => "created_at DESC"
|
28
|
+
)
|
29
|
+
end
|
30
|
+
|
31
|
+
# Helper class method to lookup comments for
|
32
|
+
# the mixin commentable type written by a given user.
|
33
|
+
# This method is NOT equivalent to Comment.find_comments_for_user
|
34
|
+
def find_comments_by_user(user)
|
35
|
+
commentable = ActiveRecord::Base.send(:class_name_of_active_record_descendant, self).to_s
|
36
|
+
|
37
|
+
Comment.find(:all,
|
38
|
+
:conditions => ["user_id = ? and commentable_type = ?", user.id, commentable],
|
39
|
+
:order => "created_at DESC"
|
40
|
+
)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
# This module contains instance methods
|
45
|
+
module InstanceMethods
|
46
|
+
# Helper method to sort comments by date
|
47
|
+
def comments_ordered_by_submitted
|
48
|
+
Comment.find(:all,
|
49
|
+
:conditions => ["commentable_id = ? and commentable_type = ?", id, self.type.name],
|
50
|
+
:order => "created_at DESC"
|
51
|
+
)
|
52
|
+
end
|
53
|
+
|
54
|
+
# Helper method that defaults the submitted time.
|
55
|
+
def add_comment(comment)
|
56
|
+
comments << comment
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
ActiveRecord::Base.send(:include, Juixe::Acts::Commentable)
|
data/lib/comment.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
class Comment < ActiveRecord::Base
|
2
|
+
belongs_to :commentable, :polymorphic => true
|
3
|
+
|
4
|
+
# NOTE: install the acts_as_votable plugin if you
|
5
|
+
# want user to vote on the quality of comments.
|
6
|
+
#acts_as_voteable
|
7
|
+
|
8
|
+
# NOTE: Comments belong to a user
|
9
|
+
belongs_to :user
|
10
|
+
|
11
|
+
# Helper class method to lookup all comments assigned
|
12
|
+
# to all commentable types for a given user.
|
13
|
+
def self.find_comments_by_user(user)
|
14
|
+
find(:all,
|
15
|
+
:conditions => ["user_id = ?", user.id],
|
16
|
+
:order => "created_at DESC"
|
17
|
+
)
|
18
|
+
end
|
19
|
+
|
20
|
+
# Helper class method to look up all comments for
|
21
|
+
# commentable class name and commentable id.
|
22
|
+
def self.find_comments_for_commentable(commentable_str, commentable_id)
|
23
|
+
find(:all,
|
24
|
+
:conditions => ["commentable_type = ? and commentable_id = ?", commentable_str, commentable_id],
|
25
|
+
:order => "created_at DESC"
|
26
|
+
)
|
27
|
+
end
|
28
|
+
|
29
|
+
# Helper class method to look up a commentable object
|
30
|
+
# given the commentable class name and id
|
31
|
+
def self.find_commentable(commentable_str, commentable_id)
|
32
|
+
commentable_str.constantize.find(commentable_id)
|
33
|
+
end
|
34
|
+
end
|
metadata
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jimiray-acts_as_commentable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jack Dempsey
|
8
|
+
- Xelipe
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2008-10-30 00:00:00 -07:00
|
14
|
+
default_executable:
|
15
|
+
dependencies: []
|
16
|
+
|
17
|
+
description: Polymorphic comments Rails plugin
|
18
|
+
email: jimiray@mac.com
|
19
|
+
executables: []
|
20
|
+
|
21
|
+
extensions: []
|
22
|
+
|
23
|
+
extra_rdoc_files: []
|
24
|
+
|
25
|
+
files:
|
26
|
+
- CHANGELOG
|
27
|
+
- MIT-LICENSE
|
28
|
+
- README
|
29
|
+
- Rakefile
|
30
|
+
- acts_as_commentable.gemspec
|
31
|
+
- init.rb
|
32
|
+
- install.rb
|
33
|
+
- lib/acts_as_commentable.rb
|
34
|
+
- lib/comment.rb
|
35
|
+
has_rdoc: true
|
36
|
+
homepage: http://github.com/jimiray/acts_as_commentable
|
37
|
+
post_install_message:
|
38
|
+
rdoc_options:
|
39
|
+
- --main
|
40
|
+
- README
|
41
|
+
require_paths:
|
42
|
+
- lib
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: "0"
|
48
|
+
version:
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: "0"
|
54
|
+
version:
|
55
|
+
requirements: []
|
56
|
+
|
57
|
+
rubyforge_project:
|
58
|
+
rubygems_version: 1.2.0
|
59
|
+
signing_key:
|
60
|
+
specification_version: 2
|
61
|
+
summary: Polymorphic comments Rails plugin
|
62
|
+
test_files:
|
63
|
+
- test/acts_as_commentable_test.rb
|