Fixed #2339 - Integer minimal is ignored.

This commit is contained in:
Billy Zhou 2019-06-27 13:53:17 +02:00 committed by Thorsten Eckel
parent 7e113870a7
commit 29f96527a7
2 changed files with 97 additions and 0 deletions

View file

@ -0,0 +1,30 @@
class ObjectManager::Attribute::Validation::MinMax < ObjectManager::Attribute::Validation::Backend
def validate
return if value.blank?
return if irrelevant_attribute?
validate_min
validate_max
end
private
def irrelevant_attribute?
attribute.data_type != 'integer'.freeze
end
def validate_min
return if !attribute.data_option[:min]
return if value >= attribute.data_option[:min]
invalid_because_attribute("is smaller than the allowed minimum value of #{attribute.data_option[:min]}")
end
def validate_max
return if !attribute.data_option[:max]
return if value <= attribute.data_option[:max]
invalid_because_attribute("is larger than the allowed maximum value of #{attribute.data_option[:max]}")
end
end

View file

@ -0,0 +1,67 @@
require 'rails_helper'
require 'models/object_manager/attribute/validation/backend_examples'
RSpec.describe ::ObjectManager::Attribute::Validation::MinMax do
subject do
described_class.new(
record: record,
attribute: attribute
)
end
let(:record) { build(:user) }
let(:attribute) { build(:object_manager_attribute_integer) }
before do
allow(subject).to receive(:value).and_return(value) # rubocop:disable RSpec/SubjectStub, RSpec/NamedSubject
end
context 'when validation should not be performed' do
context 'for blank value' do
let(:value) { nil }
it_behaves_like 'a validation without errors'
end
context 'for irrelevant attribute data_type' do
let(:value) { 'some_value' }
before { attribute.data_type = 'select' }
it_behaves_like 'a validation without errors'
end
end
context 'when validation should be performed' do
shared_examples 'data_option validator' do |data_option:, data_option_value:, valid:, invalid:|
context "for data_option '#{data_option}'" do
before { attribute.data_option[data_option] = data_option_value }
context 'when value is the same as data_option' do
let(:value) { data_option_value }
it_behaves_like 'a validation without errors'
end
context 'when value is valid' do
let(:value) { valid }
it_behaves_like 'a validation without errors'
end
context 'when value is invalid' do
let(:value) { invalid }
it_behaves_like 'a validation with errors'
end
end
end
it_behaves_like 'data_option validator', data_option: :min, data_option_value: 1, valid: 2, invalid: 0
it_behaves_like 'data_option validator', data_option: :max, data_option_value: 1, valid: 0, invalid: 2
end
end