mirror of
https://github.com/internetee/registry.git
synced 2025-08-11 03:59:33 +02:00
Refactor account activity csv generation
This commit is contained in:
parent
fd41d77390
commit
daafa756aa
2 changed files with 10 additions and 17 deletions
|
@ -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,7 +1,7 @@
|
||||||
class CsvGenerator
|
class CsvGenerator
|
||||||
def self.generate_csv(objects)
|
def self.generate_csv(objects)
|
||||||
class_name = objects.first.class
|
class_name = objects.first.class
|
||||||
return objects.to_csv unless custom_csv(class_name)
|
return objects.to_csv unless custom_csv?(class_name)
|
||||||
|
|
||||||
CSV.generate do |csv|
|
CSV.generate do |csv|
|
||||||
csv << class_name.csv_header
|
csv << class_name.csv_header
|
||||||
|
@ -9,9 +9,7 @@ class CsvGenerator
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
def self.custom_csv?(class_name)
|
||||||
|
[Version::DomainVersion, Version::ContactVersion, Domain, Contact, Invoice, Account, AccountActivity].include?(class_name)
|
||||||
def self.custom_csv(class_name)
|
|
||||||
[Version::DomainVersion, Version::ContactVersion, Domain, Contact, Invoice, Account].include?(class_name)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue