From 29f96527a7ed21e9505a0644b179e962a91e0371 Mon Sep 17 00:00:00 2001 From: Billy Zhou Date: Thu, 27 Jun 2019 13:53:17 +0200 Subject: [PATCH] Fixed #2339 - Integer minimal is ignored. --- .../attribute/validation/min_max.rb | 30 +++++++++ .../attribute/validation/min_max_spec.rb | 67 +++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 app/models/object_manager/attribute/validation/min_max.rb create mode 100644 spec/models/object_manager/attribute/validation/min_max_spec.rb diff --git a/app/models/object_manager/attribute/validation/min_max.rb b/app/models/object_manager/attribute/validation/min_max.rb new file mode 100644 index 000000000..7bb0d8c70 --- /dev/null +++ b/app/models/object_manager/attribute/validation/min_max.rb @@ -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 diff --git a/spec/models/object_manager/attribute/validation/min_max_spec.rb b/spec/models/object_manager/attribute/validation/min_max_spec.rb new file mode 100644 index 000000000..e4ee818b5 --- /dev/null +++ b/spec/models/object_manager/attribute/validation/min_max_spec.rb @@ -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