From: Jan Provaznik jprovazn@redhat.com
https://bugzilla.redhat.com/show_bug.cgi?id=644282
This patch disables deleting template if there is an instance. --- src/app/controllers/templates_controller.rb | 15 ++++++++++++++- src/app/models/template.rb | 13 ++++++++++--- 2 files changed, 24 insertions(+), 4 deletions(-)
diff --git a/src/app/controllers/templates_controller.rb b/src/app/controllers/templates_controller.rb index 3373c05..8261eab 100644 --- a/src/app/controllers/templates_controller.rb +++ b/src/app/controllers/templates_controller.rb @@ -233,7 +233,20 @@ add account on <a href="#{url_for :controller => 'provider', \ if ids.empty? flash[:notice] = "No Template Selected" else - Template.destroy(ids) + errs = {} + Template.find(ids).each do |t| + t.destroy + unless t.destroyed? + errs[t.name] = t.errors.full_messages.join(". ") + end + end + if errs.empty? + flash[:notice] = 'Template deleted' + else + flash[:error] ||= {} + flash[:error][:summary] = 'Error while deleting template' + (flash[:error][:failures] ||= {}).merge!(errs) + end end redirect_to :action => 'index' end diff --git a/src/app/models/template.rb b/src/app/models/template.rb index 9264e76..4bbf729 100644 --- a/src/app/models/template.rb +++ b/src/app/models/template.rb @@ -4,20 +4,27 @@ require 'typhoeus' class Template < ActiveRecord::Base has_many :images, :dependent => :destroy before_validation :update_attrs + before_destroy :no_instances?
WAREHOUSE_CONFIG = YAML.load_file("#{RAILS_ROOT}/config/image_warehouse.yml")
validates_presence_of :uuid validates_uniqueness_of :uuid - # uncomment this after reworking view (currently is used wizard, - # so there can be situation when save is called and name and platform can be - # unset) validates_presence_of :name validates_uniqueness_of :name validates_presence_of :platform validates_presence_of :platform_version validates_presence_of :architecture
+ def no_instances? + if Instance.find_all_by_template_id(self.id).empty? + return true + else + errors.add_to_base "There are instances for this template." + return false + end + end + def update_xml_attributes(opts = {}) xml.name = opts[:name] if opts[:name] xml.description = opts[:summary] if opts[:summary]