Class: Spree::SampleData::ImportBuilder

Inherits:
Object
  • Object
show all
Defined in:
app/services/spree/sample_data/import_builder.rb

Overview

Creates the Spree::Import record for a sample CSV, attaching the file but processing nothing — the two runners differ only in what they do next.

Class Method Summary collapse

Class Method Details

.call(csv_path:, import_class:, store: nil, user: nil, inline: false, skip_events: false) ⇒ Spree::Import

Returns persisted, unprocessed.

Returns:



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/services/spree/sample_data/import_builder.rb', line 7

def self.call(csv_path:, import_class:, store: nil, user: nil, inline: false, skip_events: false)
  store ||= Spree::Store.default
  # An admin of the target store, not `admin_user_class.first` — in a
  # multi-store or multi-tenant app that global first can belong to a
  # different store entirely.
  user ||= store.users.first

  raise 'No admin user found. Please run seeds first.' unless user

  import = import_class.new(owner: store, user: user)
  # Persisted before save: the processing jobs reload the record, so these
  # have to travel with it rather than live on the instance.
  import.preferred_inline = inline
  import.preferred_skip_events = skip_events
  import.number = import.generate_permalink(import_class)
  import.attachment.attach(
    io: File.open(csv_path),
    filename: File.basename(csv_path),
    content_type: 'text/csv'
  )
  import.save!(validate: false)
  import
end