Module: Batch::RequestBehaviour

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object


2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/models/batch/request_behaviour.rb', line 2

def self.included(base)
  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

#create_batch_request!(*args) ⇒ Object


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

def create_batch_request!(*args)
  # I think this is actually deprecated
  create_batch_request(args)
end

#recycle_from_batch!Object


30
31
32
33
34
35
36
# File 'app/models/batch/request_behaviour.rb', line 30

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


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

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)

26
27
28
# File 'app/models/batch/request_behaviour.rb', line 26

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