Commit 63b8d8865a3ef89bb9ac6a605363a7c6262b7579

Authored by Rafael Manzo
1 parent 49f3853e

Fixed validation for project edition

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