Commit 63b8d8865a3ef89bb9ac6a605363a7c6262b7579
1 parent
49f3853e
Exists in
colab
and in
4 other branches
Fixed validation for project edition
Showing
3 changed files
with
4 additions
and
5 deletions
Show diff stats
app/models/validators/kalibro_uniqueness_validator.rb
| 1 | class KalibroUniquenessValidator < ActiveModel::EachValidator | 1 | class KalibroUniquenessValidator < ActiveModel::EachValidator |
| 2 | def validate_each(record, attribute, value) | 2 | def validate_each(record, attribute, value) |
| 3 | record.class.all.each do |entity| | 3 | record.class.all.each do |entity| |
| 4 | - if entity.send(attribute) == value | 4 | + if (entity.send(attribute) == value) and (entity.id != record.id) |
| 5 | record.errors[attribute] << "There's already a #{record.class} with #{attribute} #{value}! Please, choose another one." | 5 | record.errors[attribute] << "There's already a #{record.class} with #{attribute} #{value}! Please, choose another one." |
| 6 | break | 6 | break |
| 7 | end | 7 | end |
features/project/edition.feature
| @@ -45,7 +45,7 @@ Feature: Project | @@ -45,7 +45,7 @@ Feature: Project | ||
| 45 | When I press the Update button | 45 | When I press the Update button |
| 46 | Then I should see There's already | 46 | Then I should see There's already |
| 47 | 47 | ||
| 48 | - @kalibro_restart @wip | 48 | + @kalibro_restart |
| 49 | Scenario: Editing just the description | 49 | Scenario: Editing just the description |
| 50 | Given I am a regular user | 50 | Given I am a regular user |
| 51 | And I am signed in | 51 | And I am signed in |
| @@ -53,7 +53,6 @@ Feature: Project | @@ -53,7 +53,6 @@ Feature: Project | ||
| 53 | And I am at the sample project edit page | 53 | And I am at the sample project edit page |
| 54 | And I fill the Description field with "Web Service to collect metrics" | 54 | And I fill the Description field with "Web Service to collect metrics" |
| 55 | When I press the Update button | 55 | When I press the Update button |
| 56 | - Then I should see Kalibro | ||
| 57 | And I should see Web Service to collect metrics | 56 | And I should see Web Service to collect metrics |
| 58 | 57 | ||
| 59 | @kalibro_restart | 58 | @kalibro_restart |
spec/models/validators/kalibro_uniqueness_validator_spec.rb
| @@ -16,10 +16,10 @@ describe KalibroUniquenessValidator do | @@ -16,10 +16,10 @@ describe KalibroUniquenessValidator do | ||
| 16 | end | 16 | end |
| 17 | end | 17 | end |
| 18 | 18 | ||
| 19 | - context 'with name already taken' do | 19 | + context 'with name already taken by another project' do |
| 20 | before :each do | 20 | before :each do |
| 21 | @subject = FactoryGirl.build(:project) | 21 | @subject = FactoryGirl.build(:project) |
| 22 | - Project.expects(:all).returns([@subject]) | 22 | + Project.expects(:all).returns([FactoryGirl.build(:project, id: @subject.id + 1)]) |
| 23 | end | 23 | end |
| 24 | 24 | ||
| 25 | it 'should contain errors' do | 25 | it 'should contain errors' do |