Module: Batch::RequestBehaviour

Included in:
Request
Defined in:
app/models/batch/request_behaviour.rb

Overview

rubocop:todo Style/Documentation

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

rubocop:todo Metrics/MethodLength


3
4
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
# File 'app/models/batch/request_behaviour.rb', line 3

def self.included(base) # rubocop:todo Metrics/MethodLength
  base.class_eval do
    has_one :batch_request, inverse_of: :request, dependent: :destroy
    has_one :batch, through: :batch_request, inverse_of: :requests

    scope :include_for_batch_view,
          -> { includes(:batch_request, :asset, :target_asset, :request_metadata, :comments) }

    # For backwards compatibility
    def batch_requests
      [batch_request].compact
    end

    def batches
      [batch].compact
    end

    # Identifies all requests that are not part of a batch.
    # Note: we join, rather than includes due to custom select limitations.
    scope :unbatched,
          -> {
            joins('LEFT OUTER JOIN batch_requests ON batch_requests.request_id = requests.id')
              .readonly(false)
              .where(batch_requests: { request_id: nil })
          }

    delegate :position, to: :batch_request, allow_nil: true
  end
end

Instance Method Details

#recycle_from_batch!Object


37
38
39
40
41
42
43
# File 'app/models/batch/request_behaviour.rb', line 37

def recycle_from_batch!
  ActiveRecord::Base.transaction do
    return_for_inbox!
    batch_request.destroy if batch_request.present?
    save!
  end
end

#return_for_inbox!Object


45
46
47
48
49
50
# File 'app/models/batch/request_behaviour.rb', line 45

def return_for_inbox!
  # Valid for started, cancelled and pending batches
  # Will raise an exception outside of this
  cancel! if started?
  detach! unless pending?
end

#with_batch_id {|batch.id| ... } ⇒ Object

Yields:

  • (batch.id)

33
34
35
# File 'app/models/batch/request_behaviour.rb', line 33

def with_batch_id
  yield batch.id if batch.present?
end