Class: Spree::Translations::Batch

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model
Defined in:
app/models/spree/translations/batch.rb

Overview

Atomically upserts translations across MANY records of (possibly) different translatable resource types in one operation — the domain behind POST /api/v3/admin/translations/batch.

The input is a flat list of independent registry writes, each naming its own resource_type + resource_id (no nested parent-owns-children payload), so there's no per-model branching. Records are resolved within the current store, then upserted in a single transaction: any failure rolls back the whole batch and surfaces the offending entry's index via a typed EntryError.

Examples:

Spree::Translations::Batch.new(entries).process! do |record|
  authorize!(:update, record)
end

Defined Under Namespace

Classes: EmptyError, EntryError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entries) ⇒ Batch

Returns a new instance of Batch.

Parameters:

  • entries (Array<Hash>)

    [{ resource_type:, resource_id:, values: { locale => { field => value } } }]



41
42
43
44
# File 'app/models/spree/translations/batch.rb', line 41

def initialize(entries)
  @entries = Array(entries)
  @records = []
end

Instance Attribute Details

#recordsArray<Spree.base_class> (readonly)

Returns records written by the last successful #process!.

Returns:

  • (Array<Spree.base_class>)

    records written by the last successful #process!



38
39
40
# File 'app/models/spree/translations/batch.rb', line 38

def records
  @records
end

Instance Method Details

#process! {|record| ... } ⇒ Array<Spree.base_class>

Upserts every entry in one transaction. Yields each resolved record before it is written so the caller can authorize it; a raised error in the block (or any entry failure) rolls back the whole batch.

Yield Parameters:

  • record (Spree.base_class)

    the resolved record, pre-write

Returns:

  • (Array<Spree.base_class>)

    the written records

Raises:

  • (EmptyError)

    when there are no entries

  • (EntryError)

    when an entry can't be resolved or saved



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/models/spree/translations/batch.rb', line 62

def process!
  raise EmptyError if @entries.empty?

  @records = []
  ActiveRecord::Base.transaction do
    @entries.each_with_index do |entry, index|
      record = resolve_record!(entry, index)
      yield record if block_given?
      upsert!(record, entry[:values], index)
      @records << record
    end
  end
  @records
end

#required_scopesArray<String>

Distinct write_<resource> scopes a caller needs to authorize this batch — one per resource type present. Lets the API layer gate an API-key request without re-deriving scope names.

Returns:

  • (Array<String>)


50
51
52
# File 'app/models/spree/translations/batch.rb', line 50

def required_scopes
  @entries.map { |entry| "write_#{entry[:resource_type].to_s.pluralize}" }.uniq
end