app/models/katello/activation_key.rb in katello-2.2.2 vs app/models/katello/activation_key.rb in katello-2.4.0.rc1
- old
+ new
@@ -1,24 +1,11 @@
-#
-# Copyright 2014 Red Hat, Inc.
-#
-# This software is licensed to you under the GNU General Public
-# License as published by the Free Software Foundation; either version
-# 2 of the License (GPLv2) or (at your option) any later version.
-# There is NO WARRANTY for this software, express or implied,
-# including the implied warranties of MERCHANTABILITY,
-# NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should
-# have received a copy of GPLv2 along with this software; if not, see
-# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
-
module Katello
class ActivationKey < Katello::Model
self.include_root_in_json = false
- include Glue::Candlepin::ActivationKey if Katello.config.use_cp
- include Glue::ElasticSearch::ActivationKey if Katello.config.use_elasticsearch
- include Glue if Katello.config.use_cp
+ include Glue::Candlepin::ActivationKey if SETTINGS[:katello][:use_cp]
+ include Glue if SETTINGS[:katello][:use_cp]
include Katello::Authorization::ActivationKey
include ForemanTasks::Concerns::ActionSubject
belongs_to :organization, :inverse_of => :activation_keys
belongs_to :environment, :class_name => "KTEnvironment", :inverse_of => :activation_keys
@@ -29,10 +16,13 @@
has_many :host_collections, :through => :key_host_collections
has_many :system_activation_keys, :class_name => "Katello::SystemActivationKey", :dependent => :destroy
has_many :systems, :through => :system_activation_keys
+ has_many :pools, :through => :pool_activation_keys, :class_name => "Katello::Pool"
+ has_many :pool_activation_keys, :class_name => "Katello::PoolActivationKey", :dependent => :destroy, :inverse_of => :activation_keys
+
before_validation :set_default_content_view, :unless => :persisted?
validates_lengths_from_database
validates_with Validators::KatelloNameFormatValidator, :attributes => :name
validates :name, :presence => true
@@ -55,14 +45,17 @@
end
end
end
validates_with Validators::ContentViewEnvironmentValidator
- scope :in_environment, lambda { |env| where(:environment_id => env) }
+ scope :in_environment, ->(env) { where(:environment_id => env) }
scoped_search :on => :name, :complete_value => true
scoped_search :on => :organization_id, :complete_value => true
+ scoped_search :rename => :environment, :on => :name, :in => :environment, :complete_value => true
+ scoped_search :rename => :content_view, :on => :name, :in => :content_view, :complete_value => true
+ scoped_search :on => :content_view_id, :complete_value => true
def environment_exists
if environment_id && environment.nil?
errors.add(:environment, _("ID: %s doesn't exist ") % environment_id)
elsif !environment.nil? && environment.organization != self.organization
@@ -84,55 +77,39 @@
else
self.organization.library.available_releases
end
end
- # For efficiency, sometimes the candlepin pool objects have already been fetched so allow
- # them to be passed in directly. By default, a call to candlepin will be made
- def subscriptions(cp_pools = nil)
- cp_pools ||= self.get_key_pools
-
- pools = cp_pools.collect { |cp_pool| Pool.find_pool(cp_pool['id'], cp_pool) }
-
- subscriptions = pools.collect do |pool|
- product = Product.where(:cp_id => pool.product_id).first
- next if product.nil?
- pool.provider_id = product.provider_id
- pool
- end
- subscriptions.compact
+ def subscriptions
+ self.pools
end
def available_subscriptions
- all_pools = self.get_pools
- key_pool_ids = self.get_key_pools.collect { |pool| pool[:id] }
- pools = all_pools.reject { |pool| key_pool_ids.include? pool[:id] }
- self.subscriptions(pools)
+ all_pools = self.get_pools.map { |pool| pool["id"] }
+ added_pools = self.get_key_pools.map { |pool| pool["id"] }
+ available_pools = all_pools - added_pools
+ Pool.where(:cp_id => available_pools)
end
def products
all_products = []
cp_pools = self.get_key_pools
if cp_pools
- pools = cp_pools.collect { |cp_pool| Pool.find_pool(cp_pool['id'], cp_pool) }
- product_ids = pools.map(&:product_id)
- marketing_products = MarketingProduct.includes(:engineering_products, :marketing_engineering_products).
- where(:cp_id => product_ids)
- products = Product.where(:cp_id => product_ids).where('type != ?', "Katello::MarketingProduct")
-
- marketing_products.each do |product|
- all_products += product.engineering_products
+ pools = cp_pools.collect { |cp_pool| Pool.find_by_cp_id(cp_pool['id']) }
+ pools.each do |pool|
+ all_products << pool.subscription.products
end
-
- all_products += products
end
-
- all_products
+ all_products.flatten!
end
def available_content
self.products.map(&:available_content).flatten
+ end
+
+ def valid_content_label?(content_label)
+ self.available_content.map(&:content).any? { |content| content.label == content_label }
end
# sets up system when registering with this activation key - must be executed in a transaction
def apply_to_system(system)
if !max_content_hosts.nil? && !self.unlimited_content_hosts && usage_count >= max_content_hosts