Class: ProductCatalogue

Inherits:
ApplicationRecord show all
Includes:
HasBehaviour
Defined in:
app/models/product_catalogue.rb

Overview

Product catalogues provide a means of associating products with a submission template. selection_behaviour can allow a submission template to select an appropriate product. Ideally we want to deprecate submission templates in favour of products.

Defined Under Namespace

Classes: LibraryDriven, Manual, SingleProduct

Constant Summary collapse

UndefinedBehaviour =
Class.new(StandardError)

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HasBehaviour

included

Methods inherited from ApplicationRecord

convert_labware_to_receptacle_for, find_by_id_or_name, find_by_id_or_name!

Methods included from Warren::BroadcastMessages

#broadcast, included, #queue_associated_for_broadcast, #queue_for_broadcast, #warren

Class Method Details

.construct!(arguments) ⇒ Object


30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/models/product_catalogue.rb', line 30

def construct!(arguments)
  ActiveRecord::Base.transaction do
    products = arguments.delete(:products)
    product_assocations = products.map do |criterion, product_name|
      {
        selection_criterion: criterion,
        product: Product.find_or_create_by(name: product_name)
      }
    end
    create!(arguments) do |catalogue|
      catalogue.product_product_catalogues.build(product_assocations)
    end
  end
end

Instance Method Details

#product_for(submission_attributes) ⇒ Object


46
47
48
# File 'app/models/product_catalogue.rb', line 46

def product_for(submission_attributes)
  selection_class.new(self, submission_attributes).product
end

#product_with_criteria(criteria) ⇒ Object


50
51
52
# File 'app/models/product_catalogue.rb', line 50

def product_with_criteria(criteria)
  products.find_by(product_product_catalogues: { selection_criterion: criteria })
end

#product_with_default(criteria) ⇒ Object


54
55
56
57
58
59
60
61
# File 'app/models/product_catalogue.rb', line 54

def product_with_default(criteria)
  # Order of priorities to select a Product:
  # In a LibraryDriven selection we select the Product with this priorities:
  # 1- The product linked with the library type
  # 2- The first product linked with "nil" SelectionCriterion
  # 3- nil in any other case
  product_with_criteria(criteria) || product_with_criteria(nil)
end