mirror of
https://github.com/internetee/registry.git
synced 2025-07-28 13:36:15 +02:00
Migrate ToCsv
module to CsvGenerator
service
This commit is contained in:
parent
daafa756aa
commit
e50501f13f
16 changed files with 34 additions and 60 deletions
|
@ -35,7 +35,7 @@ module Admin
|
||||||
respond_to do |format|
|
respond_to do |format|
|
||||||
format.html
|
format.html
|
||||||
format.csv do
|
format.csv do
|
||||||
send_data @q.result.to_csv, filename: "account_activities_#{Time.zone.now.to_formatted_s(:number)}.csv"
|
send_data CsvGenerator.generate_csv(@q.result), filename: "account_activities_#{Time.zone.now.to_formatted_s(:number)}.csv"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -20,7 +20,7 @@ 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 @q.result.to_csv, filename: "account_activities_#{Time.zone.now.to_formatted_s(:number)}.csv"
|
send_data CsvGenerator.generate_csv(@q.result), filename: "account_activities_#{Time.zone.now.to_formatted_s(:number)}.csv"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -40,7 +40,7 @@ class Registrar
|
||||||
@contacts = @contacts.per(contacts_per_page) if contacts_per_page.positive?
|
@contacts = @contacts.per(contacts_per_page) if contacts_per_page.positive?
|
||||||
end
|
end
|
||||||
format.csv do
|
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"
|
send_data raw_csv, filename: 'contacts.csv', type: "#{Mime[:csv]}; charset=utf-8"
|
||||||
end
|
end
|
||||||
format.pdf do
|
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
|
class Account < ApplicationRecord
|
||||||
extend ToCsv
|
|
||||||
include Versions
|
include Versions
|
||||||
|
|
||||||
belongs_to :registrar, required: true
|
belongs_to :registrar, required: true
|
||||||
|
|
|
@ -1,5 +1,3 @@
|
||||||
module ApiLog
|
module ApiLog
|
||||||
class EppLog < Db
|
class EppLog < Db; end
|
||||||
extend ToCsv
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,5 +1,3 @@
|
||||||
module ApiLog
|
module ApiLog
|
||||||
class ReppLog < Db
|
class ReppLog < Db; end
|
||||||
extend ToCsv
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
class BlockedDomain < ApplicationRecord
|
class BlockedDomain < ApplicationRecord
|
||||||
include Versions
|
include Versions
|
||||||
extend ToCsv
|
|
||||||
before_save :generate_data
|
before_save :generate_data
|
||||||
after_destroy :remove_data
|
after_destroy :remove_data
|
||||||
|
|
||||||
|
|
|
@ -188,15 +188,6 @@ class Contact < ApplicationRecord
|
||||||
]
|
]
|
||||||
end
|
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)
|
def pdf(html)
|
||||||
kit = PDFKit.new(html)
|
kit = PDFKit.new(html)
|
||||||
kit.to_pdf
|
kit.to_pdf
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
class Dispute < ApplicationRecord
|
class Dispute < ApplicationRecord
|
||||||
extend ToCsv
|
|
||||||
include WhoisStatusPopulate
|
include WhoisStatusPopulate
|
||||||
validates :domain_name, :password, :starts_at, :expires_at, presence: true
|
validates :domain_name, :password, :starts_at, :expires_at, presence: true
|
||||||
before_validation :fill_empty_passwords, :set_expiry_date
|
before_validation :fill_empty_passwords, :set_expiry_date
|
||||||
|
|
|
@ -289,21 +289,6 @@ class Domain < ApplicationRecord
|
||||||
)
|
)
|
||||||
end
|
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
|
private
|
||||||
|
|
||||||
def registrant_user_domains_by_registrant(registrant_user)
|
def registrant_user_domains_by_registrant(registrant_user)
|
||||||
|
|
|
@ -3,7 +3,6 @@ class Invoice < ApplicationRecord
|
||||||
include Invoice::Cancellable
|
include Invoice::Cancellable
|
||||||
include Invoice::Payable
|
include Invoice::Payable
|
||||||
include Invoice::BookKeeping
|
include Invoice::BookKeeping
|
||||||
extend ToCsv
|
|
||||||
|
|
||||||
belongs_to :buyer, class_name: 'Registrar'
|
belongs_to :buyer, class_name: 'Registrar'
|
||||||
has_one :account_activity
|
has_one :account_activity
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
class ReservedDomain < ApplicationRecord
|
class ReservedDomain < ApplicationRecord
|
||||||
extend ToCsv
|
|
||||||
include Versions # version/reserved_domain_version.rb
|
include Versions # version/reserved_domain_version.rb
|
||||||
include WhoisStatusPopulate
|
include WhoisStatusPopulate
|
||||||
before_save :fill_empty_passwords
|
before_save :fill_empty_passwords
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
class Version::ContactVersion < PaperTrail::Version
|
class Version::ContactVersion < PaperTrail::Version
|
||||||
extend ToCsv
|
|
||||||
include VersionSession
|
include VersionSession
|
||||||
|
|
||||||
self.table_name = :log_contacts
|
self.table_name = :log_contacts
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
class Version::DomainVersion < PaperTrail::Version
|
class Version::DomainVersion < PaperTrail::Version
|
||||||
extend ToCsv
|
|
||||||
include VersionSession
|
include VersionSession
|
||||||
|
|
||||||
self.table_name = :log_domains
|
self.table_name = :log_domains
|
||||||
|
|
|
@ -1,15 +1,34 @@
|
||||||
class CsvGenerator
|
class CsvGenerator
|
||||||
def self.generate_csv(objects)
|
class << self
|
||||||
class_name = objects.first.class
|
def generate_csv(objects)
|
||||||
return objects.to_csv unless custom_csv?(class_name)
|
@class_name = objects.first.class
|
||||||
|
return default_generation(objects) unless custom_csv?
|
||||||
|
|
||||||
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
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def default_generation(objects)
|
||||||
|
CSV.generate do |csv|
|
||||||
|
csv << @class_name.column_names
|
||||||
|
objects.each { |object| csv << object.attributes.values_at(*@class_name.column_names) }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def custom_csv?
|
||||||
|
[
|
||||||
|
Version::DomainVersion,
|
||||||
|
Version::ContactVersion,
|
||||||
|
Domain,
|
||||||
|
Contact,
|
||||||
|
Invoice,
|
||||||
|
Account,
|
||||||
|
AccountActivity
|
||||||
|
].include?(@class_name)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.custom_csv?(class_name)
|
|
||||||
[Version::DomainVersion, Version::ContactVersion, Domain, Contact, Invoice, Account, AccountActivity].include?(class_name)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue