Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
make all module compatible with rails3
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhu committed Mar 6, 2010
1 parent f50add6 commit e38ce90
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 27 deletions.
17 changes: 5 additions & 12 deletions lib/comment_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,21 @@ module Comment

def self.included(comment_model)
comment_model.extend Finders
comment_model.named_scope :in_order, {:order => 'created_at ASC'}
comment_model.named_scope :recent, {:order => "created_at DESC"}
comment_model.named_scope :limit, lambda {|limit| {:limit => limit}}
comment_model.scope :in_order, comment_model.order('created_at ASC')
comment_model.scope :recent, comment_model.order('created_at DESC')
end

module Finders
# Helper class method to lookup all comments assigned
# to all commentable types for a given user.
def find_comments_by_user(user)
find(:all,
:conditions => ["user_id = ?", user.id],
:order => "created_at DESC"
)
where(["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 find_comments_for_commentable(commentable_str, commentable_id)
find(:all,
:conditions => ["commentable_type = ? and commentable_id = ?", commentable_str, commentable_id],
:order => "created_at DESC"
)
where(["commentable_type = ? and commentable_id = ?", commentable_str, commentable_id]).order("created_at DESC")
end

# Helper class method to look up a commentable object
Expand All @@ -40,4 +33,4 @@ def find_commentable(commentable_str, commentable_id)
end
end
end
end
end
17 changes: 3 additions & 14 deletions lib/commentable_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,34 +23,23 @@ module SingletonMethods
# 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"
)
Comment.find_comments_for_commentable(commentable, obj.id)
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"
)
Comment.where(["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.class.name],
:order => "created_at DESC"
)
Comment.find_comments_for_commentable(self.class.name, id)
end

# Helper method that defaults the submitted time.
Expand Down
1 change: 0 additions & 1 deletion lib/generators/comment/templates/comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,4 @@ class Comment < ActiveRecord::Base

# NOTE: Comments belong to a user
belongs_to :user

end

0 comments on commit e38ce90

Please sign in to comment.