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 | 3 | class Project < KalibroEntities::Entities::Project |
2 | 4 | include ActiveModel::Validations |
3 | 5 | include ActiveModel::Conversion |
4 | 6 | extend ActiveModel::Naming |
5 | 7 | delegate :url_helpers, to: 'Rails.application.routes' |
6 | 8 | |
9 | + attr_accessor :name | |
10 | + validates :name, presence: true, kalibro_uniqueness: true | |
11 | + | |
7 | 12 | def persisted? |
8 | 13 | Project.exists?(self.id) unless self.id.nil? |
9 | 14 | end |
... | ... | @@ -16,4 +21,12 @@ class Project < KalibroEntities::Entities::Project |
16 | 21 | def self.latest(count = 1) |
17 | 22 | all.sort { |a,b| b.id <=> a.id }.first(count) |
18 | 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 | 32 | end | ... | ... |
... | ... | @@ -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 | ... | ... |