Class: Spree::Translations::Batch
- Inherits:
-
Object
- Object
- Spree::Translations::Batch
- 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.
Defined Under Namespace
Classes: EmptyError, EntryError
Instance Attribute Summary collapse
-
#records ⇒ Array<Spree.base_class>
readonly
Records written by the last successful #process!.
Instance Method Summary collapse
-
#initialize(entries) ⇒ Batch
constructor
A new instance of Batch.
-
#process! {|record| ... } ⇒ Array<Spree.base_class>
Upserts every entry in one transaction.
-
#required_scopes ⇒ Array<String>
Distinct
write_<resource>scopes a caller needs to authorize this batch — one per resource type present.
Constructor Details
#initialize(entries) ⇒ Batch
Returns a new instance of Batch.
41 42 43 44 |
# File 'app/models/spree/translations/batch.rb', line 41 def initialize(entries) @entries = Array(entries) @records = [] end |
Instance Attribute Details
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.
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_scopes ⇒ Array<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.
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 |