diff --git a/app/models/orchestration_template.rb b/app/models/orchestration_template.rb index 696a84bfe0ce..a376536d2d26 100644 --- a/app/models/orchestration_template.rb +++ b/app/models/orchestration_template.rb @@ -42,7 +42,8 @@ def self.find_or_create_by_contents(hashes) create!(hash.except(:md5)) # always create a new template if it is a draft true else - md5s << calc_md5(with_universal_newline(hash[:content])) + klass = hash[:type].present? ? hash[:type].constantize : self + md5s << klass.calc_md5(with_universal_newline(hash[:content])) false end end diff --git a/spec/models/orchestration_template_spec.rb b/spec/models/orchestration_template_spec.rb index b153846e7cd2..4393a638e97f 100644 --- a/spec/models/orchestration_template_spec.rb +++ b/spec/models/orchestration_template_spec.rb @@ -36,6 +36,24 @@ expect(@existing_record).not_to eq(OrchestrationTemplate.find_or_create_by_contents(@query_hash)[0]) expect(OrchestrationTemplate.count).to eq(2) end + + it "uses subclass if type is present" do + expect(ManageIQ::Providers::Vmware::CloudManager::OrchestrationTemplate).to receive(:calc_md5).at_least(:once) + expect(described_class).not_to receive(:calc_md5) + + @query_hash[:draft] = false + @query_hash[:type] = ManageIQ::Providers::Vmware::CloudManager::OrchestrationTemplate.name + OrchestrationTemplate.find_or_create_by_contents(@query_hash) + end + + it "uses parent if type is not present" do + expect(ManageIQ::Providers::Vmware::CloudManager::OrchestrationTemplate).not_to receive(:calc_md5) + expect(described_class).to receive(:calc_md5).at_least(:once) + + @query_hash[:draft] = false + @query_hash[:type] = nil + OrchestrationTemplate.find_or_create_by_contents(@query_hash) + end end end