app/models/katello/activation_key.rb in katello-4.6.2.1 vs app/models/katello/activation_key.rb in katello-4.7.0.rc1
- old
+ new
@@ -26,11 +26,11 @@
has_many :purpose_addons, :class_name => "Katello::PurposeAddon", :through => :activation_key_purpose_addons
alias_method :lifecycle_environment, :environment
before_validation :set_default_content_view, :unless => :persisted?
-
+ before_destroy :validate_destroyable!
accepts_nested_attributes_for :purpose_addons
validates_lengths_from_database
validates_with Validators::KatelloNameFormatValidator, :attributes => :name
validates :name, :presence => true
@@ -173,9 +173,20 @@
def self.find_by_purpose_addons(_key, operator, value)
conditions = sanitize_sql_for_conditions(["#{Katello::PurposeAddon.table_name}.name #{operator} ?", value_to_sql(operator, value)])
activation_keys = ::Katello::ActivationKey.joins(:purpose_addons).where(conditions)
return_activation_keys_by_id(activation_keys.pluck(:id))
+ end
+
+ def validate_destroyable!
+ unless organization.being_deleted?
+ if Parameter.where(name: 'kt_activation_keys').pluck(:value).any? { |value| value.split(",").include?(name) }
+ fail _("This activation key is associated to one or more Hosts/Hostgroups. "\
+ "Search and unassociate Hosts/Hostgroups using params.kt_activation_keys ~ \"%{name}\" "\
+ "before deleting." % {name: name})
+ end
+ end
+ true
end
private
def set_default_content_view