mirror of
https://github.com/internetee/registry.git
synced 2025-07-28 21:46:24 +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|
|
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"
|
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 @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
|
||||||
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
|
||||||
|
|
|
@ -11,6 +11,7 @@ class AccountActivity < ApplicationRecord
|
||||||
UPDATE_CREDIT = 'update_credit'.freeze
|
UPDATE_CREDIT = 'update_credit'.freeze
|
||||||
|
|
||||||
after_create :update_balance
|
after_create :update_balance
|
||||||
|
|
||||||
def update_balance
|
def update_balance
|
||||||
account.balance += sum
|
account.balance += sum
|
||||||
account.save
|
account.save
|
||||||
|
@ -19,23 +20,17 @@ class AccountActivity < ApplicationRecord
|
||||||
save
|
save
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def as_csv_row
|
||||||
|
[account.registrar.try(:code), description, I18n.t(activity_type), I18n.l(created_at), sum]
|
||||||
|
end
|
||||||
|
|
||||||
class << self
|
class << self
|
||||||
def types_for_select
|
def types_for_select
|
||||||
[CREATE, RENEW, ADD_CREDIT, UPDATE_CREDIT].map { |x| [I18n.t(x), x] }
|
[CREATE, RENEW, ADD_CREDIT, UPDATE_CREDIT].map { |x| [I18n.t(x), x] }
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_csv
|
def csv_header
|
||||||
attributes = %w(description activity_type created_at sum)
|
['Registrar', 'Description', 'Activity Type', 'Receipt Date', '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
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -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)
|
||||||
|
@ -763,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
|
||||||
|
|
||||||
|
@ -779,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)
|
||||||
|
|
|
@ -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
|
||||||
|
@ -126,7 +125,7 @@ class Invoice < ApplicationRecord
|
||||||
issue_date,
|
issue_date,
|
||||||
total,
|
total,
|
||||||
currency,
|
currency,
|
||||||
seller_name
|
seller_name,
|
||||||
]
|
]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -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,17 +1,29 @@
|
||||||
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?(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
|
||||||
|
|
||||||
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
def self.custom_csv(class_name)
|
|
||||||
[Version::DomainVersion, Version::ContactVersion, Domain, Contact, Invoice, Account].include?(class_name)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue