From: Jan Provaznik jprovazn@redhat.com
--- src/spec/models/template_spec.rb | 8 ++++++++ 1 files changed, 8 insertions(+), 0 deletions(-)
diff --git a/src/spec/models/template_spec.rb b/src/spec/models/template_spec.rb index ef30fc1..cac172c 100644 --- a/src/spec/models/template_spec.rb +++ b/src/spec/models/template_spec.rb @@ -16,4 +16,12 @@ describe Template do rimg.save tpl.providers.size.should eql(1) end + + it "should not destroy template if there are instances created from this template" do + inst = Factory.build(:instance) + inst.save! + lambda do + inst.template.destroy + end.should_not change(Template, :count) + end end
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 | 16 ++++++++++++---- 2 files changed, 26 insertions(+), 5 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..d1c17c4 100644 --- a/src/app/models/template.rb +++ b/src/app/models/template.rb @@ -2,22 +2,30 @@ require 'util/image_descriptor_xml' require 'typhoeus'
class Template < ActiveRecord::Base - has_many :images, :dependent => :destroy + has_many :images, :dependent => :destroy + has_many :instances 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? + unless instances.empty? + errors.add_to_base "There are instances for this template." + return false + end + + true + end + def update_xml_attributes(opts = {}) xml.name = opts[:name] if opts[:name] xml.description = opts[:summary] if opts[:summary]
On Nov 8, 2010, at 9:46 AM, jprovazn@redhat.com wrote:
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 | 16 ++++++++++++---- 2 files changed, 26 insertions(+), 5 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 redirect_to :action => 'index' endend
diff --git a/src/app/models/template.rb b/src/app/models/template.rb index 9264e76..d1c17c4 100644 --- a/src/app/models/template.rb +++ b/src/app/models/template.rb @@ -2,22 +2,30 @@ require 'util/image_descriptor_xml' require 'typhoeus'
class Template < ActiveRecord::Base
- has_many :images, :dependent => :destroy
has_many :images, :dependent => :destroy
has_many :instances 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?
- unless instances.empty?
errors.add_to_base "There are instances for this template."
As of Rails 3.0 method add_to_base is marked as deprecated. Better would be to use: errors.add(:base, 'xxx').
return false
- end
- true
- end
- def update_xml_attributes(opts = {}) xml.name = opts[:name] if opts[:name] xml.description = opts[:summary] if opts[:summary]
-- 1.7.2.3
deltacloud-devel mailing list deltacloud-devel@lists.fedorahosted.org https://fedorahosted.org/mailman/listinfo/deltacloud-devel
ACK (with small comment above)
-- Ladislav
deltacloud-devel@lists.fedorahosted.org