Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #require 'dm-validations'
- #require 'dm-timestamps'
- #require 'taunaki/shorten'
- #require 'taunaki/id_maker'
- class Article
- include DataMapper::Resource
- # include Taunaki::Shorten
- # include Taunaki::IdMaker
- property :id, Serial
- property :friendly_id, String, :length => (1..255)
- property :title, String, :nullable => false, :length => (1..255)
- property :content, Text, :nullable => false, :length => (1..10_000)
- property :created_at, DateTime
- property :updated_at, DateTime
- property :rating, Integer, :format =>
- Proc.new { |r| 0 < r && r <= 5 }
- # Shortened version of the content, without markup, used to display on the
- # sides
- property :shortened_content, String, :length => (0..150)
- belongs_to :author, :class_name => 'User', :child_key => [:article_id]
- # is_indexed :texts => [:title, :content, :rating]
- # field_for_id :title
- # before :valid?, :setup
- # validates_with_method :author, :check_author
- def setup
- if self.new_record?
- unless self.title
- errors.add(:title, 'Title cannot be blank')
- return false
- end
- self.friendly_id = self.generate_id
- end
- self.shortened_content = self.shorten(self.content, 150)
- end
- private :setup
- # Check that the author is a) present and is b) a contributor
- def check_author
- if !self.author
- return [false, "The author is missing"]
- else
- if self.author.roles.select{ |r| r.id == 'contributor' }.empty?
- return [false, "The author is not a contributor"]
- else
- return true
- end
- end
- end
- private :check_author
- end
Add Comment
Please, Sign In to add comment