Skip to content

Commit

Permalink
All specs pass already. Awesome.
Browse files Browse the repository at this point in the history
  • Loading branch information
Evan David Light committed Nov 16, 2008
1 parent 6fdbc68 commit 7e57ef8
Show file tree
Hide file tree
Showing 15 changed files with 1,153 additions and 0 deletions.
Empty file added CHANGELOG
Empty file.
20 changes: 20 additions & 0 deletions MIT-LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Copyright (c) 2008 Evan David Light

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
75 changes: 75 additions & 0 deletions README
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
Acts As Commentable
=================

Allows for threaded comments to be added to multiple and different models. Drop-in compatible for acts_as_commentable (however requiring a database schema change)

== Resources

Install

Rails

* To install as a plugin:

script/plugin install http://github.com/jackdempsey/acts_as_commentable

Merb/Rails

* To install as a gem:

rake install

* Create a new rails migration and add the following self.up and self.down methods

def self.up
create_table "comments", :force => true do |t|
t.integer "commentable_id", :default => 0
t.string "commentable_type", :limit => 15, :default => ""
t.string "title", :default => ""
t.text "body", :default => ""
t.string "subject", :default => ""
t.integer "user_id", :default => 0, :null => false
t.integer "parent_id"
t.integer "lft"
t.integer "rgt"
t.timestamps
end

add_index "comments", "user_id"
add_index "comments", "commentable_id"
end

def self.down
drop_table :comments
end

== Usage
class Model < ActiveRecord::Base
acts_as_commentable_with_threading
end

* Add a comment to a model instance

model = Model.new
comment = Comment.new(:model => model, :body => "Your comment text here")
comment.save!
comment.move_to_child_of(the_desired_parent_comment)

# Following doesn't work/make sense to me. Leaving for historical sake -- Jack
# * Each comment reference commentable object
#
# model = Model.find(1)
# model.comments.get(0).commentable == model

== Credits

Jack Dempsey - This plugin/gem is heavily influenced/liberally borrowed/stole from acts_as_commentable

which in turn credits....

Xelipe - Because acts_as_commentable was heavily influenced by Acts As Taggable.

== More

http://triple-dog-dare.com
http://evan.tiggerpalace.com
28 changes: 28 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require 'rubygems'
require 'rake/gempackagetask'
require 'spec'
require 'spec/rake/spectask'

PLUGIN = "acts_as_commentable_with_threading"
NAME = "acts_as_commentable_with_threading"
GEM_VERSION = "1.0.0"
AUTHOR = "Evan Light"
EMAIL = "evan@triple-dog-dare.com"
SUMMARY = "Plugin/gem that provides threaded comment functionality"

load 'acts_as_commentable_with_threading.gemspec'
spec = ACTS_AS_COMMENTABLE_WITH_THREADING

Rake::GemPackageTask.new(spec) do |pkg|
pkg.gem_spec = spec
end

Spec::Rake::SpecTask.new do |t|
t.spec_files = FileList['spec/**_spec.rb']
end

