Class: Spree::Import

Inherits:
Object
  • Object
show all
Includes:
NumberIdentifier
Defined in:
app/models/spree/import.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.available_modelsArray<Class>

Returns the available models for the import

Returns:

  • (Array<Class>)


261
262
263
# File 'app/models/spree/import.rb', line 261

def available_models
  available_types.map(&:model_class)
end

.available_typesArray<Class>

Returns the available types for the import

Returns:

  • (Array<Class>)


255
256
257
# File 'app/models/spree/import.rb', line 255

def available_types
  Spree.import_types
end

.model_classObject

eg. Spree::Imports::Orders => Spree::Order

Raises:

  • (NameError)


272
273
274
275
276
277
278
279
280
# File 'app/models/spree/import.rb', line 272

def model_class
  return Spree.user_class if to_s == 'Spree::Imports::Customers'

  klass = "Spree::#{to_s.demodulize.singularize}".safe_constantize

  raise NameError, "Missing model class for #{self}" unless klass

  klass
end

.type_for_model(model) ⇒ Class

Returns the type for the model

Returns:

  • (Class)


267
268
269
# File 'app/models/spree/import.rb', line 267

def type_for_model(model)
  available_types.find { |type| type.model_class.to_s == model.to_s }
end

Instance Method Details

#attachment_file_contentString

Returns the content of the attachment file

Returns:

  • (String)


206
207
208
# File 'app/models/spree/import.rb', line 206

def attachment_file_content
  @attachment_file_content ||= attachment.attached? ? attachment.blob.download&.force_encoding('UTF-8') : nil
end

#complete?Boolean

Returns true if the import is complete

Returns:

  • (Boolean)


109
110
111
# File 'app/models/spree/import.rb', line 109

def complete?
  status == 'completed'
end

#create_mappingsObject

Creates mappings from the schema fields TODO: get mappings from the previous import if it exists, so user won’t have to map the same columns again



212
213
214
215
216
217
218
# File 'app/models/spree/import.rb', line 212

def create_mappings
  schema_fields.each do |schema_field|
    mapping = mappings.find_or_create_by!(schema_field: schema_field[:name])
    mapping.try_to_auto_assign_file_column(csv_headers)
    mapping.save!
  end
end

#create_rows_asyncvoid

This method returns an undefined value.

Creates rows asynchronously



222
223
224
# File 'app/models/spree/import.rb', line 222

def create_rows_async
  Spree::Imports::CreateRowsJob.set(wait: 2.seconds).perform_later(id)
end

#csv_headersArray<String>

Returns the headers of the csv file

Returns:

  • (Array<String>)


195
196
197
198
199
200
201
202
# File 'app/models/spree/import.rb', line 195

def csv_headers
  return [] if attachment_file_content.blank?

  @csv_headers ||= ::CSV.parse_line(
    attachment_file_content,
    col_sep: preferred_delimiter
  )
end

#current_abilitySpree::Ability

Returns the current ability for the import

Returns:



244
245
246
# File 'app/models/spree/import.rb', line 244

def current_ability
  @current_ability ||= Spree.ability_class.new(user, { store: store })
end

#display_nameString

Returns the display name for the import

Returns:

  • (String)


181
182
183
# File 'app/models/spree/import.rb', line 181

def display_name
  "#{Spree.t(type.demodulize.pluralize.downcase)} #{number}"
end

#event_serializer_classObject



248
249
250
# File 'app/models/spree/import.rb', line 248

def event_serializer_class
  'Spree::Api::V3::ImportSerializer'.safe_constantize
end

#group_columnString?

Returns the schema field name used to group rows for parallel processing. Rows sharing the same value in this field are processed together in one job. Returns nil for imports where rows are independent (default — batched in chunks).

Returns:

  • (String, nil)


91
92
93
# File 'app/models/spree/import.rb', line 91

def group_column
  nil
end

#import_schemaSpree::ImportSchema

Returns the import schema for the import

Returns:



