Maintenance: Reduce HasBackends overhead by autoloading backends on initialisation.

This commit is contained in:
Mantas Masalskis 2020-08-19 17:24:19 +02:00 committed by Thorsten Eckel
parent 9df575060a
commit 59c787bacb
9 changed files with 19 additions and 44 deletions

View file

@ -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__)

View file

@ -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

View file

@ -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

View file

@ -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__)

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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