Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
Add Comment
Please, Sign In to add comment