Commit 0313bc9c6cd273657e48e167be02c8f714d22555
Committed by
Rafael Manzo
1 parent
6534f0ea
Exists in
colab
and in
4 other branches
Removed the validators
Showing
6 changed files
with
0 additions
and
147 deletions
Show diff stats
app/models/validators/beginning_uniqueness_validator.rb
| ... | ... | @@ -1,10 +0,0 @@ |
| 1 | -class BeginningUniquenessValidator < ActiveModel::EachValidator | |
| 2 | - def validate_each(record, attribute, value) | |
| 3 | - record.class.ranges_of(record.metric_configuration_id).each do |mezuro_range| | |
| 4 | - if mezuro_range.beginning == value && mezuro_range.id != record.id | |
| 5 | - record.errors[attribute] << "There is already a #{record.class} with #{attribute} #{value}! Please, choose another one." | |
| 6 | - break | |
| 7 | - end | |
| 8 | - end | |
| 9 | - end | |
| 10 | -end | |
| 11 | 0 | \ No newline at end of file |
app/models/validators/greater_than_beginning_validator.rb
| ... | ... | @@ -1,18 +0,0 @@ |
| 1 | -class GreaterThanBeginningValidator < ActiveModel::EachValidator | |
| 2 | - def validate_each(record, attribute, value) | |
| 3 | - if record.beginning.is_a?(String) || value.is_a?(String) #TODO This will be useless when we start representing INF as ruby Infinity with the new Kalibro configuration application. | |
| 4 | - if record.beginning=="INF" || value=="-INF" || record.beginning == value | |
| 5 | - add_error(record,attribute) | |
| 6 | - end | |
| 7 | - elsif record.beginning >= value | |
| 8 | - add_error(record,attribute) | |
| 9 | - end | |
| 10 | - end | |
| 11 | - | |
| 12 | - private | |
| 13 | - | |
| 14 | - def add_error(record, attribute) | |
| 15 | - record.errors[attribute] << "The End value should be greater than the Beginning value." | |
| 16 | - end | |
| 17 | - | |
| 18 | -end |
app/models/validators/kalibro_uniqueness_validator.rb
| ... | ... | @@ -1,10 +0,0 @@ |
| 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) and (entity.id != record.id) | |
| 5 | - record.errors[attribute] << "There is already a #{record.class} with #{attribute} #{value}! Please, choose another one." | |
| 6 | - break | |
| 7 | - end | |
| 8 | - end | |
| 9 | - end | |
| 10 | -end |
app/models/validators/range_overlapping_validator.rb
| ... | ... | @@ -1,27 +0,0 @@ |
| 1 | -class RangeOverlappingValidator < ActiveModel::Validator | |
| 2 | - def validate(record) | |
| 3 | - record.class.ranges_of(record.metric_configuration_id).each do |mezuro_range| | |
| 4 | - if mezuro_range.id != record.id && overlaps?(mezuro_range,record) | |
| 5 | - record.errors[:beginning] << "There is already a #{record.class} within these boundaries! Please, choose another interval." | |
| 6 | - break | |
| 7 | - end | |
| 8 | - end | |
| 9 | - end | |
| 10 | - | |
| 11 | - private | |
| 12 | - | |
| 13 | - def overlaps?(range1, range2) | |
| 14 | - return true if to_float(range1.beginning) >= to_float(range2.beginning) && to_float(range1.beginning) < to_float(range2.end) | |
| 15 | - return true if to_float(range1.end) > to_float(range2.beginning) && to_float(range1.end) <= to_float(range2.end) | |
| 16 | - return true if to_float(range1.beginning) >= to_float(range2.beginning) && to_float(range1.end) <= to_float(range2.end) | |
| 17 | - return true if to_float(range1.beginning) <= to_float(range2.beginning) && to_float(range1.end) >= to_float(range2.end) | |
| 18 | - return false | |
| 19 | - end | |
| 20 | - | |
| 21 | - def to_float(value) | |
| 22 | - return 1.0/0 if value=="INF" | |
| 23 | - return -1.0/0 if value=="-INF" | |
| 24 | - return value.to_f if value.is_a?(String) | |
| 25 | - return value | |
| 26 | - end | |
| 27 | -end |
spec/models/validators/beginning_uniqueness_validator_spec.rb
| ... | ... | @@ -1,39 +0,0 @@ |
| 1 | -require 'rails_helper' | |
| 2 | -require 'validators/beginning_uniqueness_validator' | |
| 3 | - | |
| 4 | -describe BeginningUniquenessValidator, :type => :model do | |
| 5 | - pending 'waiting for kalibro configurations integration' do | |
| 6 | - describe 'methods' do | |
| 7 | - describe 'validate_each' do | |
| 8 | - let(:metric_configuration) { FactoryGirl.build(:metric_configuration) } | |
| 9 | - context 'without saved mezuro_range' do | |
| 10 | - before :each do | |
| 11 | - MezuroRange.expects(:ranges_of).with(metric_configuration.id).returns([]) | |
| 12 | - MezuroRange.expects(:request).returns(42) | |
| 13 | - RangeOverlappingValidator.any_instance.stubs(:validate) | |
| 14 | - end | |
| 15 | - | |
| 16 | - subject { FactoryGirl.build(:mezuro_range, metric_configuration_id: metric_configuration.id) } | |
| 17 | - it 'should contain no errors' do | |
| 18 | - subject.save | |
| 19 | - expect(subject.errors).to be_empty | |
| 20 | - end | |
| 21 | - end | |
| 22 | - | |
| 23 | - context 'with beginning already taken by another mezuro_range' do | |
| 24 | - let(:another_mezuro_range) { FactoryGirl.build(:another_mezuro_range, id: 3, metric_configuration_id: metric_configuration.id) } | |
| 25 | - before :each do | |
| 26 | - @subject = FactoryGirl.build(:mezuro_range, metric_configuration_id: metric_configuration.id) | |
| 27 | - MezuroRange.expects(:ranges_of).with(@subject.metric_configuration_id).returns([another_mezuro_range]) | |
| 28 | - RangeOverlappingValidator.any_instance.stubs(:validate) | |
| 29 | - end | |
| 30 | - | |
| 31 | - it 'should contain errors' do | |
| 32 | - @subject.save | |
| 33 | - expect(@subject.errors[:beginning]).to eq(["There is already a MezuroRange with beginning #{@subject.beginning}! Please, choose another one."]) | |
| 34 | - end | |
| 35 | - end | |
| 36 | - end | |
| 37 | - end | |
| 38 | - end | |
| 39 | -end |
spec/models/validators/greater_than_beginning_validator_spec.rb
| ... | ... | @@ -1,43 +0,0 @@ |
| 1 | -require 'rails_helper' | |
| 2 | -require 'validators/greater_than_beginning_validator' | |
| 3 | - | |
| 4 | -describe GreaterThanBeginningValidator, :type => :model do | |
| 5 | - pending 'waiting for kalibro configurations integration' do | |
| 6 | - describe 'methods' do | |
| 7 | - describe 'validate_each' do | |
| 8 | - before :each do | |
| 9 | - BeginningUniquenessValidator.any_instance.stubs(:validate_each) | |
| 10 | - RangeOverlappingValidator.any_instance.stubs(:validate) | |
| 11 | - end | |
| 12 | - context 'when beginning is INF or end is -INF' do | |
| 13 | - subject { FactoryGirl.build(:mezuro_range, end: "-INF") } | |
| 14 | - it 'is expected to return an error' do | |
| 15 | - subject.save | |
| 16 | - expect(subject.errors[:end]).to eq(["The End value should be greater than the Beginning value."]) | |
| 17 | - end | |
| 18 | - end | |
| 19 | - context 'when beginning is -INF or end is INF' do | |
| 20 | - subject { FactoryGirl.build(:mezuro_range, end: "INF") } | |
| 21 | - it 'is expected to not return an error' do | |
| 22 | - subject.save | |
| 23 | - expect(subject.errors[:end]).to be_empty | |
| 24 | - end | |
| 25 | - end | |
| 26 | - context 'when beginning is greater than end' do | |
| 27 | - subject { FactoryGirl.build(:mezuro_range, beginning: 1.0, end: 0.0) } | |
| 28 | - it 'is expected to return an error' do | |
| 29 | - subject.save | |
| 30 | - expect(subject.errors[:end]).to eq(["The End value should be greater than the Beginning value."]) | |
| 31 | - end | |
| 32 | - end | |
| 33 | - context 'when beginning is smaller than end' do | |
| 34 | - subject { FactoryGirl.build(:mezuro_range) } | |
| 35 | - it 'is expected to not return an error' do | |
| 36 | - subject.save | |
| 37 | - expect(subject.errors[:end]).to be_empty | |
| 38 | - end | |
| 39 | - end | |
| 40 | - end | |
| 41 | - end | |
| 42 | - end | |
| 43 | -end |