Skip to content

Commit

Permalink
Remove subclassing hack by not subclassing EMS
Browse files Browse the repository at this point in the history
  • Loading branch information
Fryguy committed Sep 27, 2024
1 parent 9637774 commit d44b16a
Showing 1 changed file with 16 additions and 24 deletions.
40 changes: 16 additions & 24 deletions spec/models/mixins/supports_feature_mixin_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,14 @@ def initialize(values = {})
end
end

let(:model) do
define_model("Model", ActiveRecord::Base,
:publish => true,
:archive => true,
:delete => false,
:fake => 'We keep it real!')
end

describe ".subclasses_supporting" do
it 'detect' do
define_subclass("ProviderA", model, :fake => true)
Expand All @@ -285,14 +293,6 @@ def initialize(values = {})
end
end

let(:model) do
define_model("Model", ActiveRecord::Base,
:publish => true,
:archive => true,
:delete => false,
:fake => 'We keep it real!')
end

describe ".supporting" do
it 'detect' do
ca = define_subclass("ProviderA", model, :fake => true)
Expand All @@ -311,17 +311,19 @@ def initialize(values = {})

describe ".providers_supporting" do
it 'detect' do
providera = define_subclass("ProviderA", ExtManagementSystem)
providerb = define_subclass("ProviderB", ExtManagementSystem)
providerc = define_subclass("ProviderC", ExtManagementSystem)
stub_const("ExtManagementSystem", model)

providera = define_subclass("ProviderA", model)
providerb = define_subclass("ProviderB", model)
providerc = define_subclass("ProviderC", model)

define_subclass(providera.name, model, :fake => true)
define_subclass(providerb.name, model, :publish => false, :delete => true, :fake => true)
define_subclass(providerc.name, model, :publish => false, :delete => true, :fake => true)

FactoryBot.create(:ext_management_system, :type => providera.name, :name => "a1")
FactoryBot.create(:ext_management_system, :type => providera.name, :name => "a2")
FactoryBot.create(:ext_management_system, :type => providerb.name, :name => "b1")
providera.create(:name => "a1")
providera.create(:name => "a2")
providerb.create(:name => "b1")

expect(model.providers_supporting(:publish).map(&:name)).to match_array(%w[a1 a2])
expect(model.providers_supporting(:delete).map(&:name)).to eq(%w[b1])
Expand All @@ -347,16 +349,6 @@ def define_supporting_class(class_name, parent, supports_values = {})
# This also helps when the active record class does not have a name.
self.table_name = "vms" if parent.ancestors.include?(ActiveRecord::Base)

# We sometimes create a temporary Ems child class (e.g.: Vm)
# When the spec finishes and the class is disposed, Ems.subclasses still returns the old class.
# It will go away after a few GC cycles, but before then, it causes BlacklistedEvents spec failures.
# Adding this method to have those specs work despite the bogus class returned from subclasses.
if parent == ExtManagementSystem
def self.default_blacklisted_event_names
[]
end
end

yield(self) if block_given?
supports_values.each do |feature, value|
case value
Expand Down

0 comments on commit d44b16a

Please sign in to comment.