Module: Glue::Candlepin::Product

Included in:
Katello::Product
Defined in:
app/models/katello/glue/candlepin/product.rb

Defined Under Namespace

Modules: InstanceMethods

Class Method Summary collapse

Class Method Details

.import_from_cp(attrs = nil, &block) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/models/katello/glue/candlepin/product.rb', line 27

def self.import_from_cp(attrs = nil, &block)
  product_content_attrs = attrs.delete(:productContent) || []
  import_logger        = attrs[:import_logger]

  attrs = attrs.merge('name' => validate_name(attrs['name']), 'label' => Util::Model.labelize(attrs['name']))

  product = Product.new(attrs, &block)
  product.orchestration_for = :import_from_cp_ar_setup
  product.save!
  product.productContent_will_change!
  product.productContent = product.build_product_content(product_content_attrs)
  product.orchestration_for = :import_from_cp
  product.save!

rescue => e
  [Rails.logger, import_logger].each do |logger|
    logger.error "Failed to create product #{attrs['name']}: #{e}" if logger
  end
  raise e
end

.included(base) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'app/models/katello/glue/candlepin/product.rb', line 3

def self.included(base)
  base.send :include, LazyAccessor
  base.send :include, InstanceMethods

  base.class_eval do
    lazy_accessor :productContent, :multiplier, :href, :attrs,
      :initializer => lambda { |_s| convert_from_cp_fields(Resources::Candlepin::Product.get(cp_id)[0]) }
    # Entitlement Certificate for this product
    lazy_accessor :certificate,
      :initializer => lambda { |_s| Resources::Candlepin::Product.certificate(cp_id, self.organization.label) },
      :unless => lambda { |_s| cp_id.nil? }
    # Entitlement Key for this product
    lazy_accessor :key, :initializer => lambda { |_s| Resources::Candlepin::Product.key(cp_id, self.organization.label) }, :unless => lambda { |_s| cp_id.nil? }

    # we must store custom logger object during product importing so we can log status
    # from various places like callbacks
    attr_accessor :import_logger
  end
end

.validate_name(name) ⇒ Object



23
24
25
# File 'app/models/katello/glue/candlepin/product.rb', line 23

def self.validate_name(name)
  name.gsub(/[^a-z0-9\-_ ]/i, "")
end