Skip to content

Commit

Permalink
further conversion to arel. more specs. generators not updated yet.
Browse files Browse the repository at this point in the history
we have it anyway. we might as well use it.

update generators for rails 3
  • Loading branch information
mpd committed Sep 25, 2010
1 parent 486c92d commit f70a263
Show file tree
Hide file tree
Showing 11 changed files with 148 additions and 73 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,3 +1,4 @@
.idea/
.bundle/
spec/debug.log
*.gem
2 changes: 2 additions & 0 deletions Gemfile.lock
Expand Up @@ -3,6 +3,7 @@ PATH
specs:
acts_as_commentable_with_threading (0.1.1)
activerecord (~> 3.0)
activesupport (~> 3.0)
moretea-awesome_nested_set

GEM
Expand Down Expand Up @@ -80,6 +81,7 @@ PLATFORMS

DEPENDENCIES
activerecord (~> 3.0)
activesupport (~> 3.0)
acts_as_commentable_with_threading!
bundler (~> 1.0)
moretea-awesome_nested_set
Expand Down
1 change: 1 addition & 0 deletions acts_as_commentable_with_threading.gemspec
Expand Up @@ -28,5 +28,6 @@ ACTS_AS_COMMENTABLE_WITH_THREADING = Gem::Specification.new do |s|
s.add_development_dependency 'rails', '~> 3.0'

s.add_dependency 'activerecord', '~> 3.0'
s.add_dependency 'activesupport', '~> 3.0'
s.add_dependency 'moretea-awesome_nested_set'
end
2 changes: 1 addition & 1 deletion generators/acts_as_commentable_upgrade_migration/USAGE
@@ -1,5 +1,5 @@
Description:
run ./script/generate acts_as_commentable_upgrade_migration
run rails generate acts_as_commentable_upgrade_migration

no need to specify any parameters after acts_as_commentable_upgrade_migration as the generator just creates a migration to update your existing comments table
the acts_as_commentable_upgrade_migration file will be created in db/migrate
@@ -1,11 +1,18 @@
class ActsAsCommentableUpgradeMigrationGenerator < Rails::Generator::Base
def manifest
record do |m|
m.migration_template 'migration.rb', 'db/migrate'
class ActsAsCommentableUpgradeMigrationGenerator < Rails::Generators::Base
include Rails::Generators::Migration

source_root File.expand_path('../templates', __FILE__)

# *for the life of me*, i can't figure out why this isn't implemented elsewhere.
def self.next_migration_number(dirname)
if ActiveRecord::Base.timestamped_migrations
Time.now.utc.strftime("%Y%m%d%H%M%S")
else
"%.3d" % (current_migration_number(dirname) + 1)
end
end
def file_name
"acts_as_commentable_upgrade_migration"

def manifest
migration_template 'migration.rb', 'db/migrate/acts_as_commentable_upgrade_migration'
end
end
@@ -1,5 +1,5 @@
Description:
run ./script/generate acts_as_commentable_with_threading_migration
run rails generate acts_as_commentable_with_threading_migration

no need to specify any parameters after acts_as_commentable_with_threading_migration as the plugin relies on the table being named "comments"
the acts_as_commentable_with_threading_migration file will be created in db/migrate
@@ -1,11 +1,18 @@
class ActsAsCommentableWithThreadingMigrationGenerator < Rails::Generator::Base
def manifest
record do |m|
m.migration_template 'migration.rb', 'db/migrate'
class ActsAsCommentableWithThreadingMigrationGenerator < Rails::Generators::Base
include Rails::Generators::Migration

source_root File.expand_path('../templates', __FILE__)

# Complete wtf that this isn't provided elsewhere.
def self.next_migration_number(dirname)
if ActiveRecord::Base.timestamped_migrations
Time.now.utc.strftime("%Y%m%d%H%M%S")
else
"%.3d" % (current_migration_number(dirname) + 1)
end
end
def file_name
"acts_as_commentable_with_threading_migration"

def manifest
migration_template 'migration.rb', 'db/migrate/acts_as_commentable_with_threading_migration'
end
end
90 changes: 37 additions & 53 deletions lib/acts_as_commentable_with_threading.rb
Expand Up @@ -9,67 +9,51 @@
# ActsAsCommentableWithThreading
module Acts #:nodoc:
module CommentableWithThreading #:nodoc:
extend ActiveSupport::Concern

def self.included(base)
base.extend ClassMethods
module ClassMethods
def acts_as_commentable
has_many :comment_threads, :class_name => "Comment", :as => :commentable, :dependent => :destroy, :order => 'created_at ASC'
include Acts::CommentableWithThreading::LocalInstanceMethods
extend Acts::CommentableWithThreading::SingletonMethods
end

module ClassMethods
def acts_as_commentable
has_many :comment_threads, :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)
Comment.where(:commentable_id => obj.id, :commentable_type => obj.class).order('created_at DESC')
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
# 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 = self.base_class.name.to_s
Comment.where(:user_id => user.id, :commentable_type => commentable).order('created_at DESC')
end
end

