Maintenance: Reduce HasBackends overhead by autoloading backends on initialisation.
This commit is contained in:
parent
9df575060a
commit
59c787bacb
9 changed files with 19 additions and 44 deletions
|
@ -1,10 +1,4 @@
|
||||||
class ObjectManager::Attribute::Validation::Backend
|
class ObjectManager::Attribute::Validation::Backend
|
||||||
include Mixin::IsBackend
|
|
||||||
|
|
||||||
def self.inherited(subclass)
|
|
||||||
subclass.is_backend_of(::ObjectManager::Attribute::Validation)
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.validate(*args)
|
def self.validate(*args)
|
||||||
new(*args).validate
|
new(*args).validate
|
||||||
end
|
end
|
||||||
|
@ -22,5 +16,3 @@ class ObjectManager::Attribute::Validation::Backend
|
||||||
record.errors.add attribute.name.to_sym, message
|
record.errors.add attribute.name.to_sym, message
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
Mixin::RequiredSubPaths.eager_load_recursive(__dir__)
|
|
||||||
|
|
|
@ -7,7 +7,14 @@ module Mixin
|
||||||
Set.new
|
Set.new
|
||||||
end
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -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
|
|
|
@ -1,9 +1,2 @@
|
||||||
class SecureMailing::Backend
|
class SecureMailing::Backend
|
||||||
include Mixin::IsBackend
|
|
||||||
|
|
||||||
def self.inherited(subclass)
|
|
||||||
subclass.is_backend_of(::SecureMailing)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
Mixin::RequiredSubPaths.eager_load_recursive(__dir__)
|
|
||||||
|
|
|
@ -2,6 +2,7 @@ require 'rails_helper'
|
||||||
|
|
||||||
RSpec.shared_examples 'a validation without errors' do
|
RSpec.shared_examples 'a validation without errors' do
|
||||||
it 'validatates without errors' do
|
it 'validatates without errors' do
|
||||||
|
allow(subject).to receive(:value).and_return(value) # rubocop:disable RSpec/SubjectStub
|
||||||
subject.validate
|
subject.validate
|
||||||
expect(record.errors).to be_blank
|
expect(record.errors).to be_blank
|
||||||
end
|
end
|
||||||
|
@ -9,7 +10,14 @@ end
|
||||||
|
|
||||||
RSpec.shared_examples 'a validation with errors' do
|
RSpec.shared_examples 'a validation with errors' do
|
||||||
it 'validates with errors' do
|
it 'validates with errors' do
|
||||||
|
allow(subject).to receive(:value).and_return(value) # rubocop:disable RSpec/SubjectStub
|
||||||
subject.validate
|
subject.validate
|
||||||
expect(record.errors).to be_present
|
expect(record.errors).to be_present
|
||||||
end
|
end
|
||||||
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
|
||||||
|
|
|
@ -2,13 +2,6 @@ require 'rails_helper'
|
||||||
|
|
||||||
RSpec.describe ObjectManager::Attribute::Validation::Backend do
|
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
|
describe 'backend interface' do
|
||||||
|
|
||||||
subject do
|
subject do
|
||||||
|
|
|
@ -13,9 +13,7 @@ RSpec.describe ::ObjectManager::Attribute::Validation::FuturePast do
|
||||||
let(:record) { build(:user) }
|
let(:record) { build(:user) }
|
||||||
let(:attribute) { build(:object_manager_attribute_datetime) }
|
let(:attribute) { build(:object_manager_attribute_datetime) }
|
||||||
|
|
||||||
before do
|
it_behaves_like 'validate backend'
|
||||||
allow(subject).to receive(:value).and_return(value)
|
|
||||||
end
|
|
||||||
|
|
||||||
shared_examples 'data_option validator' do |data_option:, value:|
|
shared_examples 'data_option validator' do |data_option:, value:|
|
||||||
context "for data_option '#{data_option}'" do
|
context "for data_option '#{data_option}'" do
|
||||||
|
|
|
@ -13,9 +13,7 @@ RSpec.describe ::ObjectManager::Attribute::Validation::MinMax do
|
||||||
let(:record) { build(:user) }
|
let(:record) { build(:user) }
|
||||||
let(:attribute) { build(:object_manager_attribute_integer) }
|
let(:attribute) { build(:object_manager_attribute_integer) }
|
||||||
|
|
||||||
before do
|
it_behaves_like 'validate backend'
|
||||||
allow(subject).to receive(:value).and_return(value) # rubocop:disable RSpec/SubjectStub, RSpec/NamedSubject
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when validation should not be performed' do
|
context 'when validation should not be performed' do
|
||||||
|
|
||||||
|
|
|
@ -13,9 +13,7 @@ RSpec.describe ::ObjectManager::Attribute::Validation::Required do
|
||||||
let(:record) { build(:user) }
|
let(:record) { build(:user) }
|
||||||
let(:attribute) { build(:object_manager_attribute_date) }
|
let(:attribute) { build(:object_manager_attribute_date) }
|
||||||
|
|
||||||
before do
|
it_behaves_like 'validate backend'
|
||||||
allow(subject).to receive(:value).and_return(value)
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when validation should be performed' do
|
context 'when validation should be performed' do
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue