mirror of
https://github.com/internetee/registry.git
synced 2025-07-28 05:26:17 +02:00
Improve default csv generation
This commit is contained in:
parent
e50501f13f
commit
aef30eeb4d
5 changed files with 19 additions and 19 deletions
|
@ -35,7 +35,8 @@ module Admin
|
||||||
respond_to do |format|
|
respond_to do |format|
|
||||||
format.html
|
format.html
|
||||||
format.csv do
|
format.csv do
|
||||||
send_data CsvGenerator.generate_csv(@q.result), filename: "account_activities_#{Time.zone.now.to_formatted_s(:number)}.csv"
|
raw_csv = CsvGenerator.generate_csv(@q.result)
|
||||||
|
send_data raw_csv, filename: "account_activities_#{Time.zone.now.to_formatted_s(:number)}.csv"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -20,7 +20,8 @@ class Registrar
|
||||||
respond_to do |format|
|
respond_to do |format|
|
||||||
format.html { @account_activities = @q.result.page(params[:page]) }
|
format.html { @account_activities = @q.result.page(params[:page]) }
|
||||||
format.csv do
|
format.csv do
|
||||||
send_data CsvGenerator.generate_csv(@q.result), filename: "account_activities_#{Time.zone.now.to_formatted_s(:number)}.csv"
|
raw_csv = CsvGenerator.generate_csv(@q.result)
|
||||||
|
send_data raw_csv, filename: "account_activities_#{Time.zone.now.to_formatted_s(:number)}.csv"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -748,7 +748,7 @@ class Domain < ApplicationRecord
|
||||||
statuses,
|
statuses,
|
||||||
contacts.pluck(:code),
|
contacts.pluck(:code),
|
||||||
force_delete_date,
|
force_delete_date,
|
||||||
force_delete_data
|
force_delete_data,
|
||||||
]
|
]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -764,7 +764,10 @@ class Domain < ApplicationRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.csv_header
|
def self.csv_header
|
||||||
['Domain', 'Registrant', 'Valid to', 'Registrar', 'Created at', 'Statuses', 'Contacts code', 'Force delete date', 'Force delete data']
|
[
|
||||||
|
'Domain', 'Registrant', 'Valid to', 'Registrar', 'Created at',
|
||||||
|
'Statuses', 'Contacts code', 'Force delete date', 'Force delete data',
|
||||||
|
]
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.pdf(html)
|
def self.pdf(html)
|
||||||
|
|
|
@ -125,7 +125,7 @@ class Invoice < ApplicationRecord
|
||||||
issue_date,
|
issue_date,
|
||||||
total,
|
total,
|
||||||
currency,
|
currency,
|
||||||
seller_name
|
seller_name,
|
||||||
]
|
]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
class CsvGenerator
|
class CsvGenerator
|
||||||
class << self
|
class << self
|
||||||
def generate_csv(objects)
|
def generate_csv(objects)
|
||||||
@class_name = objects.first.class
|
class_name = objects.first.class
|
||||||
return default_generation(objects) unless custom_csv?
|
return default_generation(objects) unless custom_csv?(class_name)
|
||||||
|
|
||||||
CSV.generate do |csv|
|
CSV.generate do |csv|
|
||||||
csv << @class_name.csv_header
|
csv << class_name.csv_header
|
||||||
objects.each { |object| csv << object.as_csv_row }
|
objects.each { |object| csv << object.as_csv_row }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -14,21 +14,16 @@ class CsvGenerator
|
||||||
|
|
||||||
def default_generation(objects)
|
def default_generation(objects)
|
||||||
CSV.generate do |csv|
|
CSV.generate do |csv|
|
||||||
csv << @class_name.column_names
|
csv << objects.column_names
|
||||||
objects.each { |object| csv << object.attributes.values_at(*@class_name.column_names) }
|
objects.all.find_each { |object| csv << object.attributes.values_at(*objects.column_names) }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def custom_csv?
|
def custom_csv?(class_name)
|
||||||
[
|
[
|
||||||
Version::DomainVersion,
|
Version::DomainVersion, Version::ContactVersion, Domain,
|
||||||
Version::ContactVersion,
|
Contact, Invoice, Account, AccountActivity
|
||||||
Domain,
|
].include?(class_name)
|
||||||
Contact,
|
|
||||||
Invoice,
|
|
||||||
Account,
|
|
||||||
AccountActivity
|
|
||||||
].include?(@class_name)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue