Class: Spree::Import
- Inherits:
-
Object
- Object
- Spree::Import
- Includes:
- NumberIdentifier
- Defined in:
- app/models/spree/import.rb
Direct Known Subclasses
Spree::Imports::Customers, Spree::Imports::ProductTranslations, Spree::Imports::Products
Class Method Summary collapse
-
.available_models ⇒ Array<Class>
Returns the available models for the import.
-
.available_types ⇒ Array<Class>
Returns the available types for the import.
-
.model_class ⇒ Object
eg.
-
.type_for_model(model) ⇒ Class
Returns the type for the model.
Instance Method Summary collapse
-
#attachment_file_content ⇒ String
Returns the content of the attachment file.
-
#complete? ⇒ Boolean
Returns true if the import is complete.
-
#create_mappings ⇒ Object
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.
-
#create_rows_async ⇒ void
Creates rows asynchronously.
-
#csv_headers ⇒ Array<String>
Returns the headers of the csv file.
-
#current_ability ⇒ Spree::Ability
Returns the current ability for the import.
-
#display_name ⇒ String
Returns the display name for the import.
- #event_serializer_class ⇒ Object
-
#group_column ⇒ String?
Returns the schema field name used to group rows for parallel processing.
-
#import_schema ⇒ Spree::ImportSchema
Returns the import schema for the import.
-
#large_import? ⇒ Boolean
Returns true if the import has more rows than the large import threshold.
-
#mapped_fields ⇒ Array<String>
Returns the mapped fields for the import schema.
-
#mapping? ⇒ Boolean
Returns true if the import is in mapping state.
-
#mapping_done? ⇒ Boolean
Returns true if the mapping is done.
-
#model_class ⇒ Class
Returns the model class for the import.
-
#process_rows_async ⇒ void
Processes rows asynchronously.
-
#processing? ⇒ Boolean
Returns true if the import is processing or completed mapping.
- #publish_import_completed_event ⇒ Object
-
#required_fields ⇒ Array<String>
Returns the required fields for the import schema.
-
#row_processor_class ⇒ Class
Returns the row processor class for the import.
-
#schema_fields ⇒ Array<Hash>
Returns the fields for the import schema If model supports metafields, it will include the metafield definitions for this model.
-
#store ⇒ Spree::Store
Returns the store for the import.
-
#unmapped_file_columns ⇒ Array<String>
Returns the file columns that are not mapped.
Class Method Details
.available_models ⇒ Array<Class>
Returns the available models for the import
256 257 258 |
# File 'app/models/spree/import.rb', line 256 def available_models available_types.map(&:model_class) end |
.available_types ⇒ Array<Class>
Returns the available types for the import
250 251 252 |
# File 'app/models/spree/import.rb', line 250 def available_types Spree.import_types end |
.model_class ⇒ Object
eg. Spree::Imports::Orders => Spree::Order
267 268 269 270 271 272 273 274 275 |
# File 'app/models/spree/import.rb', line 267 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
262 263 264 |
# File 'app/models/spree/import.rb', line 262 def type_for_model(model) available_types.find { |type| type.model_class.to_s == model.to_s } end |
Instance Method Details
#attachment_file_content ⇒ String
Returns the content of the attachment file
201 202 203 |
# File 'app/models/spree/import.rb', line 201 def @attachment_file_content ||= .attached? ? .blob.download&.force_encoding('UTF-8') : nil end |
#complete? ⇒ Boolean
Returns true if the import is complete
108 109 110 |
# File 'app/models/spree/import.rb', line 108 def complete? status == 'completed' end |
#create_mappings ⇒ Object
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
207 208 209 210 211 212 213 |
# File 'app/models/spree/import.rb', line 207 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_async ⇒ void
This method returns an undefined value.
Creates rows asynchronously
217 218 219 |
# File 'app/models/spree/import.rb', line 217 def create_rows_async Spree::Imports::CreateRowsJob.set(wait: 2.seconds).perform_later(id) end |
#csv_headers ⇒ Array<String>
Returns the headers of the csv file
190 191 192 193 194 195 196 197 |
# File 'app/models/spree/import.rb', line 190 def csv_headers return [] if .blank? @csv_headers ||= ::CSV.parse_line( , col_sep: preferred_delimiter ) end |
#current_ability ⇒ Spree::Ability
Returns the current ability for the import
239 240 241 |
# File 'app/models/spree/import.rb', line 239 def current_ability @current_ability ||= Spree.ability_class.new(user, { store: store }) end |
#display_name ⇒ String
Returns the display name for the import
180 181 182 |
# File 'app/models/spree/import.rb', line 180 def display_name "#{Spree.t(type.demodulize.pluralize.downcase)} #{number}" end |
#event_serializer_class ⇒ Object
243 244 245 |
# File 'app/models/spree/import.rb', line 243 def event_serializer_class 'Spree::Api::V3::ImportSerializer'.safe_constantize end |
#group_column ⇒ String?
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).
90 91 92 |
# File 'app/models/spree/import.rb', line 90 def group_column nil end |
#import_schema ⇒ Spree::ImportSchema
Returns the import schema for the import
124 125 126 |
# File 'app/models/spree/import.rb', line 124 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.
82 83 84 |
# File 'app/models/spree/import.rb', line 82 def large_import? rows_count >= Spree::Config[:large_import_threshold] end |
#mapped_fields ⇒ Array<String>
Returns the mapped fields for the import schema
168 169 170 |
# File 'app/models/spree/import.rb', line 168 def mapped_fields @mapped_fields ||= mappings.mapped.where(schema_field: required_fields) end |
#mapping? ⇒ Boolean
Returns true if the import is in mapping state
96 97 98 |
# File 'app/models/spree/import.rb', line 96 def mapping? status == 'mapping' end |
#mapping_done? ⇒ Boolean
Returns true if the mapping is done
174 175 176 |
# File 'app/models/spree/import.rb', line 174 def mapping_done? mapped_fields.count == required_fields.count end |
#model_class ⇒ Class
Returns the model class for the import
114 115 116 117 118 119 120 |
# File 'app/models/spree/import.rb', line 114 def model_class if type == 'Spree::Imports::Customers' Spree.user_class else "Spree::#{type.demodulize.singularize}".safe_constantize end end |
#process_rows_async ⇒ void
This method returns an undefined value.
Processes rows asynchronously
223 224 225 |
# File 'app/models/spree/import.rb', line 223 def process_rows_async Spree::Imports::ProcessRowsJob.perform_later(id) end |
#processing? ⇒ Boolean
Returns true if the import is processing or completed mapping
102 103 104 |
# File 'app/models/spree/import.rb', line 102 def processing? ['processing', 'completed_mapping'].include?(status) end |
#publish_import_completed_event ⇒ Object
184 185 186 |
# File 'app/models/spree/import.rb', line 184 def publish_import_completed_event publish_event('import.completed') end |
#required_fields ⇒ Array<String>
Returns the required fields for the import schema
162 163 164 |
# File 'app/models/spree/import.rb', line 162 def required_fields import_schema.required_fields end |
#row_processor_class ⇒ Class
Returns the row processor class for the import
130 131 132 |
# File 'app/models/spree/import.rb', line 130 def row_processor_class "Spree::ImportRowProcessors::#{type.demodulize.singularize}".safe_constantize end |
#schema_fields ⇒ Array<Hash>
Returns the fields for the import schema If model supports metafields, it will include the metafield definitions for this model
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'app/models/spree/import.rb', line 137 def schema_fields base_fields = import_schema.fields # Dynamically add metafield definitions if the model supports metafields if = .map do |definition| { name: definition.csv_header_name, label: definition.name } end base_fields + else base_fields end end |
#store ⇒ Spree::Store
Returns the store for the import
229 230 231 232 233 234 235 |
# File 'app/models/spree/import.rb', line 229 def store if owner.is_a?(Spree::Store) owner else owner.respond_to?(:store) ? owner.store : Spree::Store.default end end |
#unmapped_file_columns ⇒ Array<String>
Returns the file columns that are not mapped
156 157 158 |
# File 'app/models/spree/import.rb', line 156 def unmapped_file_columns csv_headers.reject { |header| mappings.mapped.exists?(file_column: header) } end |