Class: Postnhost::Publishing::BulkTransition

Inherits:
BaseService
  • Object
show all
Defined in:
app/services/postnhost/publishing/bulk_transition.rb

Instance Method Summary collapse

Methods inherited from BaseService

call

Constructor Details

#initialize(scope:, ids:, service_class:, argument_name:) ⇒ BulkTransition

Returns a new instance of BulkTransition.



4
5
6
7
8
9
# File 'app/services/postnhost/publishing/bulk_transition.rb', line 4

def initialize(scope:, ids:, service_class:, argument_name:)
  @scope = scope
  @ids = ids
  @service_class = service_class
  @argument_name = argument_name
end

Instance Method Details

#callObject



11
12
13
14
15
16
17
18
19
20
21
22
# File 'app/services/postnhost/publishing/bulk_transition.rb', line 11

def call
  normalized_ids = normalize_ids
  return failure("Select at least one translation") if normalized_ids.empty?

  records = scope.where(id: normalized_ids).order(:id).to_a
  successes, failures = transition_all(records)
  (normalized_ids - records.map(&:id)).each { |id| failures[id] = ["Translation was not found"] }

  success({ successes:, failures: }, status: failures.empty? ? :ok : :unprocessable_content)
rescue ArgumentError, TypeError
  failure("Translation IDs must be integers")
end