mirror of
https://github.com/internetee/registry.git
synced 2025-07-28 13:36:15 +02:00
Merge pull request #2330 from internetee/2321-refactor-csv-module
2321 refactor csv module
This commit is contained in:
commit
2256b3827f
17 changed files with 44 additions and 77 deletions
|
@ -35,7 +35,8 @@ module Admin
|
|||
respond_to do |format|
|
||||
format.html
|
||||
format.csv do
|
||||
send_data @q.result.to_csv, 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
|
||||
|
||||
|
|
|
@ -20,7 +20,8 @@ class Registrar
|
|||
respond_to do |format|
|
||||
format.html { @account_activities = @q.result.page(params[:page]) }
|
||||
format.csv do
|
||||
send_data @q.result.to_csv, 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
|
||||
|
||||
|
|
|
@ -40,7 +40,7 @@ class Registrar
|
|||
@contacts = @contacts.per(contacts_per_page) if contacts_per_page.positive?
|
||||
end
|
||||
format.csv do
|
||||
raw_csv = contacts.to_csv
|
||||
raw_csv = CsvGenerator.generate_csv(contacts)
|
||||
send_data raw_csv, filename: 'contacts.csv', type: "#{Mime[:csv]}; charset=utf-8"
|
||||
end
|
||||
format.pdf do
|
||||
|
|
|
@ -1,10 +0,0 @@
|
|||
module ToCsv
|
||||
def to_csv
|
||||
CSV.generate do |csv|
|
||||
csv << column_names
|
||||
all.find_each do |item|
|
||||
csv << item.attributes.values_at(*column_names)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,5 +1,4 @@
|
|||
class Account < ApplicationRecord
|
||||
extend ToCsv
|
||||
include Versions
|
||||
|
||||
belongs_to :registrar, required: true
|
||||
|
|
|
@ -11,6 +11,7 @@ class AccountActivity < ApplicationRecord
|
|||
UPDATE_CREDIT = 'update_credit'.freeze
|
||||
|
||||
after_create :update_balance
|
||||
|
||||
def update_balance
|
||||
account.balance += sum
|
||||
account.save
|
||||
|
@ -19,23 +20,17 @@ class AccountActivity < ApplicationRecord
|
|||
save
|
||||
end
|
||||
|
||||
def as_csv_row
|
||||
[account.registrar.try(:code), description, I18n.t(activity_type), I18n.l(created_at), sum]
|
||||
end
|
||||
|
||||
class << self
|
||||
def types_for_select
|
||||
[CREATE, RENEW, ADD_CREDIT, UPDATE_CREDIT].map { |x| [I18n.t(x), x] }
|
||||
end
|
||||
|
||||
def to_csv
|
||||
attributes = %w(description activity_type created_at sum)
|
||||
|
||||
CSV.generate(headers: true) do |csv|
|
||||
csv << %w(registrar description activity_type receipt_date sum)
|
||||
|
||||
all.each do |x|
|
||||
attrs = [x.account.registrar.try(:code)]
|
||||
attrs += attributes.map { |attr| x.send(attr) }
|
||||
csv << attrs
|
||||
end
|
||||
end
|
||||
def csv_header
|
||||
['Registrar', 'Description', 'Activity Type', 'Receipt Date', 'Sum']
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,5 +1,3 @@
|
|||
module ApiLog
|
||||
class EppLog < Db
|
||||
extend ToCsv
|
||||
end
|
||||
class EppLog < Db; end
|
||||
end
|
||||
|
|
|
@ -1,5 +1,3 @@
|
|||
module ApiLog
|
||||
class ReppLog < Db
|
||||
extend ToCsv
|
||||
end
|
||||
class ReppLog < Db; end
|
||||
end
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
class BlockedDomain < ApplicationRecord
|
||||
include Versions
|
||||
extend ToCsv
|
||||
before_save :generate_data
|
||||
after_destroy :remove_data
|
||||
|
||||
|
|
|
@ -188,15 +188,6 @@ class Contact < ApplicationRecord
|
|||
]
|
||||
end
|
||||
|
||||
def to_csv
|
||||
CSV.generate do |csv|
|
||||
csv << column_names
|
||||
all.each do |contact|
|
||||
csv << contact.attributes.values_at(*column_names)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def pdf(html)
|
||||
kit = PDFKit.new(html)
|
||||
kit.to_pdf
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
class Dispute < ApplicationRecord
|
||||
extend ToCsv
|
||||
include WhoisStatusPopulate
|
||||
validates :domain_name, :password, :starts_at, :expires_at, presence: true
|
||||
before_validation :fill_empty_passwords, :set_expiry_date
|
||||
|
|
|
@ -289,21 +289,6 @@ class Domain < ApplicationRecord
|
|||
)
|
||||
end
|
||||
|
||||
def to_csv
|
||||
CSV.generate do |csv|
|
||||
headers = column_names.dup
|
||||
swap_elements(headers, [[0, 1], [1, 5]])
|
||||
headers[0] = 'Domain'
|
||||
headers[1] = headers[1].humanize
|
||||
csv << headers
|
||||
all.find_each do |item|
|
||||
row = item.attributes.values_at(*column_names)
|
||||
swap_elements(row, [[0, 1], [1, 5]])
|
||||
csv << row
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def registrant_user_domains_by_registrant(registrant_user)
|
||||
|
@ -763,7 +748,7 @@ class Domain < ApplicationRecord
|
|||
statuses,
|
||||
contacts.pluck(:code),
|
||||
force_delete_date,
|
||||
force_delete_data
|
||||
force_delete_data,
|
||||
]
|
||||
end
|
||||
|
||||
|
@ -779,7 +764,10 @@ class Domain < ApplicationRecord
|
|||
end
|
||||
|
||||
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
|
||||
|
||||
def self.pdf(html)
|
||||
|
|
|
@ -3,7 +3,6 @@ class Invoice < ApplicationRecord
|
|||
include Invoice::Cancellable
|
||||
include Invoice::Payable
|
||||
include Invoice::BookKeeping
|
||||
extend ToCsv
|
||||
|
||||
belongs_to :buyer, class_name: 'Registrar'
|
||||
has_one :account_activity
|
||||
|
@ -126,7 +125,7 @@ class Invoice < ApplicationRecord
|
|||
issue_date,
|
||||
total,
|
||||
currency,
|
||||
seller_name
|
||||
seller_name,
|
||||
]
|
||||
end
|
||||
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
class ReservedDomain < ApplicationRecord
|
||||
extend ToCsv
|
||||
include Versions # version/reserved_domain_version.rb
|
||||
include WhoisStatusPopulate
|
||||
before_save :fill_empty_passwords
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
class Version::ContactVersion < PaperTrail::Version
|
||||
extend ToCsv
|
||||
include VersionSession
|
||||
|
||||
self.table_name = :log_contacts
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
class Version::DomainVersion < PaperTrail::Version
|
||||
extend ToCsv
|
||||
include VersionSession
|
||||
|
||||
self.table_name = :log_domains
|
||||
|
|
|
@ -1,17 +1,29 @@
|
|||
class CsvGenerator
|
||||
def self.generate_csv(objects)
|
||||
class_name = objects.first.class
|
||||
return objects.to_csv unless custom_csv(class_name)
|
||||
class << self
|
||||
def generate_csv(objects)
|
||||
class_name = objects.first.class
|
||||
return default_generation(objects) unless custom_csv?(class_name)
|
||||
|
||||
CSV.generate do |csv|
|
||||
csv << class_name.csv_header
|
||||
objects.each { |object| csv << object.as_csv_row }
|
||||
CSV.generate do |csv|
|
||||
csv << class_name.csv_header
|
||||
objects.each { |object| csv << object.as_csv_row }
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def default_generation(objects)
|
||||
CSV.generate do |csv|
|
||||
csv << objects.column_names
|
||||
objects.all.find_each { |object| csv << object.attributes.values_at(*objects.column_names) }
|
||||
end
|
||||
end
|
||||
|
||||
def custom_csv?(class_name)
|
||||
[
|
||||
Version::DomainVersion, Version::ContactVersion, Domain,
|
||||
Contact, Invoice, Account, AccountActivity
|
||||
].include?(class_name)
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def self.custom_csv(class_name)
|
||||
[Version::DomainVersion, Version::ContactVersion, Domain, Contact, Invoice, Account].include?(class_name)
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue