Module: Transfer::Associations

Included in:
Plate, Tube
Defined in:
app/models/transfer/associations.rb

Overview

Include in assets that can act as sources/destinations for transfers

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object

rubocop:todo Metrics/AbcSize, Metrics/MethodLength



5
6
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
37
# File 'app/models/transfer/associations.rb', line 5

def self.included(base) # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
  base.class_eval do
    include Transfer::State

    has_many :transfers_as_source, # rubocop:todo Rails/HasManyOrHasOneDependent
             -> { order(created_at: :asc) },
             class_name: 'Transfer',
             foreign_key: :source_id,
             inverse_of: :source

    has_many :transfers_to_tubes, # rubocop:todo Rails/HasManyOrHasOneDependent
             -> { order(created_at: :asc) },
             class_name: 'Transfer::BetweenPlateAndTubes',
             foreign_key: :source_id,
             inverse_of: :source

    has_many :transfers_as_destination, # rubocop:todo Rails/HasManyOrHasOneDependent
             -> { order(id: :asc) },
             class_name: 'Transfer',
             foreign_key: :destination_id,
             inverse_of: :destination

    # This looks odd but it's a LEFT OUTER JOIN, meaning that the rows we would be interested in have no source_id.
    scope :with_no_outgoing_transfers,
          lambda {
            select("DISTINCT #{base.quoted_table_name}.*").joins(
              # rubocop:todo Layout/LineLength
              "LEFT OUTER JOIN `transfers` outgoing_transfers ON outgoing_transfers.`source_id`=#{base.quoted_table_name}.`id`"
              # rubocop:enable Layout/LineLength
            ).where('outgoing_transfers.source_id IS NULL')
          }
  end
end