125
126
127
# File 'app/models/spree/import.rb', line 125

def import_schema
  "Spree::ImportSchemas::#{type.demodulize}".safe_constantize.new
end

#large_import?Boolean

Returns true if the import has more rows than the large import threshold. Large imports skip per-row UI broadcasts and use bulk processing.

Returns:

  • (Boolean)


83
84
85
# File 'app/models/spree/import.rb', line 83

def large_import?
  rows_count >= Spree::Config[:large_import_threshold]
end

#mapped_fieldsArray<String>

Returns the mapped fields for the import schema

Returns:

  • (Array<String>)


169
170
171
# File 'app/models/spree/import.rb', line 169

def mapped_fields
  @mapped_fields ||= mappings.mapped.where(schema_field: required_fields)
end

#mapping?Boolean

Returns true if the import is in mapping state

Returns:

  • (Boolean)


97
98
99
# File 'app/models/spree/import.rb', line 97

def mapping?
  status == 'mapping'
end

#mapping_done?Boolean

Returns true if the mapping is done

Returns:

  • (Boolean)


175
176
177
# File 'app/models/spree/import.rb', line 175

def mapping_done?
  mapped_fields.count == required_fields.count
end

#model_classClass

Returns the model class for the import

Returns:

  • (Class)


115
116
117
118
119
120
121
# File 'app/models/spree/import.rb', line 115

def model_class
  if type == 'Spree::Imports::Customers'
    Spree.user_class
  else
    "Spree::#{type.demodulize.singularize}".safe_constantize
  end
end

#process_rows_asyncvoid

This method returns an undefined value.

Processes rows asynchronously



228
229
230
# File 'app/models/spree/import.rb', line 228

def process_rows_async
  Spree::Imports::ProcessRowsJob.perform_later(id)
end

#processing?Boolean

Returns true if the import is processing or completed mapping

Returns:

  • (Boolean)


103
104
105
# File 'app/models/spree/import.rb', line 103

def processing?
  ['processing', 'completed_mapping'].include?(status)
end

#publish_import_completed_eventObject



189
190
191
# File 'app/models/spree/import.rb', line 189

def publish_import_completed_event
  publish_event('import.completed')
end

#required_fieldsArray<String>

Returns the required fields for the import schema

Returns:

  • (Array<String>)


163
164
165
# File 'app/models/spree/import.rb', line 163

def required_fields
  import_schema.required_fields
end

#row_processor_classClass

Returns the row processor class for the import

Returns:

  • (Class)


131
132
133
# File 'app/models/spree/import.rb', line 131

def row_processor_class
  "Spree::ImportRowProcessors::#{type.demodulize.singularize}".safe_constantize
end

#schema_fieldsArray<Hash>

Returns the fields for the import schema If model supports metafields, it will include the metafield definitions for this model

Returns:

  • (Array<Hash>)


138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'app/models/spree/import.rb', line 138

def schema_fields
  base_fields = import_schema.fields

  # Dynamically add metafield definitions if the model supports metafields
  if model_class_supports_metafields?
    metafield_fields = metafield_definitions_for_model.map do |definition|
      {
        name: definition.csv_header_name,
        label: definition.name
      }
    end
    base_fields + metafield_fields
  else
    base_fields
  end
end

#storeSpree::Store

Returns the store for the import

Returns:



234
235
236
237
238
239
240
# File 'app/models/spree/import.rb', line 234

def store
  if owner.is_a?(Spree::Store)
    owner
  else
    owner.respond_to?(:store) ? owner.store : Spree::Store.default
  end
end

#touch_storeObject



185
186
187
# File 'app/models/spree/import.rb', line 185

def touch_store
  store.touch
end

#unmapped_file_columnsArray<String>

Returns the file columns that are not mapped

Returns:

  • (Array<String>)


157
158
159
# File 'app/models/spree/import.rb', line 157

def unmapped_file_columns
  csv_headers.reject { |header| mappings.mapped.exists?(file_column: header) }
end