Commit 924726cd248ffba85ad77ed0e5bc2b444b7f0039

Authored by Rafael Manzo
1 parent 4e5d6969

Fixed metric configuration validations

app/models/metric_configuration.rb
  1 +require "validators/code_uniqueness_validator.rb"
  2 +
1 3 class MetricConfiguration < KalibroGem::Entities::MetricConfiguration
2 4 include KalibroRecord
3 5  
4   - attr_accessor :code, :weight, :aggregation_form, :metric_name, :metric_scope, :metric_origin
  6 + attr_accessor :code, :weight, :aggregation_form
5 7  
6   - validates :code, presence: true, kalibro_uniqueness: true
  8 + validates :code, presence: true, code_uniqueness: true
7 9 validates :weight, presence: true
8 10 validates :aggregation_form, presence: true
9   - validates :metric_name, presence: true
10   - validates :metric_scope, presence: true
11   - validates :metric_origin, presence: true
12 11  
13 12 end
... ...
app/models/validators/code_uniqueness_validator.rb 0 → 100644
... ... @@ -0,0 +1,10 @@
  1 +class CodeUniquenessValidator < ActiveModel::EachValidator
  2 + def validate_each(record, attribute, value)
  3 + record.class.metric_configurations_of(record.configuration_id).each do |metric_configuration|
  4 + if metric_configuration.code == value && metric_configuration.id != record.id
  5 + record.errors[attribute] << "There's already a #{record.class} with #{attribute} #{value}! Please, choose another one."
  6 + break
  7 + end
  8 + end
  9 + end
  10 +end
0 11 \ No newline at end of file
... ...
spec/factories/metric_configurations.rb
1 1 FactoryGirl.define do
2   - factory :metric_configuration, class: KalibroGem::Entities::MetricConfiguration do
  2 + factory :metric_configuration, class: MetricConfiguration do
3 3 id 1
4 4 code 'code'
5 5 metric {FactoryGirl.build(:metric)}
... ...
spec/models/validators/code_uniqueness_validator_spec.rb 0 → 100644
... ... @@ -0,0 +1,32 @@
  1 +require 'spec_helper'
  2 +
  3 +describe CodeUniquenessValidator do
  4 + describe 'methods' do
  5 + describe 'validate_each' do
  6 + context 'without saved metric_configurations' do
  7 + before :each do
  8 + MetricConfiguration.expects(:metric_configurations_of).returns([])
  9 + MetricConfiguration.expects(:request).returns(42)
  10 + end
  11 +
  12 + subject { FactoryGirl.build(:metric_configuration) }
  13 + it 'should contain no errors' do
  14 + subject.save
  15 + subject.errors.should be_empty
  16 + end
  17 + end
  18 +
  19 + context 'with name already taken by another metric_configuration' do
  20 + before :each do
  21 + @subject = FactoryGirl.build(:metric_configuration)
  22 + MetricConfiguration.expects(:metric_configurations_of).with(@subject.configuration_id).returns([FactoryGirl.build(:metric_configuration, id: @subject.id + 1)])
  23 + end
  24 +
  25 + it 'should contain errors' do
  26 + @subject.save
  27 + @subject.errors[:code].should eq(["There's already a MetricConfiguration with code #{@subject.code}! Please, choose another one."])
  28 + end
  29 + end
  30 + end
  31 + end
  32 +end
0 33 \ No newline at end of file
... ...