Class: Spree::Export
Constant Summary
collapse
- SUPPORTED_FILE_FORMATS =
%i[csv].freeze
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.available_models ⇒ Object
191
192
193
|
# File 'app/models/spree/export.rb', line 191
def available_models
available_types.map(&:model_class)
end
|
.available_types ⇒ Object
187
188
189
|
# File 'app/models/spree/export.rb', line 187
def available_types
Spree.export_types
end
|
.model_class ⇒ Object
eg. Spree::Exports::Products => Spree::Product
200
201
202
203
204
205
206
|
# File 'app/models/spree/export.rb', line 200
def model_class
klass = "Spree::#{to_s.demodulize.singularize}".safe_constantize
raise NameError, "Missing model class for #{self}" unless klass
klass
end
|
.type_for_model(model) ⇒ Object
195
196
197
|
# File 'app/models/spree/export.rb', line 195
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
110
111
112
|
# File 'app/models/spree/export.rb', line 110
def build_csv_line(_record)
raise NotImplementedError, 'build_csv_line must be implemented'
end
|
99
100
101
|
# File 'app/models/spree/export.rb', line 99
def
raise NotImplementedError, 'csv_headers must be implemented'
end
|
#current_ability ⇒ Object
169
170
171
|
# File 'app/models/spree/export.rb', line 169
def current_ability
@current_ability ||= Spree.ability_class.new(user, { store: store })
end
|
#done? ⇒ Boolean
64
65
66
|
# File 'app/models/spree/export.rb', line 64
def done?
attachment.present? && attachment.attached?
end
|
#event_serializer_class ⇒ Object
#export_file_name ⇒ Object
eg. Spree::Exports::Products => products-store-my-store-code-20241030133348.csv
174
175
176
|
# File 'app/models/spree/export.rb', line 174
def export_file_name
"#{type.demodulize.underscore}-#{store.code}-#{created_at.strftime('%Y%m%d%H%M%S')}.#{format}"
end
|
#export_tmp_file_path ⇒ Object
178
179
180
|
# File 'app/models/spree/export.rb', line 178
def export_tmp_file_path
Rails.root.join('tmp', export_file_name)
end
|
#generate ⇒ Object
72
73
74
75
76
|
# File 'app/models/spree/export.rb', line 72
def generate
send(:"generate_#{format}")
handle_attachment
send_export_done_email
end
|
#generate_async ⇒ Object
68
69
70
|
# File 'app/models/spree/export.rb', line 68
def generate_async
Spree::Exports::GenerateJob.perform_later(id)
end
|
#generate_csv ⇒ Object
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
# File 'app/models/spree/export.rb', line 78
def generate_csv
::CSV.open(export_tmp_file_path, 'wb', encoding: 'UTF-8', col_sep: ',', row_sep: "\r\n") do |csv|
csv <<
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_attachment ⇒ Object
114
115
116
117
118
|
# File 'app/models/spree/export.rb', line 114
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
|
Returns an array of metafield headers for the model
106
107
108
|
# File 'app/models/spree/export.rb', line 106
def
@metafields_headers ||= Spree::MetafieldDefinition.for_resource_type(model_class.to_s).order(:namespace, :key).map(&:csv_header_name)
end
|
#model_class ⇒ Object
eg. Spree::Exports::Products => Spree::Product
140
141
142
143
144
145
146
|
# File 'app/models/spree/export.rb', line 140
def model_class
if type == 'Spree::Exports::Customers'
Spree.user_class
else
"Spree::#{type.demodulize.singularize}".constantize
end
end
|
#multi_line_csv? ⇒ Boolean
95
96
97
|
# File 'app/models/spree/export.rb', line 95
def multi_line_csv?
false
end
|
#normalize_search_params ⇒ Object
Ensures search params maintain consistent format between UI and exports
-
Preserves valid JSON unchanged
-
Converts Ruby hashes to JSON strings
-
Handles malformed input gracefully
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
# File 'app/models/spree/export.rb', line 152
def normalize_search_params
return if search_params.blank?
if search_params.is_a?(Hash)
self.search_params = search_params.to_json
return
end
begin
parsed = JSON.parse(search_params.to_s)
self.search_params = parsed.to_json
rescue JSON::ParserError
end
end
|
#records_to_export ⇒ Object
127
128
129
130
131
132
133
|
# File 'app/models/spree/export.rb', line 127
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
|
#scope ⇒ Object
120
121
122
123
124
125
|
# File 'app/models/spree/export.rb', line 120
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_includes ⇒ Object
135
136
137
|
# File 'app/models/spree/export.rb', line 135
def scope_includes
[]
end
|
#send_export_done_email ⇒ Object
182
183
184
|
# File 'app/models/spree/export.rb', line 182
def send_export_done_email
Spree::ExportMailer.export_done(self).deliver_later
end
|