Class: StockAssetsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/stock_assets_controller.rb

Overview

The stock assets controller makes stock assets for the products of batches. Originally this behaviour was on BathcesController#new_stock_assets.

Instance Method Summary collapse

Methods inherited from ApplicationController

#block_api_access, #clean_params_from_check, #evil_parameter_hack!, #extract_header_info, #set_cache_disabled!

Instance Method Details

#createObject


38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/controllers/stock_assets_controller.rb', line 38

def create
  params[:assets].each do |id, params|
    asset = Labware.find(id)
    stock_asset = asset.create_stock_asset!(
      name: params[:name],
      volume: params[:volume],
      concentration: params[:concentration]
    )
    stock_asset.assign_relationships(asset.parents, asset)
  end
  flash[:notice] = "#{params[:assets].to_unsafe_h.count} stock #{'tubes'.pluralize(params[:assets].to_unsafe_h.count)} created"
  redirect_to batch_path(params[:batch_id])
end

#newObject


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/controllers/stock_assets_controller.rb', line 7

def new
  if @batch.requests.empty?
    redirect_to @batch, alert: 'No requests to create stock tubes'
  else
    batch_assets = if @batch.multiplexed?
                     candidate_multiplexed_library = @batch.target_assets.first.children.first

                     if candidate_multiplexed_library.nil?
                       redirect_to batch_path(@batch), alert: "There's no multiplexed library tube available to have a stock tube."
                       []
                     elsif candidate_multiplexed_library.has_stock_asset? || candidate_multiplexed_library.is_a_stock_asset?
                       redirect_to batch_path(@batch), alert: 'Stock tubes have already been created'
                       []
                     else
                       [candidate_multiplexed_library]
                     end

                   else
                     @batch.target_assets.reject(&:has_stock_asset?).tap do |batch_assets|
                       if batch_assets.empty?
                         redirect_to batch_path(@batch), alert: 'Stock tubes have already been created'
                       end
                     end
                   end

    @assets = batch_assets.each_with_object({}) do |asset, assets|
      assets[asset.labware.id] = asset.labware.new_stock_asset
    end
  end
end