app/models/katello/pool.rb in katello-2.2.2 vs app/models/katello/pool.rb in katello-2.4.0.rc1

- old
+ new

@@ -1,67 +1,80 @@ -# -# 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 Pool < Katello::Model + include Katello::Authorization::Pool + belongs_to :subscription, :inverse_of => :pools, :class_name => "Katello::Subscription" + + has_many :activation_keys, :through => :pool_activation_keys, :class_name => "Katello::ActivationKey" + has_many :pool_activation_keys, :class_name => "Katello::PoolActivationKey", :dependent => :destroy, :inverse_of => :pool + self.include_root_in_json = false include Glue::Candlepin::Pool - include Glue::ElasticSearch::Pool if Katello.config.use_elasticsearch + include Glue::Candlepin::CandlepinObject - self.table_name = "katello_pools" + scoped_search :on => :cp_id, :complete_value => true, :rename => :id + scoped_search :on => :quantity, :complete_value => true + scoped_search :on => :start_date, :complete_value => true, :rename => :starts + scoped_search :on => :end_date, :complete_value => true, :rename => :expires + scoped_search :on => :ram, :complete_value => true + scoped_search :on => :multi_entitlement, :complete_value => true + scoped_search :on => :consumed, :complete_value => true + scoped_search :on => :account_number, :complete_value => true, :rename => :account + scoped_search :on => :contract_number, :complete_value => true, :rename => :contract + scoped_search :on => :name, :in => :subscription, :complete_value => true, :rename => :name + scoped_search :on => :support_level, :in => :subscription, :complete_value => true + scoped_search :on => :sockets, :in => :subscription, :complete_value => true + scoped_search :on => :cores, :in => :subscription, :complete_value => true + scoped_search :on => :product_id, :in => :subscription, :complete_value => true + scoped_search :on => :stacking_id, :in => :subscription, :complete_value => true + scoped_search :on => :instance_multiplier, :in => :subscription, :complete_value => true - # Some fields are are not native to the Candlepin object but are useful for searching - attr_accessor :cp_provider_id - alias_method :provider_id, :cp_provider_id - alias_method :provider_id=, :cp_provider_id= - attr_accessor :cp_id - attr_accessor :subscription_id - attr_accessor :amount - validates_lengths_from_database DAYS_EXPIRING_SOON = 120 DAYS_RECENTLY_EXPIRED = 30 # ActivationKey includes the Pool's json in its own' def as_json(*_args) self.remote_data.merge(:cp_id => self.cp_id) end - # If the pool_json is passed in, then candlepin is not hit again to fetch it. This is for the case where - # prior to this call the pool was already fetched. - def self.find_pool(cp_id, pool_json = nil) - pool_json = Resources::Candlepin::Pool.find(cp_id) unless pool_json - Katello::Pool.new(pool_json) unless pool_json.nil? - end - # Convert active, expiring_soon, and recently_expired into elasticsearch # filters and move implementation into ES pool module if performance becomes # an issue (though I doubt it will--just sayin') def self.active(subscriptions) subscriptions.select { |s| s.active } end def self.expiring_soon(subscriptions) - subscriptions.select { |s| (s.end_date - Date.today) <= DAYS_EXPIRING_SOON } + subscriptions.select { |s| (s.end_date.to_date - Date.today) <= DAYS_EXPIRING_SOON } end def self.recently_expired(subscriptions) today_date = Date.today subscriptions.select do |s| - end_date = s.end_date + end_date = s.end_date.to_date today_date >= end_date && today_date - end_date <= DAYS_RECENTLY_EXPIRED end + end + + def quantity_available + return 0 unless self.quantity && self.consumed + self.quantity - self.consumed + end + + def type + self.pool_type + end + + def products + self.subscription.products if self.subscription + end + + private + + def default_sort + Pool.joins(:subscription).order("subscription.name") end end end