mirror of
https://github.com/internetee/registry.git
synced 2025-07-28 05:26:17 +02:00
Fix domain history csv
This commit is contained in:
parent
b18ad62834
commit
a9b942dac0
2 changed files with 50 additions and 0 deletions
|
@ -88,5 +88,17 @@ module Admin
|
||||||
|
|
||||||
params_copy
|
params_copy
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def render_by_format(page, filename)
|
||||||
|
respond_to do |format|
|
||||||
|
format.html { render page }
|
||||||
|
format.csv do
|
||||||
|
raw_csv = csv_generate
|
||||||
|
send_data raw_csv,
|
||||||
|
filename: "#{filename}_#{Time.zone.now.to_formatted_s(:number)}.csv",
|
||||||
|
type: "#{Mime[:csv]}; charset=utf-8"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
module ObjectVersionsHelper
|
module ObjectVersionsHelper
|
||||||
|
CSV_HEADER = ['Name', 'Registrant', 'Registrar', 'Action', 'Created at'].freeze
|
||||||
|
|
||||||
def attach_existing_fields(version, new_object)
|
def attach_existing_fields(version, new_object)
|
||||||
version.object_changes.to_h.each do |key, value|
|
version.object_changes.to_h.each do |key, value|
|
||||||
method_name = "#{key}=".to_sym
|
method_name = "#{key}=".to_sym
|
||||||
|
@ -11,9 +13,45 @@ module ObjectVersionsHelper
|
||||||
version.object.to_h.select { |key, _value| field_names.include?(key) }
|
version.object.to_h.select { |key, _value| field_names.include?(key) }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def csv_generate
|
||||||
|
CSV.generate do |csv|
|
||||||
|
csv << CSV_HEADER
|
||||||
|
@versions.each do |version|
|
||||||
|
attributes = only_present_fields(version, Domain)
|
||||||
|
domain = Domain.new(attributes)
|
||||||
|
attach_existing_fields(version, domain) unless version.event == 'destroy'
|
||||||
|
|
||||||
|
csv << create_row(domain, version)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def event_value(version, val)
|
def event_value(version, val)
|
||||||
version.event == 'destroy' ? val.first : val.last
|
version.event == 'destroy' ? val.first : val.last
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def registrant_name(domain, version)
|
||||||
|
if domain.registrant
|
||||||
|
domain.registrant.name
|
||||||
|
else
|
||||||
|
contact = Contact.all_versions_for([domain.registrant_id], version.created_at).first
|
||||||
|
if contact.nil? && ver = Version::ContactVersion.where(item_id: domain.registrant_id).last
|
||||||
|
merged_obj = ver.object_changes.to_h.each_with_object({}) {|(k,v), o| o[k] = v.last }
|
||||||
|
result = ver.object.to_h.merge(merged_obj)&.slice(*Contact&.column_names)
|
||||||
|
contact = Contact.new(result)
|
||||||
|
end
|
||||||
|
contact.try(:name) || 'Deleted'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def create_row(domain, version)
|
||||||
|
name = domain.name
|
||||||
|
registrant = registrant_name(domain, version)
|
||||||
|
registrar = domain.registrar
|
||||||
|
event = version.event
|
||||||
|
created_at = version.created_at.to_formatted_s(:db)
|
||||||
|
[name, registrant, registrar, event, created_at]
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue