Class: Spree::Export

Inherits:
Object
  • Object
show all
Includes:
NumberAsParam, NumberIdentifier, SingleStoreResource, VendorConcern
Defined in:
app/models/spree/export.rb

Constant Summary collapse

SUPPORTED_FILE_FORMATS =
i[csv].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.available_modelsObject



148
149
150
# File 'app/models/spree/export.rb', line 148

def available_models
  available_types.map(&:model_class)
end

.available_typesObject



144
145
146
# File 'app/models/spree/export.rb', line 144

def available_types
  Rails.application.config.spree.export_types
end

.model_classObject

eg. Spree::Exports::Products => Spree::Product



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

def model_class
  "Spree::#{to_s.demodulize.singularize}".constantize
end

.type_for_model(model) ⇒ Object



152
153
154
# File 'app/models/spree/export.rb', line 152

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

Instance Method Details

#build_csv_line(_record) ⇒ Object

Raises:

  • (NotImplementedError)


92
93
94
# File 'app/models/spree/export.rb', line 92

def build_csv_line(_record)
  raise NotImplementedError, 'build_csv_line must be implemented'
end

#csv_headersObject

Raises:

  • (NotImplementedError)


88
89
90
# File 'app/models/spree/export.rb', line 88

def csv_headers
  raise NotImplementedError, 'csv_headers must be implemented'
end

#current_abilityObject



126
127
128
# File 'app/models/spree/export.rb', line 126

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

#done?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'app/models/spree/export.rb', line 53

def done?
  attachment.present? && attachment.attached?
end

#export_file_nameObject

eg. Spree::Exports::Products => products-store-my-store-code-20241030133348.csv



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

def export_file_name
  "#{type.demodulize.underscore}-#{store.code}-#{created_at.strftime('%Y%m%d%H%M%S')}.#{format}"
end

#export_tmp_file_pathObject



135
136
137
# File 'app/models/spree/export.rb', line 135

def export_tmp_file_path
  Rails.root.join('tmp', export_file_name)
end

#generateObject



61
62
63
64
65
# File 'app/models/spree/export.rb', line 61

def generate
  send(:"generate_#{format}")
  handle_attachment
  send_export_done_email
end

#generate_asyncObject



57
58
59
# File 'app/models/spree/export.rb', line 57

def generate_async
  Spree::Exports::GenerateJob.perform_later(id)
end

#generate_csvObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/models/spree/export.rb', line 67

def generate_csv
  ::CSV.open(export_tmp_file_path, 'wb', encoding: 'UTF-8', col_sep: ',', row_sep: "\r\n") do |csv|
    csv << csv_headers
    records_to_export.includes(scope_includes).find_in_batches do |batch|
      batch.each do |record|
        if multi_line_csv?
          record.to_csv(store).each do |line|
            csv << line
          end
        else
          csv << record.to_csv(store)
        end
      end
    end
  end
end

#handle_attachmentObject



96
97
98
99
100
# File 'app/models/spree/export.rb', line 96

def handle_attachment
  file = ::File.open(export_tmp_file_path)
  attachment.attach(io: file, filename: export_file_name)
  ::File.delete(export_tmp_file_path) if ::File.exist?(export_tmp_file_path)
end

#model_classObject

eg. Spree::Exports::Products => Spree::Product



122
123
124
# File 'app/models/spree/export.rb', line 122

def model_class
  "Spree::#{type.demodulize.singularize}".constantize
end

#multi_line_csv?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'app/models/spree/export.rb', line 84

def multi_line_csv?
  false
end

#records_to_exportObject



109
110
111
112
113
114
115
# File 'app/models/spree/export.rb', line 109

def records_to_export
  if search_params.present?
    scope.ransack(search_params.is_a?(String) ? JSON.parse(search_params.to_s).to_h : search_params)
  else
    scope.ransack
  end.result
end

#scopeObject



102
103
104
105
106
107
# File 'app/models/spree/export.rb', line 102

def scope
  scope = model_class
  scope = scope.for_store(store) if model_class.respond_to?(:for_store)
  scope = scope.for_vendor(vendor) if model_class.respond_to?(:for_vendor) && vendor.present?
  scope.accessible_by(current_ability)
end

#scope_includesObject



117
118
119
# File 'app/models/spree/export.rb', line 117

def scope_includes
  []
end

#send_export_done_emailObject



139
140
141
# File 'app/models/spree/export.rb', line 139

def send_export_done_email
  Spree::ExportMailer.export_done(self).deliver_later
end