Basic archiving overhaul

This commit is contained in:
Andres Keskküla 2014-10-15 10:35:56 +03:00
parent 19846a3abf
commit 3b1e21d6eb
12 changed files with 260 additions and 30 deletions

View file

@ -5,8 +5,6 @@ class Contact < ActiveRecord::Base
include EppErrors
#has_one :local_address, dependent: :destroy
#has_one :international_address, dependent: :destroy
has_one :address, dependent: :destroy
has_one :disclosure, class_name: 'ContactDisclosure'
@ -35,6 +33,11 @@ class Contact < ActiveRecord::Base
delegate :street, to: :address#, prefix: true
delegate :zip, to: :address#, prefix: true
# callbacks
#after_commit :domains_snapshot
after_update :domains_snapshot
after_destroy :domains_snapshot
#scopes
scope :current_registrars, ->(id) { where(registrar_id: id) }
# archiving
@ -60,6 +63,13 @@ class Contact < ActiveRecord::Base
errors.add(:ident, 'bad format') unless code.valid?
end
def domains_snapshot
(domains + domains_owned).uniq.each do |domain|
next unless domain.is_a?(Domain)
domain.touch_with_version # Method from paper_trail
end
end
def juridical?
ident_type == IDENT_TYPE_ICO
end
@ -124,6 +134,17 @@ class Contact < ActiveRecord::Base
name
end
# for archiving
def snapshot
{
name: name,
phone: phone,
code: code,
ident: ident,
email: email
}
end
class << self
# non-EPP

View file

@ -57,10 +57,31 @@ class Domain < ActiveRecord::Base
validate :validate_dnskeys_uniqueness
validate :validate_nameserver_ips
attr_accessor :owner_contact_typeahead
attr_accessor :owner_contact_typeahead, :update_me
# archiving
has_paper_trail class_name: 'DomainVersion'
has_paper_trail class_name: 'DomainVersion', meta: { snapshot: :create_snapshot }
# TODO Add touch_with_version hook to the DomainContact as well to track add/delete(?) of contact
# Not sure what hook to use since Contact.destroy fires update from here so possibly after_create
def create_snapshot
oc = owner_contact.snapshot if owner_contact.is_a?(Contact)
{
owner_contact: oc,
tech_contacts: tech_contacts.map{ |tc| tc.snapshot },
admin_contacts: admin_contacts.map{ |ac| ac.snapshot },
nameservers: nameservers.map{ |ns| ns.snapshot },
domain: make_snapshot
}.to_yaml
end
def make_snapshot
{
name: name,
status: status
}
end
def name=(value)
value.strip!
@ -254,4 +275,28 @@ class Domain < ActiveRecord::Base
return period.to_i.years if unit == 'y'
end
end
private
#for archiving
#def version_owner
# return nil unless owner_contact
# owner_contact.id
#end
#def version_admin_contacts
# return nil unless admin_contacts
# return admin_contacts.map { |ns| ns.id }
#end
#def version_tech_contacts
# return nil unless tech_contacts
# return tech_contacts.map { |ns| ns.id }
#end
#def version_nameservers
# return nil unless nameservers
# return nameservers.map { |ns| ns.id }
#end
end

View file

@ -5,4 +5,8 @@ class DomainVersion < PaperTrail::Version
self.table_name = :domain_versions
self.sequence_name = :domain_version_id_seq
def load_snapshot
YAML.load(snapshot)
end
end

View file

@ -27,6 +27,14 @@ class Nameserver < ActiveRecord::Base
}
end
def snapshot
{
hostname: hostname,
ipv4: ipv4,
ipv6: ipv6
}
end
def to_s
hostname
end