Class: Title::Validator
- Inherits:
-
ActiveModel::Validator
- Object
- ActiveModel::Validator
- Title::Validator
- Defined in:
- lib/ecrire/app/models/title.rb
Instance Method Summary collapse
- #validate(record) ⇒ Object
- #validate_draft!(record) ⇒ Object
- #validate_length!(record) ⇒ Object
- #validate_uniqueness!(record) ⇒ Object
Instance Method Details
#validate(record) ⇒ Object
3 4 5 6 7 |
# 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) ⇒ Object
31 32 33 34 35 36 37 |
# 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) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 |
# 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) ⇒ Object
21 22 23 24 25 26 27 28 29 |
# 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 |