acts_as_commentable 2.0.1

Sign up to get free protection for your applications and to get access to all the features.
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,73 @@
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
+ * To install as a gem
17
+ sudo gem install
18
+
19
+ Merb/Rails
20
+
21
+ * To install as a gem:
22
+ Run the following if you haven't already:
23
+ gem sources -a http://gems.github.com
24
+
25
+ Install the gem(s):
26
+ sudo gem install jackdempsey-acts_as_commentable
27
+
28
+ add the folloowing line to your environment.rb
29
+ config.gem 'jackdempsey-acts_as_commentable', :lib => 'acts_as_commentable', :source => "http://gems.github.com"
30
+
31
+
32
+ Generate your comment model:
33
+
34
+ script/generate comment
35
+
36
+ Then migrate your database:
37
+
38
+ rake db:migrate
39
+
40
+ == Usage
41
+ Merb Users:
42
+ * add 'dependency "acts_as_commentable"' to your init.rb or dependencies.rb if using merb-stack
43
+
44
+ * Make your ActiveRecord model act as commentable.
45
+
46
+ class Model < ActiveRecord::Base
47
+ acts_as_commentable
48
+ end
49
+
50
+ * Add a comment to a model instance
51
+
52
+ commentable = Model.create
53
+ commentable.comments.create(:title => "First comment.", :comment => "This is the first comment.")
54
+
55
+ * Fetch comments for a commentable model:
56
+
57
+ commentable = Model.find(1)
58
+ comments = commentable.comments.recent.limit(10).all
59
+
60
+ # Following doesn't work/make sense to me. Leaving for historical sake -- Jack
61
+ # * Each comment reference commentable object
62
+ #
63
+ # model = Model.find(1)
64
+ # model.comments.get(0).commentable == model
65
+
66
+ == Credits
67
+
68
+ Xelipe - This plugin is heavily influenced by Acts As Taggable.
69
+
70
+ == More
71
+
72
+ http://www.juixe.com/techknow/index.php/2006/06/18/acts-as-commentable-plugin/
73
+ http://www.juixe.com/projects/acts_as_commentable
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.join(File.dirname(__FILE__), 'rails', 'init')
data/install.rb ADDED
@@ -0,0 +1,2 @@
1
+ puts "To create the comment model please run:"
2
+ puts "script/generate comment"
@@ -0,0 +1,2 @@
1
+ require 'commentable_methods'
2
+ require 'comment_methods'
@@ -0,0 +1,36 @@
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.scope :in_order, comment_model.order('created_at ASC')
13
+ comment_model.scope :recent, comment_model.order('created_at DESC')
14
+ end
15
+
16
+ module Finders
17
+ # Helper class method to lookup all comments assigned
18
+ # to all commentable types for a given user.
19
+ def find_comments_by_user(user)
20
+ where(["user_id = ?", user.id]).order("created_at DESC")
21
+ end
22
+
23
+ # Helper class method to look up all comments for
24
+ # commentable class name and commentable id.
25
+ def find_comments_for_commentable(commentable_str, commentable_id)
26
+ where(["commentable_type = ? and commentable_id = ?", commentable_str, commentable_id]).order("created_at DESC")
27
+ end
28
+
29
+ # Helper class method to look up a commentable object
30
+ # given the commentable class name and id
31
+ def find_commentable(commentable_str, commentable_id)
32
+ commentable_str.constantize.find(commentable_id)
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,55 @@
1
+ require 'active_record'
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
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
+ Comment.find_comments_for_commentable(commentable, obj.id)
27
+ end
28
+
29
+ # Helper class method to lookup comments for
30
+ # the mixin commentable type written by a given user.
31
+ # This method is NOT equivalent to Comment.find_comments_for_user
32
+ def find_comments_by_user(user)
33
+ commentable = ActiveRecord::Base.send(:class_name_of_active_record_descendant, self).to_s
34
+ Comment.where(["user_id = ? and commentable_type = ?", user.id, commentable]).order("created_at DESC")
35
+ end
36
+ end
37
+
38
+ # This module contains instance methods
39
+ module InstanceMethods
40
+ # Helper method to sort comments by date
41
+ def comments_ordered_by_submitted
42
+ Comment.find_comments_for_commentable(self.class.name, id)
43
+ end
44
+
45
+ # Helper method that defaults the submitted time.
46
+ def add_comment(comment)
47
+ comments << comment
48
+ end
49
+ end
50
+
51
+ end
52
+ end
53
+ end
54
+
55
+ ActiveRecord::Base.send(:include, Juixe::Acts::Commentable)
@@ -0,0 +1,6 @@
1
+ Description:
2
+ Copies comment.rb to app/models/.
3
+ Copies create_comment.rb to db/migrate
4
+
5
+ Examples:
6
+ `rails generate comment`
@@ -0,0 +1,18 @@
1
+ require 'rails/generators/migration'
2
+
3
+ class CommentGenerator < Rails::Generators::Base
4
+ include Rails::Generators::Migration
5
+
6
+ def self.source_root
7
+ @_acts_as_commentable_source_root ||= File.expand_path("../templates", __FILE__)
8
+ end
9
+
10
+ def self.next_migration_number(path)
11
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
12
+ end
13
+
14
+ def create_model_file
15
+ template "comment.rb", "app/models/comment.rb"
16
+ migration_template "create_comments.rb", "db/migrate/create_comments.rb"
17
+ end
18
+ end
@@ -0,0 +1,15 @@
1
+ class Comment < ActiveRecord::Base
2
+
3
+ include ActsAsCommentable::Comment
4
+
5
+ belongs_to :commentable, :polymorphic => true
6
+
7
+ default_scope :order => 'created_at ASC'
8
+
9
+ # NOTE: install the acts_as_votable plugin if you
10
+ # want user to vote on the quality of comments.
11
+ #acts_as_voteable
12
+
13
+ # NOTE: Comments belong to a user
14
+ belongs_to :user
15
+ 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
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :acts_as_commentable do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts_as_commentable
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 2
7
+ - 0
8
+ - 1
9
+ version: 2.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Cosmin Radoi, Jack Dempsey, Xelipe, Chris Eppstein
13
+ autorequire: acts_as_commentable
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-09-05 00:00:00 -04:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: Plugin/gem that provides comment functionality
22
+ email: unknown@juixe.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files:
28
+ - README
29
+ - MIT-LICENSE
30
+ files:
31
+ - MIT-LICENSE
32
+ - README
33
+ - lib/acts_as_commentable.rb
34
+ - lib/comment_methods.rb
35
+ - lib/commentable_methods.rb
36
+ - lib/generators/comment/comment_generator.rb
37
+ - lib/generators/comment/templates/comment.rb
38
+ - lib/generators/comment/templates/create_comments.rb
39
+ - lib/generators/comment/USEGA
40
+ - tasks/acts_as_commentable_tasks.rake
41
+ - init.rb
42
+ - install.rb
43
+ has_rdoc: true
44
+ homepage: http://www.juixe.com/techknow/index.php/2006/06/18/acts-as-commentable-plugin/
45
+ licenses: []
46
+
47
+ post_install_message:
48
+ rdoc_options: []
49
+
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ segments:
58
+ - 0
59
+ version: "0"
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ segments:
66
+ - 0
67
+ version: "0"
68
+ requirements: []
69
+
70
+ rubyforge_project:
71
+ rubygems_version: 1.3.7
72
+ signing_key:
73
+ specification_version: 3
74
+ summary: Plugin/gem that provides comment functionality
75
+ test_files: []
76
+