module LocalInstanceMethods

# This module contains instance methods
module InstanceMethods

# Helper method to display only root threads, no children/replies
def root_comments
self.comment_threads.find(:all, :conditions => {:parent_id => nil})
end

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

# Helper method that defaults the submitted time.
def add_comment(comment)
comments << comment
end
# Helper method to display only root threads, no children/replies
def root_comments
self.comment_threads.where(:parent_id => nil)
end

# Helper method to sort comments by date
def comments_ordered_by_submitted
Comment.where(:commentable_id => id, :commentable_type => self.class.name).order('created_at DESC')
end

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

end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/comment_spec.rb
@@ -1,4 +1,4 @@
require File.join(File.dirname(__FILE__), "spec_helper")
require File.expand_path('./spec_helper', File.dirname(__FILE__))

# Specs some of the behavior of awesome_nested_set although does so to demonstrate the use of this gem
describe Comment do
Expand Down
75 changes: 73 additions & 2 deletions spec/commentable_spec.rb
@@ -1,7 +1,78 @@
require File.join(File.dirname(__FILE__), "spec_helper")
require File.expand_path('./spec_helper', File.dirname(__FILE__))

describe "A class that is commentable" do
it "can have many root comments" do
Commentable.new.comment_threads.should be_a_kind_of(Enumerable)
end
end

describe "special class finders" do
before :each do
@user = User.create!
@commentable = Commentable.create!
@other_commentable = Commentable.create!
end

describe "#find_comments_for" do
before :each do
@comment = Comment.create!(:user => @user, :commentable_type => @commentable.class, :commentable_id => @commentable.id, :body => 'blargh')

@other_comment = Comment.create!(:user => @user, :commentable_type => @other_commentable.class, :commentable_id => @other_commentable.id, :body => 'hello')

@comments = Commentable.find_comments_for(@commentable)
end

it "should return the comments for the passed commentable" do
@comments.should include(@comment)
end

it "should not return the comments for other commentables" do
@comments.should_not include(@other_comment)
end
end

describe "#find_comments_by_user" do
before :each do
@user2 = User.create!

@comment = Comment.create!(:user => @user, :commentable_type => @commentable.class, :commentable_id => @commentable.id, :body => 'blargh')

@other_comment = Comment.create!(:user => @user2, :commentable_type => @other_commentable.class, :commentable_id => @other_commentable.id, :body => 'hello')

@comments = Commentable.find_comments_by_user(@user)
end

it "should return comments by the passed user" do
@comments.all? { |c| c.user == @user }.should be_true
end


it "should not return comments by other users" do
@comments.any? { |c| c.user != @user }.should be_false
end
end
end

describe "instance methods" do
describe "#comments_ordered_by_submitted" do
before :each do
@user = User.create!
@commentable = Commentable.create!
@other_commentable = Commentable.create!
@comment = Comment.create!(:user => @user, :commentable_type => @commentable.class, :commentable_id => @commentable.id, :body => 'sup')
@older_comment = Comment.create!(:user => @user, :commentable_type => @commentable.class, :commentable_id => @commentable.id, :body => 'sup', :created_at => 1.week.ago)
@oldest_comment = Comment.create!(:user => @user, :commentable_type => @commentable.class, :commentable_id => @commentable.id, :body => 'sup', :created_at => 2.years.ago)
@other_comment = Comment.create!(:user => @user, :commentable_type => @other_commentable.class, :commentable_id => @other_commentable.id, :body => 'sup')
@comments = @commentable.comments_ordered_by_submitted
end

it "should return its own comments, ordered with the newest first" do
@comments.all? { |c| c.commentable_type == @commentable.class.to_s and c.commentable_id == @commentable.id }.should be_true
@comments.each_cons(2) { |c, c2| c.created_at.should > c2.created_at }
end

it "should not include comments for other commentables" do
@comments.any? { |c| c.commentable_type != @commentable.class.to_s or c.commentable_id != @commentable.id }.should be_false
end
end
end
end
4 changes: 3 additions & 1 deletion spec/spec_helper.rb
Expand Up @@ -8,7 +8,7 @@

ActiveRecord::Base.logger = Logger.new(File.join(plugin_test_dir, "debug.log"))

ActiveRecord::Base.configurations = YAML::load(IO.read(File.join(plugin_test_dir, "db", "database.yml")))
ActiveRecord::Base.configurations = YAML::load_file(File.join(plugin_test_dir, "db", "database.yml"))
ActiveRecord::Base.establish_connection(ENV["DB"] || "sqlite3mem")
ActiveRecord::Migration.verbose = false
load(File.join(plugin_test_dir, "db", "schema.rb"))
Expand All @@ -22,3 +22,5 @@ class User < ActiveRecord::Base
class Commentable < ActiveRecord::Base
acts_as_commentable
end

ActiveRecord::Base.send(:include, Acts::CommentableWithThreading)

0 comments on commit f70a263

Please sign in to comment.