On Nov 8, 2010, at 9:46 AM, [email protected] wrote:
> From: Jan Provaznik <[email protected]>
>
> 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."
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
> [email protected]
> https://fedorahosted.org/mailman/listinfo/deltacloud-devel
ACK (with small comment above)
-- Ladislav
_______________________________________________
deltacloud-devel mailing list
[email protected]
https://fedorahosted.org/mailman/listinfo/deltacloud-devel