Skip to content

Commit

Permalink
Merge pull request #20756 from Fryguy/acts_as_sti_leaf_class
Browse files Browse the repository at this point in the history
Add ability to declare an intermediate class as acting like a leaf class
  • Loading branch information
chessbyte authored Jan 19, 2021
2 parents e47c678 + 10fef8d commit 9259b2d
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 10 deletions.
10 changes: 7 additions & 3 deletions app/models/ext_management_system.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,20 @@ def self.with_tenant(tenant_id)
where(:tenant_id => tenant.ancestor_ids + [tenant_id])
end

def self.concrete_subclasses
leaf_subclasses | descendants.select { |d| d.try(:acts_as_sti_leaf_class?) }
end

def self.types
leaf_subclasses.collect(&:ems_type)
concrete_subclasses.collect(&:ems_type)
end

def self.supported_types
supported_subclasses.collect(&:ems_type)
end

def self.supported_subclasses
leaf_subclasses.select(&:permitted?)
concrete_subclasses.select(&:permitted?)
end

def self.permitted?
Expand Down Expand Up @@ -405,7 +409,7 @@ def self.model_name_from_emstype(emstype)

def self.model_from_emstype(emstype)
emstype = emstype.downcase
ExtManagementSystem.leaf_subclasses.detect { |k| k.ems_type == emstype }
ExtManagementSystem.concrete_subclasses.detect { |k| k.ems_type == emstype }
end

def self.short_token
Expand Down
2 changes: 1 addition & 1 deletion app/models/miq_schedule_worker/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ def do_work

# @returns Hash<class, Integer> Hash of ems_class => refresh_interval
def schedule_settings_for_ems_refresh
ExtManagementSystem.leaf_subclasses.each.with_object({}) do |klass, hash|
ExtManagementSystem.supported_subclasses.each.with_object({}) do |klass, hash|
next unless klass.ems_type
every = ::Settings.ems_refresh[klass.ems_type].try(:refresh_interval) || ::Settings.ems_refresh.refresh_interval
every = every.respond_to?(:to_i_with_method) ? every.to_i_with_method : every.to_i
Expand Down
8 changes: 3 additions & 5 deletions app/models/provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,12 @@ class Provider < ApplicationRecord

supports :refresh_ems

def self.leaf_subclasses
descendants.select { |d| d.subclasses.empty? }
def self.concrete_subclasses
leaf_subclasses | descendants.select { |d| d.try(:acts_as_sti_leaf_class?) }
end

def self.supported_subclasses
subclasses.flat_map do |s|
s.subclasses.empty? ? s : s.supported_subclasses
end
concrete_subclasses
end

def self.short_token
Expand Down
14 changes: 14 additions & 0 deletions lib/acts_as_sti_leaf_class.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module ActsAsStiLeafClass
extend ActiveSupport::Concern

module ClassMethods
def acts_as_sti_leaf_class?
true
end

private def type_condition(table = arel_table)
sti_column = table[inheritance_column]
predicate_builder.build(sti_column, [sti_name])
end
end
end
2 changes: 1 addition & 1 deletion spec/models/ext_management_system_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
end

it ".model_name_from_emstype" do
described_class.leaf_subclasses.each do |klass|
described_class.concrete_subclasses.each do |klass|
expect(described_class.model_name_from_emstype(klass.ems_type)).to eq(klass.name)
end
expect(described_class.model_name_from_emstype('foo')).to be_nil
Expand Down

0 comments on commit 9259b2d

Please sign in to comment.