Hoodow-acts_as_commentable 2.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
@@ -0,0 +1,13 @@
1
+ class CommentGenerator < Rails::Generator::Base
2
+ def manifest
3
+ record do |m|
4
+ m.directory 'app/models'
5
+ m.file 'comment.rb', 'app/models/comment.rb'
6
+ m.migration_template "create_comments.rb", "db/migrate"
7
+ end
8
+ end
9
+ # ick what a hack.
10
+ def file_name
11
+ "create_comments"
12
+ end
13
+ end
@@ -0,0 +1,14 @@
1
+ class Comment < ActiveRecord::Base
2
+
3
+ include ActsAsCommentable::Comment
4
+
5
+ belongs_to :commentable, :polymorphic => true
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
+ 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
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.join(File.dirname(__FILE__), 'rails', 'init')
@@ -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,43 @@
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.named_scope :in_order, {:order => 'created_at ASC'}
13
+ comment_model.named_scope :recent, {:order => "created_at DESC"}
14
+ comment_model.named_scope :limit, lambda {|limit| {:limit => limit}}
15
+ end
16
+
17
+ module Finders
18
+ # Helper class method to lookup all comments assigned
19
+ # to all commentable types for a given user.
20
+ def find_comments_by_user(user)
21
+ find(:all,
22
+ :conditions => ["user_id = ?", user.id],
23
+ :order => "created_at DESC"
24
+ )
25
+ end
26
+
27
+ # Helper class method to look up all comments for
28
+ # commentable class name and commentable id.
29
+ def find_comments_for_commentable(commentable_str, commentable_id)
30
+ find(:all,
31
+ :conditions => ["commentable_type = ? and commentable_id = ?", commentable_str, commentable_id],
32
+ :order => "created_at DESC"
33
+ )
34
+ end
35
+
36
+ # Helper class method to look up a commentable object
37
+ # given the commentable class name and id
38
+ def find_commentable(commentable_str, commentable_id)
39
+ commentable_str.constantize.find(commentable_id)
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,66 @@
1
+ require 'activerecord'
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, :order => 'created_at ASC'
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
+
27
+ Comment.find(:all,
28
+ :conditions => ["commentable_id = ? and commentable_type = ?", obj.id, commentable],
29
+ :order => "created_at DESC"
30
+ )
31
+ end
32
+
33
+ # Helper class method to lookup comments for
34
+ # the mixin commentable type written by a given user.
35
+ # This method is NOT equivalent to Comment.find_comments_for_user
36
+ def find_comments_by_user(user)
37
+ commentable = ActiveRecord::Base.send(:class_name_of_active_record_descendant, self).to_s
38
+
39
+ Comment.find(:all,
40
+ :conditions => ["user_id = ? and commentable_type = ?", user.id, commentable],
41
+ :order => "created_at DESC"
42
+ )
43
+ end
44
+ end
45
+
46
+ # This module contains instance methods
47
+ module InstanceMethods
48
+ # Helper method to sort comments by date
49
+ def comments_ordered_by_submitted
50
+ Comment.find(:all,
51
+ :conditions => ["commentable_id = ? and commentable_type = ?", id, self.class.name],
52
+ :order => "created_at DESC"
53
+ )
54
+ end
55
+
56
+ # Helper method that defaults the submitted time.
57
+ def add_comment(comment)
58
+ comments << comment
59
+ end
60
+ end
61
+
62
+ end
63
+ end
64
+ end
65
+
66
+ ActiveRecord::Base.send(:include, Juixe::Acts::Commentable)
@@ -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,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: Hoodow-acts_as_commentable
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Cosmin Radoi, Jack Dempsey, Xelipe, Chris Eppstein
8
+ autorequire: acts_as_commentable
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-07-01 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Plugin/gem that provides comment functionality
17
+ email: unknown@juixe.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README
24
+ - MIT-LICENSE
25
+ files:
26
+ - MIT-LICENSE
27
+ - README
28
+ - generators/comment
29
+ - generators/comment/comment_generator.rb
30
+ - generators/comment/templates
31
+ - generators/comment/templates/comment.rb
32
+ - generators/comment/templates/create_comments.rb
33
+ - lib/acts_as_commentable.rb
34
+ - lib/comment_methods.rb
35
+ - lib/commentable_methods.rb
36
+ - tasks/acts_as_commentable_tasks.rake
37
+ - init.rb
38
+ - install.rb
39
+ has_rdoc: false
40
+ homepage: http://www.juixe.com/techknow/index.php/2006/06/18/acts-as-commentable-plugin/
41
+ post_install_message:
42
+ rdoc_options: []
43
+
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: "0"
51
+ version:
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ version:
58
+ requirements: []
59
+
60
+ rubyforge_project:
61
+ rubygems_version: 1.2.0
62
+ signing_key:
63
+ specification_version: 3
64
+ summary: Plugin/gem that provides comment functionality
65
+ test_files: []
66
+