Skip to content

Commit

Permalink
Merge pull request #21094 from agrare/set_miq_templates_model_class
Browse files Browse the repository at this point in the history
Automatically set the miq_templates model_class

(cherry picked from commit e0087eb)
  • Loading branch information
Fryguy authored and agrare committed Aug 31, 2021
1 parent 061e83f commit 701d14d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 23 deletions.
45 changes: 22 additions & 23 deletions app/models/manageiq/providers/inventory/persister/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ def self.prepare_data(name, persister_class, options = {})
builder
end

attr_accessor :name, :persister_class, :properties, :inventory_object_attributes,
:default_values, :dependency_attributes, :options, :adv_settings, :shared_properties

# @see prepare_data()
def initialize(name, persister_class, options = self.class.default_options)
@name = name
Expand All @@ -61,6 +64,17 @@ def initialize(name, persister_class, options = self.class.default_options)
@shared_properties = options[:shared_properties] # From persister
end

def manager_class
@manager_class ||= begin
provider_module = persister_class.provider_module
manager_module = self.class.name.split('::').last

"#{provider_module}::#{manager_module}".safe_constantize
rescue ::ManageIQ::Providers::Inflector::ObjectNotNamespacedError
nil
end
end

# Builds data for InventoryCollection
# Calls method @name (if exists) with specific properties
# Yields for overwriting provider-specific properties
Expand Down Expand Up @@ -206,29 +220,14 @@ def merge_hashes(source, dest, mode)
#
# @return [Class | nil] when class doesn't exist, returns nil
def auto_model_class
model_class = begin
# a) Provider specific class
provider_module = @persister_class.provider_module
manager_module = self.class.name.split('::').last

class_name = "#{provider_module}::#{manager_module}::#{@name.to_s.classify}"

inferred_class = class_name.safe_constantize

# safe_constantize can return different similar class ( some Rails auto-magic :/ )
if inferred_class.to_s == class_name
inferred_class
end
rescue ::ManageIQ::Providers::Inflector::ObjectNotNamespacedError
nil
end

if model_class
model_class
else
# b) general class
"::#{@name.to_s.classify}".safe_constantize
end
class_name = "#{manager_class}::#{name.to_s.classify}"
provider_class = class_name.safe_constantize

# Check that safe_constantize returns our expected class_name, if not then
# return the base class.
#
# safe_constantize can return different similar class ( some Rails auto-magic :/ )
provider_class.to_s == class_name ? provider_class : "::#{name.to_s.classify}".safe_constantize
end

# Enables/disables auto_model_class and exception check
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ def miq_templates
add_default_values(
:template => true
)

template_class = "#{manager_class}::Template".safe_constantize
add_properties(:model_class => template_class) if template_class
end

def vm_template_shared
Expand Down

0 comments on commit 701d14d

Please sign in to comment.