aeonscope-acts_as_commentable 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +3 -0
- data/LICENSE.rdoc +20 -0
- data/README.rdoc +116 -0
- data/Rakefile +52 -0
- data/VERSION.yml +4 -0
- data/lib/acts_as_commentable.rb +17 -0
- data/lib/class_methods.rb +20 -0
- data/lib/instance_methods.rb +15 -0
- data/spec/acts_as_commentable_spec.rb +7 -0
- data/spec/spec_helper.rb +9 -0
- metadata +75 -0
data/CHANGELOG.rdoc
ADDED
data/LICENSE.rdoc
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Brooke Kuhlmann of {Berserk Technologies}[http://www.berserktech.com]
|
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,116 @@
|
|
1
|
+
= Overview
|
2
|
+
|
3
|
+
Allows for comments to be easily added to different models.
|
4
|
+
|
5
|
+
= License
|
6
|
+
|
7
|
+
Copyright (c) 2009 Brooke Kuhlmann of {Berserk Technologies}[http://www.berserktech.com].
|
8
|
+
See the included LICENSE for more info.
|
9
|
+
|
10
|
+
= History
|
11
|
+
|
12
|
+
See the CHANGELOG file for more info.
|
13
|
+
|
14
|
+
= Requirements
|
15
|
+
|
16
|
+
1. {Ruby on Rails}[http://rubyonrails.org].
|
17
|
+
|
18
|
+
= Installation
|
19
|
+
|
20
|
+
Type the following from the command line to install:
|
21
|
+
|
22
|
+
* *UNIX*: sudo gem install aeonscope-acts_as_commentable
|
23
|
+
* *Windows*: gem install aeonscope-acts_as_commentable
|
24
|
+
|
25
|
+
Update your environment.rb file to include the new gem:
|
26
|
+
|
27
|
+
* config.gem "aeonscope-acts_as_commentable", :lib => "acts_as_commentable", :source => "http://gems.github.com"
|
28
|
+
|
29
|
+
= Usage
|
30
|
+
|
31
|
+
Assuming you have created Post and Comment migrations and models, for example, then you can modify your models as follows:
|
32
|
+
|
33
|
+
class Post < ActiveRecord::Base
|
34
|
+
# Behaviors
|
35
|
+
acts_as_commentable
|
36
|
+
end
|
37
|
+
|
38
|
+
class Comment < ActiveRecord::Base
|
39
|
+
# Behaviors
|
40
|
+
acts_as_comment
|
41
|
+
end
|
42
|
+
|
43
|
+
That's it, your done!
|
44
|
+
|
45
|
+
As an added bonus, you can customize your comment behavior. For example, lets say you want your comment label to reflect
|
46
|
+
your post label (assuming you have "label" columns for both your Post and Comment models). You could then specify the auto_label method
|
47
|
+
to be called before you save a comment. Like so:
|
48
|
+
|
49
|
+
class Comment < ActiveRecord::Base
|
50
|
+
# Behaviors
|
51
|
+
acts_as_comment
|
52
|
+
|
53
|
+
# Callbacks
|
54
|
+
before_save :auto_label
|
55
|
+
end
|
56
|
+
|
57
|
+
So if you created a post with a "My Awesome Post" label, then your comment label would end up as "RE: My Awesome Post" label in order
|
58
|
+
to preserve comment context.
|
59
|
+
|
60
|
+
What's that you say? You don't use "label" columns for your Post and Comment models? No problem, you can
|
61
|
+
change the default behavior as follows:
|
62
|
+
|
63
|
+
class Comment < ActiveRecord::Base
|
64
|
+
# Behaviors
|
65
|
+
acts_as_comment :commentable_label => "title", :comment_label => "title"
|
66
|
+
|
67
|
+
# Callbacks
|
68
|
+
before_save :auto_label
|
69
|
+
end
|
70
|
+
|
71
|
+
Now you are using "title" instead "label" columns. To be clear, the "commentable_label" defines the Post model setting, while the "comment_label" defines the Comment model setting.
|
72
|
+
|
73
|
+
= Migrations
|
74
|
+
|
75
|
+
Based on the Post and Comment models mentioned above, the following migration code might be of interest:
|
76
|
+
|
77
|
+
# Post Migration
|
78
|
+
class CreatePosts < ActiveRecord::Migration
|
79
|
+
def self.up
|
80
|
+
create_table :posts do |t|
|
81
|
+
t.string :name
|
82
|
+
t.string :label
|
83
|
+
t.text :content
|
84
|
+
t.string :visibility
|
85
|
+
t.datetime :published_at
|
86
|
+
t.timestamps
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
def self.down
|
91
|
+
drop_table :posts
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
# Comment Migration
|
96
|
+
class CreateComments < ActiveRecord::Migration
|
97
|
+
def self.up
|
98
|
+
create_table :comments do |t|
|
99
|
+
t.integer :commentable_id
|
100
|
+
t.string :commentable_type
|
101
|
+
t.string :label
|
102
|
+
t.text :content
|
103
|
+
t.timestamps
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
def self.down
|
108
|
+
drop_table :comments
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
= Contact/Feedback/Issues
|
113
|
+
|
114
|
+
* {Berserk Technologies}[http://www.berserktech.com] - Company web site.
|
115
|
+
* Aeonscope[http://www.aeonscope.net] - Personal web site.
|
116
|
+
* Twitter[http://www.twitter.com/Aeonscope] - Short bursts of insight and/or noise.
|
data/Rakefile
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "acts_as_commentable"
|
8
|
+
gem.summary = "Allows for comments to be easily added to different models."
|
9
|
+
gem.description = "Allows for comments to be easily added to different models."
|
10
|
+
gem.authors = ["Brooke Kuhlmann"]
|
11
|
+
gem.email = "aeonscope@gmail.com"
|
12
|
+
gem.homepage = "http://github.com/aeonscope/acts_as_commentable"
|
13
|
+
gem.required_ruby_version = ">= 1.8.6"
|
14
|
+
gem.add_dependency "rails", ">= 2.3.2"
|
15
|
+
gem.rdoc_options << "CHANGELOG.rdoc"
|
16
|
+
gem.files = FileList["[A-Z]*", "{bin,lib,generators,rails_generators,spec}/**/*"]
|
17
|
+
end
|
18
|
+
|
19
|
+
rescue LoadError
|
20
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
21
|
+
end
|
22
|
+
|
23
|
+
require 'spec/rake/spectask'
|
24
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
25
|
+
spec.libs << 'lib' << 'spec'
|
26
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
27
|
+
end
|
28
|
+
|
29
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
30
|
+
spec.libs << 'lib' << 'spec'
|
31
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
32
|
+
spec.rcov = true
|
33
|
+
end
|
34
|
+
|
35
|
+
|
36
|
+
task :default => :spec
|
37
|
+
|
38
|
+
require 'rake/rdoctask'
|
39
|
+
Rake::RDocTask.new do |rdoc|
|
40
|
+
if File.exist?('VERSION.yml')
|
41
|
+
config = YAML.load(File.read('VERSION.yml'))
|
42
|
+
version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
|
43
|
+
else
|
44
|
+
version = ""
|
45
|
+
end
|
46
|
+
|
47
|
+
rdoc.rdoc_dir = 'rdoc'
|
48
|
+
rdoc.title = "acts_as_commentable #{version}"
|
49
|
+
rdoc.rdoc_files.include('README*')
|
50
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
51
|
+
end
|
52
|
+
|
data/VERSION.yml
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "class_methods.rb")
|
2
|
+
require File.join(File.dirname(__FILE__), "instance_methods.rb")
|
3
|
+
|
4
|
+
module ActsAsComment
|
5
|
+
def self.included base
|
6
|
+
base.extend ClassMethods
|
7
|
+
end
|
8
|
+
include InstanceMethods
|
9
|
+
end
|
10
|
+
|
11
|
+
module ActsAsCommentable
|
12
|
+
def self.included base
|
13
|
+
base.extend ClassMethods
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
ActiveRecord::Base.class_eval { include ActsAsComment, ActsAsCommentable }
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module ActsAsComment
|
2
|
+
module ClassMethods
|
3
|
+
# Allows one to customize the default settings. Accepts the following options hash:
|
4
|
+
# * *commentable_label* - The commentable (parent object) label method. Optional. Defaults to: label.
|
5
|
+
# * *comment_label* - The comment label method. Optional. Defaults to: label.
|
6
|
+
def acts_as_comment options = {}
|
7
|
+
write_inheritable_attribute :acts_as_comment_options, options
|
8
|
+
class_inheritable_reader :acts_as_comment_options
|
9
|
+
belongs_to :commentable, :polymorphic => true
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
module ActsAsCommentable
|
15
|
+
module ClassMethods
|
16
|
+
def acts_as_commentable
|
17
|
+
has_many :comments, :as => :commentable, :dependent => :destroy
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module ActsAsComment
|
2
|
+
module InstanceMethods
|
3
|
+
# Before saving, store the comment label as as reply of the commentable label. This especially useful
|
4
|
+
# when representing context origin (i.e. a syndicated comment feed).
|
5
|
+
def auto_label
|
6
|
+
# Set up defaults and custom options (if any).
|
7
|
+
options = self.class.acts_as_comment_options || {}
|
8
|
+
options.reverse_merge! :commentable_label => "label", :comment_label => "label"
|
9
|
+
# Default the comment label to the commentable label.
|
10
|
+
if self.instance_eval("self.#{options[:comment_label]}").blank? && commentable && commentable.respond_to?(options[:commentable_label])
|
11
|
+
instance_eval("self.#{options[:comment_label]} = "RE: " + commentable.#{options[:commentable_label]}")
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: aeonscope-acts_as_commentable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Brooke Kuhlmann
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-07-27 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rails
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 2.3.2
|
24
|
+
version:
|
25
|
+
description: Allows for comments to be easily added to different models.
|
26
|
+
email: aeonscope@gmail.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- LICENSE.rdoc
|
33
|
+
- README.rdoc
|
34
|
+
files:
|
35
|
+
- CHANGELOG.rdoc
|
36
|
+
- LICENSE.rdoc
|
37
|
+
- README.rdoc
|
38
|
+
- Rakefile
|
39
|
+
- VERSION.yml
|
40
|
+
- lib/acts_as_commentable.rb
|
41
|
+
- lib/class_methods.rb
|
42
|
+
- lib/instance_methods.rb
|
43
|
+
- spec/acts_as_commentable_spec.rb
|
44
|
+
- spec/spec_helper.rb
|
45
|
+
has_rdoc: false
|
46
|
+
homepage: http://github.com/aeonscope/acts_as_commentable
|
47
|
+
licenses:
|
48
|
+
post_install_message:
|
49
|
+
rdoc_options:
|
50
|
+
- --charset=UTF-8
|
51
|
+
- CHANGELOG.rdoc
|
52
|
+
require_paths:
|
53
|
+
- lib
|
54
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: 1.8.6
|
59
|
+
version:
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: "0"
|
65
|
+
version:
|
66
|
+
requirements: []
|
67
|
+
|
68
|
+
rubyforge_project:
|
69
|
+
rubygems_version: 1.3.5
|
70
|
+
signing_key:
|
71
|
+
specification_version: 3
|
72
|
+
summary: Allows for comments to be easily added to different models.
|
73
|
+
test_files:
|
74
|
+
- spec/acts_as_commentable_spec.rb
|
75
|
+
- spec/spec_helper.rb
|