Guest User

Untitled

a guest
Feb 21st, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. #require 'dm-validations'
  2. #require 'dm-timestamps'
  3.  
  4. #require 'taunaki/shorten'
  5. #require 'taunaki/id_maker'
  6.  
  7. class Article
  8. include DataMapper::Resource
  9.  
  10. # include Taunaki::Shorten
  11. # include Taunaki::IdMaker
  12.  
  13. property :id, Serial
  14. property :friendly_id, String, :length => (1..255)
  15. property :title, String, :nullable => false, :length => (1..255)
  16. property :content, Text, :nullable => false, :length => (1..10_000)
  17. property :created_at, DateTime
  18. property :updated_at, DateTime
  19. property :rating, Integer, :format =>
  20. Proc.new { |r| 0 < r && r <= 5 }
  21. # Shortened version of the content, without markup, used to display on the
  22. # sides
  23. property :shortened_content, String, :length => (0..150)
  24. belongs_to :author, :class_name => 'User', :child_key => [:article_id]
  25.  
  26. # is_indexed :texts => [:title, :content, :rating]
  27.  
  28. # field_for_id :title
  29.  
  30. # before :valid?, :setup
  31. # validates_with_method :author, :check_author
  32.  
  33. def setup
  34. if self.new_record?
  35. unless self.title
  36. errors.add(:title, 'Title cannot be blank')
  37. return false
  38. end
  39. self.friendly_id = self.generate_id
  40. end
  41. self.shortened_content = self.shorten(self.content, 150)
  42. end
  43. private :setup
  44.  
  45.  
  46.  
  47. # Check that the author is a) present and is b) a contributor
  48. def check_author
  49. if !self.author
  50. return [false, "The author is missing"]
  51. else
  52. if self.author.roles.select{ |r| r.id == 'contributor' }.empty?
  53. return [false, "The author is not a contributor"]
  54. else
  55. return true
  56. end
  57. end
  58. end
  59. private :check_author
  60. end
Add Comment
Please, Sign In to add comment