From 59c787bacb9b8eafb315a8d20609262a502a88ab Mon Sep 17 00:00:00 2001 From: Mantas Masalskis Date: Wed, 19 Aug 2020 17:24:19 +0200 Subject: [PATCH] Maintenance: Reduce HasBackends overhead by autoloading backends on initialisation. --- .../object_manager/attribute/validation/backend.rb | 8 -------- lib/mixin/has_backends.rb | 9 ++++++++- lib/mixin/is_backend.rb | 12 ------------ lib/secure_mailing/backend.rb | 7 ------- .../attribute/validation/backend_examples.rb | 8 ++++++++ .../attribute/validation/backend_spec.rb | 7 ------- .../attribute/validation/future_past_spec.rb | 4 +--- .../attribute/validation/min_max_spec.rb | 4 +--- .../attribute/validation/required_spec.rb | 4 +--- 9 files changed, 19 insertions(+), 44 deletions(-) delete mode 100644 lib/mixin/is_backend.rb diff --git a/app/models/object_manager/attribute/validation/backend.rb b/app/models/object_manager/attribute/validation/backend.rb index 83272540c..99de80513 100644 --- a/app/models/object_manager/attribute/validation/backend.rb +++ b/app/models/object_manager/attribute/validation/backend.rb @@ -1,10 +1,4 @@ class ObjectManager::Attribute::Validation::Backend - include Mixin::IsBackend - - def self.inherited(subclass) - subclass.is_backend_of(::ObjectManager::Attribute::Validation) - end - def self.validate(*args) new(*args).validate end @@ -22,5 +16,3 @@ class ObjectManager::Attribute::Validation::Backend record.errors.add attribute.name.to_sym, message end end - -Mixin::RequiredSubPaths.eager_load_recursive(__dir__) diff --git a/lib/mixin/has_backends.rb b/lib/mixin/has_backends.rb index c425b5270..57beae99f 100644 --- a/lib/mixin/has_backends.rb +++ b/lib/mixin/has_backends.rb @@ -7,7 +7,14 @@ module Mixin Set.new end - require_dependency "#{name}::Backend".underscore + self_path = ActiveSupport::Dependencies.search_for_file name.underscore + backends_path = self_path.delete_suffix File.extname(self_path) + + Mixin::RequiredSubPaths.eager_load_recursive backends_path + + backends = "#{name}::Backend".constantize.descendants + + self.backends = Set.new(backends) end end end diff --git a/lib/mixin/is_backend.rb b/lib/mixin/is_backend.rb deleted file mode 100644 index c0ffb1c88..000000000 --- a/lib/mixin/is_backend.rb +++ /dev/null @@ -1,12 +0,0 @@ -module Mixin - module IsBackend - extend ActiveSupport::Concern - - class_methods do - - def is_backend_of(klass) # rubocop:disable Naming/PredicateName - klass.backends.add(self) - end - end - end -end diff --git a/lib/secure_mailing/backend.rb b/lib/secure_mailing/backend.rb index b840d1dea..f09c96143 100644 --- a/lib/secure_mailing/backend.rb +++ b/lib/secure_mailing/backend.rb @@ -1,9 +1,2 @@ class SecureMailing::Backend - include Mixin::IsBackend - - def self.inherited(subclass) - subclass.is_backend_of(::SecureMailing) - end end - -Mixin::RequiredSubPaths.eager_load_recursive(__dir__) diff --git a/spec/models/object_manager/attribute/validation/backend_examples.rb b/spec/models/object_manager/attribute/validation/backend_examples.rb index d6fa3973e..51bd7dcf0 100644 --- a/spec/models/object_manager/attribute/validation/backend_examples.rb +++ b/spec/models/object_manager/attribute/validation/backend_examples.rb @@ -2,6 +2,7 @@ require 'rails_helper' RSpec.shared_examples 'a validation without errors' do it 'validatates without errors' do + allow(subject).to receive(:value).and_return(value) # rubocop:disable RSpec/SubjectStub subject.validate expect(record.errors).to be_blank end @@ -9,7 +10,14 @@ end RSpec.shared_examples 'a validation with errors' do it 'validates with errors' do + allow(subject).to receive(:value).and_return(value) # rubocop:disable RSpec/SubjectStub subject.validate expect(record.errors).to be_present end end + +RSpec.shared_examples 'validate backend' do + it 'included in backends list' do + expect(::ObjectManager::Attribute::Validation.backends).to include(described_class) + end +end diff --git a/spec/models/object_manager/attribute/validation/backend_spec.rb b/spec/models/object_manager/attribute/validation/backend_spec.rb index eb9d4f9bf..16ac70ebb 100644 --- a/spec/models/object_manager/attribute/validation/backend_spec.rb +++ b/spec/models/object_manager/attribute/validation/backend_spec.rb @@ -2,13 +2,6 @@ require 'rails_helper' RSpec.describe ObjectManager::Attribute::Validation::Backend do - it 'registers inheriting classes as ObjectManager::Attribute::Validation backends' do - backends = spy - expect(ObjectManager::Attribute::Validation).to receive(:backends).and_return(backends) - backend = Class.new(described_class) - expect(backends).to have_received(:add).with(backend) - end - describe 'backend interface' do subject do diff --git a/spec/models/object_manager/attribute/validation/future_past_spec.rb b/spec/models/object_manager/attribute/validation/future_past_spec.rb index 1989afbbe..19b8b0909 100644 --- a/spec/models/object_manager/attribute/validation/future_past_spec.rb +++ b/spec/models/object_manager/attribute/validation/future_past_spec.rb @@ -13,9 +13,7 @@ RSpec.describe ::ObjectManager::Attribute::Validation::FuturePast do let(:record) { build(:user) } let(:attribute) { build(:object_manager_attribute_datetime) } - before do - allow(subject).to receive(:value).and_return(value) - end + it_behaves_like 'validate backend' shared_examples 'data_option validator' do |data_option:, value:| context "for data_option '#{data_option}'" do diff --git a/spec/models/object_manager/attribute/validation/min_max_spec.rb b/spec/models/object_manager/attribute/validation/min_max_spec.rb index e4ee818b5..4d1e57cdb 100644 --- a/spec/models/object_manager/attribute/validation/min_max_spec.rb +++ b/spec/models/object_manager/attribute/validation/min_max_spec.rb @@ -13,9 +13,7 @@ RSpec.describe ::ObjectManager::Attribute::Validation::MinMax do 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 + it_behaves_like 'validate backend' context 'when validation should not be performed' do diff --git a/spec/models/object_manager/attribute/validation/required_spec.rb b/spec/models/object_manager/attribute/validation/required_spec.rb index d881e1db0..8ac0bb714 100644 --- a/spec/models/object_manager/attribute/validation/required_spec.rb +++ b/spec/models/object_manager/attribute/validation/required_spec.rb @@ -13,9 +13,7 @@ RSpec.describe ::ObjectManager::Attribute::Validation::Required do let(:record) { build(:user) } let(:attribute) { build(:object_manager_attribute_date) } - before do - allow(subject).to receive(:value).and_return(value) - end + it_behaves_like 'validate backend' context 'when validation should be performed' do