commentem 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/Manifest +11 -0
- data/README.rdoc +112 -0
- data/Rakefile +14 -0
- data/commentem.gemspec +30 -0
- data/init.rb +1 -0
- data/lib/commentem/commentable.rb +20 -0
- data/lib/commentem/commenter.rb +24 -0
- data/lib/commentem.rb +5 -0
- data/lib/generators/commentem_generator.rb +28 -0
- data/lib/generators/templates/migration.rb +17 -0
- data/lib/generators/templates/model.rb +7 -0
- metadata +75 -0
data/Manifest
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
README.rdoc
|
2
|
+
Rakefile
|
3
|
+
commentem.gemspec
|
4
|
+
init.rb
|
5
|
+
lib/commentem.rb
|
6
|
+
lib/commentem/commentable.rb
|
7
|
+
lib/commentem/commenter.rb
|
8
|
+
lib/generators/commentem_generator.rb
|
9
|
+
lib/generators/templates/migration.rb
|
10
|
+
lib/generators/templates/model.rb
|
11
|
+
Manifest
|
data/README.rdoc
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
= Commentem
|
2
|
+
|
3
|
+
Rails gem for building a commenting systems by using Active Records.
|
4
|
+
|
5
|
+
by Peter Wong <peter@peterwongpp.com>
|
6
|
+
|
7
|
+
GitHub Project: http://github.com/peterwongpp/commentem
|
8
|
+
|
9
|
+
-----
|
10
|
+
|
11
|
+
== Installation
|
12
|
+
|
13
|
+
=== Rails 3
|
14
|
+
|
15
|
+
If you would like to install it as gem, put this in your Gemfile:
|
16
|
+
|
17
|
+
gem 'commentem'
|
18
|
+
|
19
|
+
If you would rather like to install it as plugin, run this command in your console:
|
20
|
+
|
21
|
+
rails plugin install git://github.com/peterwongpp/commentem.git
|
22
|
+
|
23
|
+
And then run:
|
24
|
+
|
25
|
+
rake generate commentem
|
26
|
+
rake db:migrate
|
27
|
+
|
28
|
+
== Usage
|
29
|
+
|
30
|
+
Just add `acts_as_commenter` and `acts_as_commentable` to your models. You could mix them in the same model if you like. See the examples below:
|
31
|
+
|
32
|
+
---
|
33
|
+
|
34
|
+
# Case 1
|
35
|
+
# A user can comment on posts
|
36
|
+
|
37
|
+
class User < ActiveRecord::Base
|
38
|
+
acts_as_commenter
|
39
|
+
end
|
40
|
+
|
41
|
+
class Post < ActiveRecord::Base
|
42
|
+
acts_as_commentable
|
43
|
+
end
|
44
|
+
|
45
|
+
# You have to build your own form for commenting
|
46
|
+
# To comment:
|
47
|
+
|
48
|
+
@user = User.find(1)
|
49
|
+
@post = Post.find(1)
|
50
|
+
|
51
|
+
@user.comment(@post, "Your comment's here :)")
|
52
|
+
|
53
|
+
# To read comments:
|
54
|
+
|
55
|
+
@post = Post.find(1)
|
56
|
+
@post.comments
|
57
|
+
|
58
|
+
---
|
59
|
+
|
60
|
+
# Case 2
|
61
|
+
# A user can comment on users
|
62
|
+
|
63
|
+
class User < ActiveRecord::Base
|
64
|
+
acts_as_commenter
|
65
|
+
acts_as_commentable
|
66
|
+
end
|
67
|
+
|
68
|
+
# To comment:
|
69
|
+
@user1 = User.find(1) # The user who gives the comment
|
70
|
+
@user2 = User.find(2) # The user who receives the comment
|
71
|
+
|
72
|
+
@user1.comment(@user2, "comment goes to here")
|
73
|
+
|
74
|
+
# To read comments:
|
75
|
+
@user = User.fond(2)
|
76
|
+
|
77
|
+
@user.comments
|
78
|
+
|
79
|
+
---
|
80
|
+
|
81
|
+
# Case 3
|
82
|
+
# A user can comment on users or posts
|
83
|
+
|
84
|
+
class User < ActiveRecord::Base
|
85
|
+
acts_as_commenter
|
86
|
+
acts_as_commentable
|
87
|
+
end
|
88
|
+
|
89
|
+
class Post < ActiveRecord::Base
|
90
|
+
acts_as_commentable
|
91
|
+
end
|
92
|
+
|
93
|
+
# To comment
|
94
|
+
@user = User.find(1)
|
95
|
+
@user2 = User.find(2)
|
96
|
+
@post = Post.find(1)
|
97
|
+
|
98
|
+
@user.comment(@user2, "comment goes to here")
|
99
|
+
@user.comment(@post, "comment goes to here")
|
100
|
+
|
101
|
+
# To read comments:
|
102
|
+
|
103
|
+
@user = User.find(1)
|
104
|
+
@user2 = User.find(2)
|
105
|
+
@post = Post.find(1)
|
106
|
+
|
107
|
+
@user2.comments
|
108
|
+
@post.comments
|
109
|
+
|
110
|
+
@user.comments # retrieve all comments from @user, including on @user2 and @post
|
111
|
+
@user.comments_on(@user2) # same as @user2.comments
|
112
|
+
@user.comments_on(@post) # same as @post.comments
|
data/Rakefile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'echoe'
|
4
|
+
|
5
|
+
Echoe.new('commentem', '1.0.0') do |p|
|
6
|
+
p.description = "A generic commenting system."
|
7
|
+
p.url = "http://github.com/peterwongpp/commentem"
|
8
|
+
p.author = "Peter Wong"
|
9
|
+
p.email = "peter@peterwongpp.com"
|
10
|
+
p.ignore_pattern = ["tmp/*", "script/*"]
|
11
|
+
p.development_dependencies = []
|
12
|
+
end
|
13
|
+
|
14
|
+
Dir["#{File.dirname(__FILE__)}/tasks/*.take"].sort.each { |ext| load ext }
|
data/commentem.gemspec
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{commentem}
|
5
|
+
s.version = "1.0.0"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Peter Wong"]
|
9
|
+
s.date = %q{2011-05-19}
|
10
|
+
s.description = %q{A generic commenting system.}
|
11
|
+
s.email = %q{peter@peterwongpp.com}
|
12
|
+
s.extra_rdoc_files = ["README.rdoc", "lib/commentem.rb", "lib/commentem/commentable.rb", "lib/commentem/commenter.rb", "lib/generators/commentem_generator.rb", "lib/generators/templates/migration.rb", "lib/generators/templates/model.rb"]
|
13
|
+
s.files = ["Manifest", "README.rdoc", "Rakefile", "init.rb", "lib/commentem.rb", "lib/commentem/commentable.rb", "lib/commentem/commenter.rb", "lib/generators/commentem_generator.rb", "lib/generators/templates/migration.rb", "lib/generators/templates/model.rb", "commentem.gemspec"]
|
14
|
+
s.homepage = %q{http://github.com/peterwongpp/commentem}
|
15
|
+
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Commentem", "--main", "README.rdoc"]
|
16
|
+
s.require_paths = ["lib"]
|
17
|
+
s.rubyforge_project = %q{commentem}
|
18
|
+
s.rubygems_version = %q{1.3.7}
|
19
|
+
s.summary = %q{A generic commenting system.}
|
20
|
+
|
21
|
+
if s.respond_to? :specification_version then
|
22
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
23
|
+
s.specification_version = 3
|
24
|
+
|
25
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
26
|
+
else
|
27
|
+
end
|
28
|
+
else
|
29
|
+
end
|
30
|
+
end
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'commentem'
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Commentem
|
2
|
+
module Commentable
|
3
|
+
def self.included(base)
|
4
|
+
base.extend ClassMethods
|
5
|
+
end
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
def acts_as_commentable
|
9
|
+
has_many :comments, :as => :commentable, :dependent => :destroy
|
10
|
+
include Commentem::Commentable::InstanceMethods
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
module InstanceMethods
|
15
|
+
def comments_by(commenter)
|
16
|
+
self.comments.comments_by(commenter)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Commentem
|
2
|
+
module Commenter
|
3
|
+
def self.included(base)
|
4
|
+
base.extend ClassMethods
|
5
|
+
end
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
def acts_as_commenter
|
9
|
+
has_many :commentings, :as => :commenter, :dependent => :destroy, :class_name => "Comment"
|
10
|
+
include Commentem::Commenter::InstanceMethods
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
module InstanceMethods
|
15
|
+
def comments_on(commentable)
|
16
|
+
self.commentings.comments_on(commentable)
|
17
|
+
end
|
18
|
+
|
19
|
+
def comment(commentable, content)
|
20
|
+
self.commentings.create(:commentable => commentable, :content => content)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
data/lib/commentem.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'rails/generators'
|
2
|
+
require 'rails/generators/migration'
|
3
|
+
|
4
|
+
class CommentemGenerator < Rails::Generators::Base
|
5
|
+
include Rails::Generators::Migration
|
6
|
+
|
7
|
+
def self.source_root
|
8
|
+
@source_root ||= File.join(File.dirname(__FILE__), 'templates')
|
9
|
+
end
|
10
|
+
|
11
|
+
# Implement the required interface for Rails::Generators::Migration.
|
12
|
+
# taken from http://github.com/rails/rails/blob/master/activerecord/lib/generators/active_record.rb
|
13
|
+
def self.next_migration_number(dirname)
|
14
|
+
if ActiveRecord::Base.timestamped_migrations
|
15
|
+
Time.now.utc.strftime("%Y%m%d%H%M%S")
|
16
|
+
else
|
17
|
+
"%.3d" % (current_migration_number(dirname) + 1)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def create_migration_file
|
22
|
+
migration_template 'migration.rb', 'db/migrate/commentem_migration.rb'
|
23
|
+
end
|
24
|
+
|
25
|
+
def create_model
|
26
|
+
template 'model.rb', File.join('app/models', 'comment.rb')
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
class CommentemMigration < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
create_table :comments, :force => true do |t|
|
4
|
+
t.references :commenter, :polymorphic => true, :null => false
|
5
|
+
t.references :commentable, :polymorphic => true, :null => false
|
6
|
+
t.text :content
|
7
|
+
t.timestamps
|
8
|
+
end
|
9
|
+
|
10
|
+
add_index :comments, [:commenter_id, :commenter_type]
|
11
|
+
add_index :comments, [:commentable_id, :commentable_type]
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.down
|
15
|
+
drop_table :comments
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,7 @@
|
|
1
|
+
class Comment < ActiveRecord::Base
|
2
|
+
scope :comments_by, lambda { |commenter| where(["commenter_id = ? AND commenter_type = ?", commenter.id, commenter.class.name]) }
|
3
|
+
scope :comments_on, lambda { |commentable| where(["commentable_id = ? AND commentable_type = ?", commentable.id, commentable.class.name]) }
|
4
|
+
|
5
|
+
belongs_to :commenter, :polymorphic => true
|
6
|
+
belongs_to :commentable, :polymorphic => true
|
7
|
+
end
|
metadata
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: commentem
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 1.0.0
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Peter Wong
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-05-19 00:00:00 Z
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: A generic commenting system.
|
17
|
+
email: peter@peterwongpp.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README.rdoc
|
24
|
+
- lib/commentem.rb
|
25
|
+
- lib/commentem/commentable.rb
|
26
|
+
- lib/commentem/commenter.rb
|
27
|
+
- lib/generators/commentem_generator.rb
|
28
|
+
- lib/generators/templates/migration.rb
|
29
|
+
- lib/generators/templates/model.rb
|
30
|
+
files:
|
31
|
+
- Manifest
|
32
|
+
- README.rdoc
|
33
|
+
- Rakefile
|
34
|
+
- init.rb
|
35
|
+
- lib/commentem.rb
|
36
|
+
- lib/commentem/commentable.rb
|
37
|
+
- lib/commentem/commenter.rb
|
38
|
+
- lib/generators/commentem_generator.rb
|
39
|
+
- lib/generators/templates/migration.rb
|
40
|
+
- lib/generators/templates/model.rb
|
41
|
+
- commentem.gemspec
|
42
|
+
homepage: http://github.com/peterwongpp/commentem
|
43
|
+
licenses: []
|
44
|
+
|
45
|
+
post_install_message:
|
46
|
+
rdoc_options:
|
47
|
+
- --line-numbers
|
48
|
+
- --inline-source
|
49
|
+
- --title
|
50
|
+
- Commentem
|
51
|
+
- --main
|
52
|
+
- README.rdoc
|
53
|
+
require_paths:
|
54
|
+
- lib
|
55
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
56
|
+
none: false
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: "0"
|
61
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
|
+
none: false
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: "1.2"
|
67
|
+
requirements: []
|
68
|
+
|
69
|
+
rubyforge_project: commentem
|
70
|
+
rubygems_version: 1.8.3
|
71
|
+
signing_key:
|
72
|
+
specification_version: 3
|
73
|
+
summary: A generic commenting system.
|
74
|
+
test_files: []
|
75
|
+
|