Skip to content

Instantly share code, notes, and snippets.

@BrianTheCoder
Created September 4, 2008 22:18
Show Gist options
  • Save BrianTheCoder/8889 to your computer and use it in GitHub Desktop.
Save BrianTheCoder/8889 to your computer and use it in GitHub Desktop.
module DataMapper
module Is
module Commentable
def is_commentable
include DataMapper::Is::Commentable::InstanceMethods
has n, :comments, :child_key => [:object_id], :object_type => "#{self}"
end
module InstanceMethods
def build_comment(attrs = {})
Comment.new(attrs.merge(:object_id => self.id, :object_type => "#{self.class}"))
end
def create_comment(attrs = {})
Comment.create(attrs.merge(:object_id => self.id, :object_type => "#{self.class}"))
end
def ordered_comments
Comment.all(:object_id => self.id, :object_type => self.class, :order => [:created_at.desc])
end
end
end
end
end
# Include the plugin in Resource
module DataMapper
module Resource
module ClassMethods
include DataMapper::Is::Commentable
end # module ClassMethods
end # module Resource
end # module DataMapper
class Comment
include DataMapper::Resource
property :id, Integer, :serial => true
property :body, Text
property :created_at, DateTime
property :user_id, Integer
property :object_id, Integer
property :object_type, String
belongs_to :user
validates_present :body
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment