Commit 822a81f5ede65bb0462b802c8906772629146947

Authored by Rafael Manzo
1 parent bddb1960

MezuroRanges accepts -INF and +INF as values for baginning and end

app/models/mezuro_range.rb
... ... @@ -2,11 +2,22 @@ require "validators/beginning_uniqueness_validator.rb"
2 2  
3 3 class MezuroRange < KalibroGatekeeperClient::Entities::Range
4 4 include KalibroRecord
5   -
  5 +
6 6 attr_accessor :beginning, :end, :reading_id, :mezuro_configuration_id, :comments
7 7  
8   - validates :beginning, presence: true, beginning_uniqueness: true, numericality: true
9   - validates :end, presence: true , numericality: true
  8 + validates :beginning, presence: true, beginning_uniqueness: true
  9 + validates :beginning, numericality: true, if: :non_infinite_beginning?
  10 + validates :end, presence: true
  11 + validates :end, numericality: true, if: :non_infinite_end?
10 12 validates :reading_id, presence: true
11 13  
  14 + private
  15 +
  16 + def non_infinite_end?
  17 + !(self.end == '-INF' || self.end == '+INF')
  18 + end
  19 +
  20 + def non_infinite_beginning?
  21 + val = !(self.beginning == '-INF' || self.beginning == '+INF')
  22 + end
12 23 end
... ...
spec/models/mezuro_range_spec.rb
... ... @@ -7,10 +7,29 @@ describe MezuroRange do
7 7 before :each do
8 8 MezuroRange.expects(:ranges_of).with(subject.metric_configuration_id).at_least_once.returns([])
9 9 end
10   -
  10 +
11 11 it { should validate_presence_of(:beginning) }
12 12 it { should validate_presence_of(:end) }
13 13 it { should validate_presence_of(:reading_id) }
  14 +
  15 + context 'beginning and end numericality' do
  16 + it { should validate_presence_of(:beginning) }
  17 + it { should validate_presence_of(:end) }
  18 +
  19 + it 'should allow -INF and +INF to beginning' do
  20 + subject.beginning = '-INF'
  21 + subject.save
  22 +
  23 + subject.errors.messages.should be_empty
  24 + end
  25 +
  26 + it 'should allow -INF and +INF to end' do
  27 + subject.end = '-INF'
  28 + subject.save
  29 +
  30 + subject.errors.messages.should be_empty
  31 + end
  32 + end
14 33 end
15 34 end
16 35  
... ...