Ignore this patch - Ladislav had a good tip how to improve this patch so I'll send it
again on monday.
----- Original Message -----
From: jprovazn(a)redhat.com
To: deltacloud-devel(a)lists.fedorahosted.org
Cc: "Jan Provaznik" <jprovazn(a)redhat.com>
Sent: Friday, November 5, 2010 2:17:57 PM GMT +01:00 Amsterdam / Berlin / Bern / Rome /
Stockholm / Vienna
Subject: [PATCH aggregator 2/2] BZ #644282 - deleting templates for which instances exist
From: Jan Provaznik <jprovazn(a)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]
--
1.7.2.3