Created
September 4, 2008 22:20
-
-
Save BrianTheCoder/8890 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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