mirror of
https://github.com/internetee/registry.git
synced 2025-08-04 17:01:44 +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
|
||||
|
||||
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,7 +1,7 @@
|
|||
class CsvGenerator
|
||||
def self.generate_csv(objects)
|
||||
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 << class_name.csv_header
|
||||
|
@ -9,9 +9,7 @@ class CsvGenerator
|
|||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def self.custom_csv(class_name)
|
||||
[Version::DomainVersion, Version::ContactVersion, Domain, Contact, Invoice, Account].include?(class_name)
|
||||
def self.custom_csv?(class_name)
|
||||
[Version::DomainVersion, Version::ContactVersion, Domain, Contact, Invoice, Account, AccountActivity].include?(class_name)
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue