Commit 02e15f4c6cd275b86fee2b138429490943c71b8f
Committed by
Rafael Manzo
1 parent
a9e7f977
Exists in
colab
and in
4 other branches
Finished validations for project form.
Missing tests.
Showing
2 changed files
with
22 additions
and
0 deletions
Show diff stats
app/models/project.rb
| 1 | +require "validators/kalibro_uniqueness_validator.rb" | ||
| 2 | + | ||
| 1 | class Project < KalibroEntities::Entities::Project | 3 | class Project < KalibroEntities::Entities::Project |
| 2 | include ActiveModel::Validations | 4 | include ActiveModel::Validations |
| 3 | include ActiveModel::Conversion | 5 | include ActiveModel::Conversion |
| 4 | extend ActiveModel::Naming | 6 | extend ActiveModel::Naming |
| 5 | delegate :url_helpers, to: 'Rails.application.routes' | 7 | delegate :url_helpers, to: 'Rails.application.routes' |
| 6 | 8 | ||
| 9 | + attr_accessor :name | ||
| 10 | + validates :name, presence: true, kalibro_uniqueness: true | ||
| 11 | + | ||
| 7 | def persisted? | 12 | def persisted? |
| 8 | Project.exists?(self.id) unless self.id.nil? | 13 | Project.exists?(self.id) unless self.id.nil? |
| 9 | end | 14 | end |
| @@ -16,4 +21,12 @@ class Project < KalibroEntities::Entities::Project | @@ -16,4 +21,12 @@ class Project < KalibroEntities::Entities::Project | ||
| 16 | def self.latest(count = 1) | 21 | def self.latest(count = 1) |
| 17 | all.sort { |a,b| b.id <=> a.id }.first(count) | 22 | all.sort { |a,b| b.id <=> a.id }.first(count) |
| 18 | end | 23 | end |
| 24 | + | ||
| 25 | + def save | ||
| 26 | + if (self.valid? and self.kalibro_errors.nil?) | ||
| 27 | + super.save | ||
| 28 | + else | ||
| 29 | + false | ||
| 30 | + end | ||
| 31 | + end | ||
| 19 | end | 32 | end |
| @@ -0,0 +1,9 @@ | @@ -0,0 +1,9 @@ | ||
| 1 | +class KalibroUniquenessValidator < ActiveModel::EachValidator | ||
| 2 | + def validate_each(record, attribute, value) | ||
| 3 | + record.class.all.each do |entity| | ||
| 4 | + if entity.send(attribute) == value | ||
| 5 | + record.kalibro_errors << "There's already a #{record.class} with this #{attribute}! Please, choose another one." | ||
| 6 | + end | ||
| 7 | + end | ||
| 8 | + end | ||
| 9 | +end |