Sha256: 99325657fd8fdbc9190c3e6ac3b6f90efb9f77e5c467a0e5d269b4b09159039d

Contents?: true

Size: 1.7 KB

Versions: 1

Compression:

Stored size: 1.7 KB

Contents

module Katello
  module Glue::Candlepin::CandlepinObject
    extend ActiveSupport::Concern

    module ClassMethods
      def get_for_organization(organization)
        # returns objects from AR database rather than candlepin data
        pool_ids = self.get_for_owner(organization.label).collect { |x| x['id'] }
        self.where(:cp_id => pool_ids)
      end

      def get_candlepin_ids(organization)
        self.get_for_owner(organization.label).map { |subscription| subscription["id"] }
      end

      def import_candlepin_ids(organization)
        candlepin_ids = self.get_candlepin_ids(organization)
        candlepin_ids.each do |cp_id|
          self.where(:cp_id => cp_id, :organization_id => organization.id).first_or_create unless cp_id.nil?
        end
        candlepin_ids
      end

      def with_identifier(ids)
        self.with_identifiers(ids).first
      end

      def with_identifiers(ids)
        ids = [ids] unless ids.is_a?(Array)
        ids.map!(&:to_s)
        id_integers = ids.map { |string| Integer(string) rescue -1 }
        where("#{self.table_name}.id = (?) or #{self.table_name}.cp_id = (?)", id_integers, ids)
      end

      def import_all(organization = nil)
        organizations = organization ? [organization] : Organization.all

        organizations.each do |org|
          candlepin_ids = import_candlepin_ids(org)

          objects = self.in_organization(org)
          objects.each do |item|
            if candlepin_ids.include?(item.cp_id)
              item.import_data
              item.import_managed_associations if item.respond_to?(:import_managed_associations)
            else
              item.destroy
            end
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
katello-3.7.0.rc1 app/models/katello/glue/candlepin/candlepin_object.rb