class Title::Validator
Public Instance Methods
validate(record)
click to toggle source
# File lib/ecrire/app/models/title.rb, line 3 def validate(record) validate_length! record validate_uniqueness! record validate_draft! record end
validate_draft!(record)
click to toggle source
# File lib/ecrire/app/models/title.rb, line 31 def validate_draft!(record) if record.post.published? && !record.new_record? msg = "You cannot modify an existing title when a post is published" record.errors['draft'] << msg record.post.errors['title'] << msg end end
validate_length!(record)
click to toggle source
# File lib/ecrire/app/models/title.rb, line 9 def validate_length!(record) if record.name.blank? msg = "Your title can't be blank." record.errors['base'] << msg record.post.errors['title'] << msg elsif record.name.length < 1 msg = "Your title needs to be at least 1 character long." record.errors['base'] << msg record.post.errors['title'] << msg end end
validate_uniqueness!(record)
click to toggle source
# File lib/ecrire/app/models/title.rb, line 21 def validate_uniqueness!(record) title = Title.where('titles.slug = ? OR titles.name = ?', record.slug, record.name).first unless title.nil? msg = "You already have a post with this title: #{title.name}" record.errors['uniqueness'] << msg record.post.errors['title'] << msg return end end