task :install => [:package] do
sh %{sudo gem install pkg/#{NAME}-#{GEM_VERSION}}
end

task :default => :spec
25 changes: 25 additions & 0 deletions acts_as_commentable_with_threading.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
ACTS_AS_COMMENTABLE_WITH_THREADING = Gem::Specification.new do |s|
s.name = "acts_as_commentable_with_threading"
s.version = "0.1.0"
s.date = "2008-11-16"
s.summary = "Polymorphic comments Rails plugin"
s.email = "evan@tiggerpalace.com"
s.homepage = "http://github.com/elight/acts_as_commentable_with_threading"
s.description = "Polymorphic threaded comments Rails plugin/gem"
s.has_rdoc = true
s.authors = ["Evan Light", "Jack Dempsey", "Xelipe"]
s.files = [
"CHANGELOG",
"MIT-LICENSE",
"README",
"Rakefile",
"acts_as_commentable_with_threading.gemspec",
"init.rb",
"install.rb",
"lib/acts_as_commentable_with_threading.rb",
"lib/comment.rb"]
s.test_files = ["spec/acts_as_commentable_test.rb"]
s.rdoc_options = ["--main", "README"]

s.add_dependency 'awesome_nested_set'
end
3 changes: 3 additions & 0 deletions init.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Include hook code here
require 'acts_as_commentable_with_threading'

1 change: 1 addition & 0 deletions install.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Install hook code here
69 changes: 69 additions & 0 deletions lib/acts_as_commentable_with_threading.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
require 'activerecord'
require 'awesome_nested_set'
ActiveRecord::Base.class_eval do
include CollectiveIdea::Acts::NestedSet
end
require 'comment'

# ActsAsCommentableWithThreading
module Acts #:nodoc:
module CommentableWithThreading #:nodoc:

def self.included(base)
base.extend ClassMethods
end

module ClassMethods
def acts_as_commentable
has_many :root_comments, :class_name => "Comment", :as => :commentable, :dependent => :destroy, :order => 'created_at ASC'
include Acts::CommentableWithThreading::InstanceMethods
extend Acts::CommentableWithThreading::SingletonMethods
end
end

# This module contains class methods
module SingletonMethods
# Helper method to lookup for comments for a given object.
# This method is equivalent to obj.comments.
def find_comments_for(obj)
commentable = ActiveRecord::Base.send(:class_name_of_active_record_descendant, self).to_s

Comment.find(:all,
:conditions => ["commentable_id = ? and commentable_type = ?", obj.id, commentable],
:order => "created_at DESC"
)
end

# Helper class method to lookup comments for
# the mixin commentable type written by a given user.
# This method is NOT equivalent to Comment.find_comments_for_user
def find_comments_by_user(user)
commentable = ActiveRecord::Base.send(:class_name_of_active_record_descendant, self).to_s

Comment.find(:all,
:conditions => ["user_id = ? and commentable_type = ?", user.id, commentable],
:order => "created_at DESC"
)
end
end

# This module contains instance methods
module InstanceMethods
# Helper method to sort comments by date
def comments_ordered_by_submitted
Comment.find(:all,
:conditions => ["commentable_id = ? and commentable_type = ?", id, self.type.name],
:order => "created_at DESC"
)
end

# Helper method that defaults the submitted time.
def add_comment(comment)
comments << comment
end
end

end
end

ActiveRecord::Base.send(:include, Acts::CommentableWithThreading)
37 changes: 37 additions & 0 deletions lib/comment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
class Comment < ActiveRecord::Base
acts_as_nested_set :scope => [:commentable_id, :commentable_type]

validates_presence_of :body
validates_presence_of :user

# NOTE: install the acts_as_votable plugin if you
# want user to vote on the quality of comments.
#acts_as_voteable

# NOTE: Comments belong to a user
belongs_to :user

# Helper class method to lookup all comments assigned
# to all commentable types for a given user.
def self.find_comments_by_user(user)
find(:all,
:conditions => ["user_id = ?", user.id],
:order => "created_at DESC"
)
end

# Helper class method to look up all comments for
# commentable class name and commentable id.
def self.find_comments_for_commentable(commentable_str, commentable_id)
find(:all,
:conditions => ["commentable_type = ? and commentable_id = ?", commentable_str, commentable_id],
:order => "created_at DESC"
)
end

# Helper class method to look up a commentable object
# given the commentable class name and id
def self.find_commentable(commentable_str, commentable_id)
commentable_str.constantize.find(commentable_id)
end
end
49 changes: 49 additions & 0 deletions spec/comment_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
require File.join(File.dirname(__FILE__), "spec_helper")

# Specs some of the behavior of awesome_nested_set although does so to demonstrate the use of this gem
describe Comment do
before do
@user = User.create!
@comment = Comment.create!(:body => "Root comment", :user => @user)
end

describe "that is valid" do
it "should have a user" do
@comment.user.should_not be_nil
end

it "should have a body" do
@comment.body.should_not be_nil
end
end

it "should not have a parent if it is a root Comment" do
@comment.parent.should be_nil
end

it "can have see how child Comments it has" do
@comment.children.size.should == 0
end

it "can add child Comments" do
grandchild = Comment.new(:body => "This is a grandchild", :user => @user)
grandchild.save!
grandchild.move_to_child_of(@comment)
@comment.children.size.should == 1
end

describe "after having a child added" do
before do
@child = Comment.create!(:body => "Child comment", :user => @user)
@child.move_to_child_of(@comment)
end

it "can be referenced by its child" do
@child.parent.should == @comment
end

it "can see its child" do
@comment.children.first.should == @child
end
end
end
7 changes: 7 additions & 0 deletions spec/commentable_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require File.join(File.dirname(__FILE__), "spec_helper")

describe "A class that is commentable" do
it "can have many root comments" do
Commentable.new.root_comments.should be_a_kind_of(Enumerable)
end
end
18 changes: 18 additions & 0 deletions spec/db/database.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
sqlite3:
adapter: sqlite3
dbfile: acts_as_commentable_with_threading.sqlite3.db
sqlite3mem:
:adapter: sqlite3
:dbfile: ":memory:"
postgresql:
:adapter: postgresql
:username: postgres
:password: postgres
:database: acts_as_commentable_with_threading_plugin_test
:min_messages: ERROR
mysql:
:adapter: mysql
:host: localhost
:username: root
:password:
:database: acts_as_commentable_with_threading_plugin_test
25 changes: 25 additions & 0 deletions spec/db/schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
ActiveRecord::Schema.define(:version => 0) do
create_table "users", :force => true do |t|
t.timestamps
end

create_table "commentables", :force => true do |t|
t.timestamps
end

create_table "comments", :force => true do |t|
t.integer "commentable_id", :default => 0
t.string "commentable_type", :limit => 15, :default => ""
t.string "title", :default => ""
t.text "body", :default => ""
t.string "subject", :default => ""
t.integer "user_id", :default => 0, :null => false
t.integer "parent_id"
t.integer "lft"
t.integer "rgt"
t.timestamps
end

add_index "comments", "user_id"
add_index "comments", "commentable_id"
end
Loading

0 comments on commit 7e57ef8

Please sign in to comment.