Class: Spree::Report
Class Method Summary
collapse
-
.available_types ⇒ Array<Class>
Registered report subclasses, mirroring Spree::Export/Spree::Import so api_type_for can resolve an STI column value without constantizing it.
-
.report_type ⇒ Object
deprecated
Deprecated.
Use .api_type — identical derivation, consistent with the
rest of the API's type fields.
Instance Method Summary
collapse
Class Method Details
.available_types ⇒ Array<Class>
Registered report subclasses, mirroring Spree::Export/Spree::Import so
api_type_for can resolve an STI column value without constantizing it.
44
45
46
|
# File 'app/models/spree/report.rb', line 44
def self.available_types
Spree.reports
end
|
.report_type ⇒ Object
Deprecated. Use .api_type — identical derivation, consistent with the
rest of the API's type fields.
50
51
52
|
# File 'app/models/spree/report.rb', line 50
def self.report_type
api_type
end
|
Instance Method Details
#attachment_file_name ⇒ Object
eg. "store-sales-total-report-20250201120000.csv"
122
123
124
|
# File 'app/models/spree/report.rb', line 122
def attachment_file_name
@attachment_file_name ||= "#{store.code}-#{type.demodulize.parameterize}-report-#{created_at.strftime('%Y%m%d%H%M%S')}.csv"
end
|
#event_serializer_class ⇒ Object
36
37
38
|
# File 'app/models/spree/report.rb', line 36
def event_serializer_class
'Spree::Api::V3::ReportSerializer'.safe_constantize
end
|
#generate ⇒ Object
87
88
89
90
91
|
# File 'app/models/spree/report.rb', line 87
def generate
generate_csv
handle_attachment
send_report_done_email
end
|
#generate_async ⇒ Object
83
84
85
|
# File 'app/models/spree/report.rb', line 83
def generate_async
Spree::Reports::GenerateJob.perform_later(id)
end
|
#generate_csv ⇒ Object
93
94
95
96
97
98
99
100
101
102
|
# File 'app/models/spree/report.rb', line 93
def generate_csv
::CSV.open(report_tmp_file_path, 'wb', encoding: 'UTF-8', col_sep: ',', row_sep: "\r\n") do |csv|
csv << line_item_class.
line_items_scope.find_in_batches do |batch|
batch.each do |record|
csv << line_item_class.new(record: record, report: self).to_csv
end
end
end
end
|
#handle_attachment ⇒ Object
104
105
106
107
108
|
# File 'app/models/spree/report.rb', line 104
def handle_attachment
file = ::File.open(report_tmp_file_path)
attachment.attach(io: file, filename: attachment_file_name)
::File.delete(report_tmp_file_path) if ::File.exist?(report_tmp_file_path)
end
|
#human_name ⇒ Object
79
80
81
|
# File 'app/models/spree/report.rb', line 79
def human_name
[Spree.t("report_names.#{type.demodulize.underscore}"), store.name, date_from.strftime('%Y-%m-%d'), date_to.strftime('%Y-%m-%d')].join(' - ')
end
|
#line_item_class ⇒ Object
eg. Spree::ReportLineItems::SalesTotal
117
118
119
|
# File 'app/models/spree/report.rb', line 117
def line_item_class
"Spree::ReportLineItems::#{type.demodulize}".safe_constantize
end
|
Returns an array of report lines
64
65
66
67
68
69
|
# File 'app/models/spree/report.rb', line 64
def line_items(options = {})
scope = line_items_scope
scope = scope.limit(options[:limit]) if options[:limit].present?
scope.map { |record| line_item_class.new(record: record, report: self) }
end
|
#line_items_scope ⇒ ActiveRecord::Relation
Returns a scope of records to be used for generating report lines
57
58
59
|
# File 'app/models/spree/report.rb', line 57
def line_items_scope
raise NotImplementedError, "Subclass #{self.class.name} must implement #line_items_scope"
end
|
#no_report_data_partial_path ⇒ Object
75
76
77
|
# File 'app/models/spree/report.rb', line 75
def no_report_data_partial_path
'spree/admin/reports/no_report_data'
end
|
#send_report_done_email ⇒ Object
110
111
112
113
114
|
# File 'app/models/spree/report.rb', line 110
def send_report_done_email
return unless user.present?
Spree::ReportMailer.report_done(self).deliver_later
end
|
#to_partial_path ⇒ Object
71
72
73
|
# File 'app/models/spree/report.rb', line 71
def to_partial_path
'spree/admin/reports/report'
end
|