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

alias_association, convert_labware_to_receptacle_for, find_by_id_or_name, find_by_id_or_name!

Methods included from Squishify

extended

Class Method Details

.construct!(arguments) ⇒ Object



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

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) { |catalogue| catalogue.product_product_catalogues.build(product_assocations) }
  end
end

Instance Method Details

#product_for(submission_attributes) ⇒ Object



43
44
45
# File 'app/models/product_catalogue.rb', line 43

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

#product_with_criteria(criteria) ⇒ Object



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

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

#product_with_default(criteria) ⇒ Object



51
52
53
54
55
56
57
58
# File 'app/models/product_catalogue.rb', line 51

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