Guest User

Untitled

a guest
Feb 19th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. module DataMapper
  2. module Is
  3. module Commentable
  4. def is_commentable
  5. include DataMapper::Is::Commentable::InstanceMethods
  6. has n, :comments, :child_key => [:object_id], :object_type => "#{self}"
  7. end
  8.  
  9. module InstanceMethods
  10. def build_comment(attrs = {})
  11. Comment.new(attrs.merge(:object_id => self.id, :object_type => "#{self.class}"))
  12. end
  13.  
  14. def create_comment(attrs = {})
  15. Comment.create(attrs.merge(:object_id => self.id, :object_type => "#{self.class}"))
  16. end
  17.  
  18. def ordered_comments
  19. Comment.all(:object_id => self.id, :object_type => self.class, :order => [:created_at.desc])
  20. end
  21. end
  22. end
  23. end
  24. end
  25.  
  26. # Include the plugin in Resource
  27. module DataMapper
  28. module Resource
  29. module ClassMethods
  30. include DataMapper::Is::Commentable
  31. end # module ClassMethods
  32. end # module Resource
  33. end # module DataMapper
  34.  
  35.  
  36. class Comment
  37. include DataMapper::Resource
  38.  
  39. property :id, Integer, :serial => true
  40. property :body, Text
  41. property :created_at, DateTime
  42. property :user_id, Integer
  43. property :object_id, Integer
  44. property :object_type, String
  45.  
  46. belongs_to :user
  47.  
  48. validates_present :body
  49. end
Add Comment
Please, Sign In to add comment