Added pure PaperTrail stack

This commit is contained in:
Priit Tark 2015-01-28 17:26:26 +02:00
parent ea6bdc19f9
commit 09a816d5a8
27 changed files with 846 additions and 492 deletions

View file

@ -1,4 +1,6 @@
class Address < ActiveRecord::Base
include Versions # version/address_version.rb
LOCAL_TYPE_SHORT = 'loc'
INTERNATIONAL_TYPE_SHORT = 'int'
LOCAL_TYPE = 'LocalAddress'
@ -10,8 +12,6 @@ class Address < ActiveRecord::Base
belongs_to :contact
belongs_to :country
has_paper_trail class_name: 'AddressVersion'
class << self
# def validate_postal_info_types(parsed_frame)
# errors, used = [], []

View file

@ -1,4 +0,0 @@
class AddressVersion < PaperTrail::Version
self.table_name = :address_versions
self.sequence_name = :address_version_id_seq
end

View file

@ -1,35 +1,36 @@
module DomainVersionObserver
extend ActiveSupport::Concern
included do
after_save :delayed_whois_update
end
# TODO: remove old
# included do
# after_save :delayed_whois_update
# end
private
# private
def delayed_whois_update
name = domain_name
return unless name
body = snapshot
delay.update_private_whois(name, body)
delay.update_public_whois(name, body)
end
# def delayed_whois_update
# name = domain_name
# return unless name
# body = snapshot
# delay.update_private_whois(name, body)
# delay.update_public_whois(name, body)
# end
def update_private_whois(domain_name, body)
wd = Whois::PublicDomain.find_or_initialize_by(name: domain_name)
wd.body = body
wd.save!
end
# def update_private_whois(domain_name, body)
# wd = Whois::PublicDomain.find_or_initialize_by(name: domain_name)
# wd.body = body
# wd.save!
# end
def update_public_whois(domain_name, body)
wd = Whois::PrivateDomain.find_or_initialize_by(name: domain_name)
wd.body = body
wd.save!
end
# def update_public_whois(domain_name, body)
# wd = Whois::PrivateDomain.find_or_initialize_by(name: domain_name)
# wd.body = body
# wd.save!
# end
def domain_name
name = reify.try(:name)
name = load_snapshot[:domain][:name] if event == 'create'
return name if name
end
# def domain_name
# name = reify.try(:name)
# name = load_snapshot[:domain][:name] if event == 'create'
# return name if name
# end
end

View file

@ -0,0 +1,10 @@
module LogTable
extend ActiveSupport::Concern
included do
# one plase to define log tables
log_table_name = "log_#{table_name.sub('_versions', '').tableize}"
self.table_name = log_table_name
self.sequence_name = log_table_name
end
end

View file

@ -1,22 +1,23 @@
module UserEvents
extend ActiveSupport::Concern
module ClassMethods
def registrar_events(id)
registrar = Registrar.find(id)
return [] unless registrar
@events = []
registrar.users.each { |user| @events << user_events(user.id) }
registrar.api_users.each { |user| @events << epp_user_events(user.id) }
@events
end
# TODO: remove old
# module ClassMethods
# def registrar_events(id)
# registrar = Registrar.find(id)
# return [] unless registrar
# @events = []
# registrar.users.each { |user| @events << user_events(user.id) }
# registrar.epp_users.each { |user| @events << epp_user_events(user.id) }
# @events
# end
def user_events(id)
where(whodunnit: id.to_s)
end
# def user_events(id)
# where(whodunnit: id.to_s)
# end
def epp_user_events(id)
where(whodunnit: "#{id}-ApiUser")
end
end
# def epp_user_events(id)
# where(whodunnit: "#{id}-EppUser")
# end
# end
end

View file

@ -0,0 +1,10 @@
module Versions
extend ActiveSupport::Concern
included do
has_paper_trail class_name: "#{model_name}Version"
end
class_methods do
end
end

View file

@ -1,6 +1,5 @@
class Contact < ActiveRecord::Base
# TODO: Foreign contact will get email with activation link/username/temp password
include Versions # version/contact_version.rb
include EppErrors
has_one :address, dependent: :destroy
@ -34,17 +33,16 @@ class Contact < ActiveRecord::Base
delegate :zip, to: :address # , prefix: true
# callbacks
# TODO: remove old
# after_commit :domains_snapshot
after_update :domains_snapshot
after_destroy :domains_snapshot
# after_update :domains_snapshot
# after_destroy :domains_snapshot
before_create :generate_code
before_create :generate_auth_info
after_create :ensure_disclosure
# scopes
scope :current_registrars, ->(id) { where(registrar_id: id) }
# archiving
has_paper_trail class_name: 'ContactVersion'
IDENT_TYPE_ICO = 'ico'
IDENT_TYPES = [
@ -72,13 +70,14 @@ class Contact < ActiveRecord::Base
create_disclosure! unless disclosure
end
def domains_snapshot
(domains + domains_owned).uniq.each do |domain|
next unless domain.is_a?(Domain)
# next if domain.versions.last == domain.create_snapshot
domain.create_version # Method from paper_trail
end
end
# TODO: remove old
# def domains_snapshot
# (domains + domains_owned).uniq.each do |domain|
# next unless domain.is_a?(Domain)
# # next if domain.versions.last == domain.create_snapshot
# domain.create_version # Method from paper_trail
# end
# end
def juridical?
ident_type == IDENT_TYPE_ICO
@ -148,16 +147,17 @@ class Contact < ActiveRecord::Base
name
end
# TODO: remove old
# for archiving
def snapshot
{
name: name,
phone: phone,
code: code,
ident: ident,
email: email
}
end
# def snapshot
# {
# name: name,
# phone: phone,
# code: code,
# ident: ident,
# email: email
# }
# end
class << self
# non-EPP

View file

@ -1,4 +1,5 @@
class Domain < ActiveRecord::Base
include Versions # version/domain_version.rb
# TODO: whois requests ip whitelist for full info for own domains and partial info for other domains
# TODO: most inputs should be trimmed before validatation, probably some global logic?
paginates_per 10 # just for showoff
@ -17,7 +18,9 @@ class Domain < ActiveRecord::Base
-> { where(domain_contacts: { contact_type: DomainContact::ADMIN }) },
through: :domain_contacts, source: :contact
has_many :nameservers, dependent: :delete_all, after_add: :track_nameserver_add
# TODO: remove old
# has_many :nameservers, dependent: :delete_all, after_add: :track_nameserver_add
has_many :nameservers, dependent: :delete_all
accepts_nested_attributes_for :nameservers, allow_destroy: true,
reject_if: proc { |attrs| attrs[:hostname].blank? }
@ -102,9 +105,10 @@ class Domain < ActiveRecord::Base
attr_accessor :owner_contact_typeahead, :update_me
# TODO: remove old
# archiving
# if proc works only on changes on domain sadly
has_paper_trail class_name: 'DomainVersion', meta: { snapshot: :create_snapshot }, if: proc(&:new_version)
# has_paper_trail class_name: 'DomainVersion', meta: { snapshot: :create_snapshot }, if: proc(&:new_version)
def tech_domain_contacts
domain_contacts.select { |x| x.contact_type == DomainContact::TECH }
@ -114,46 +118,51 @@ class Domain < ActiveRecord::Base
domain_contacts.select { |x| x.contact_type == DomainContact::ADMIN }
end
def new_version
return false if versions.try(:last).try(:snapshot) == create_snapshot
true
end
# TODO: remove old
# def new_version
# return false if versions.try(:last).try(:snapshot) == create_snapshot
# true
# end
def create_version
return true unless PaperTrail.enabled?
return true unless valid?
touch_with_version if new_version
end
# TODO: remove old
# def create_version
# return true unless PaperTrail.enabled?
# return true unless valid?
# touch_with_version if new_version
# end
def track_nameserver_add(_nameserver)
return true if versions.count == 0
return true unless valid? && new_version
# TODO: remove old
# def track_nameserver_add(_nameserver)
# return true if versions.count == 0
# return true unless valid? && new_version
touch_with_version
end
# touch_with_version
# end
def create_snapshot
oc = owner_contact.snapshot if owner_contact.is_a?(Contact)
{
owner_contact: oc,
tech_contacts: tech_contacts.map(&:snapshot),
admin_contacts: admin_contacts.map(&:snapshot),
nameservers: nameservers.map(&:snapshot),
domain: make_snapshot
}.to_yaml
end
# TODO: remove old
# def create_snapshot
# oc = owner_contact.snapshot if owner_contact.is_a?(Contact)
# {
# owner_contact: oc,
# tech_contacts: tech_contacts.map(&:snapshot),
# admin_contacts: admin_contacts.map(&:snapshot),
# nameservers: nameservers.map(&:snapshot),
# domain: make_snapshot
# }.to_yaml
# end
def make_snapshot
{
name: name,
status: status,
period: period,
period_unit: period_unit,
registrar_id: registrar.try(:id),
valid_to: valid_to,
valid_from: valid_from
}
end
# TODO: remove old
# def make_snapshot
# {
# name: name,
# status: status,
# period: period,
# period_unit: period_unit,
# registrar_id: registrar.try(:id),
# valid_to: valid_to,
# valid_from: valid_from
# }
# end
def name=(value)
value.strip!

View file

@ -3,8 +3,9 @@ class DomainContact < ActiveRecord::Base
belongs_to :contact
belongs_to :domain
after_create :domain_snapshot
after_destroy :domain_snapshot
# TODO: remove old
# after_create :domain_snapshot
# after_destroy :domain_snapshot
# after_save :domain_snapshot
attr_accessor :value_typeahead
@ -38,10 +39,11 @@ class DomainContact < ActiveRecord::Base
@value_typeahead || contact.try(:name) || nil
end
def domain_snapshot
return true if domain.nil?
return true if domain.versions.count == 0 # avoid snapshot on creation
domain.create_version
true
end
# TODO: remove old
# def domain_snapshot
# return true if domain.nil?
# return true if domain.versions.count == 0 # avoid snapshot on creation
# domain.create_version
# true
# end
end

View file

@ -1,4 +1,5 @@
class DomainStatus < ActiveRecord::Base
include Versions # version/domain_status_version.rb
include EppErrors
belongs_to :domain
@ -50,9 +51,6 @@ class DomainStatus < ActiveRecord::Base
SERVER_ADMIN_CHANGE_PROHIBITED, SERVER_TECH_CHANGE_PROHIBITED
]
# archiving
has_paper_trail class_name: 'DomainStatusVersion'
def epp_code_map
{
'2302' => [ # Object exists

View file

@ -1,4 +0,0 @@
class DomainStatusVersion < PaperTrail::Version
self.table_name = :domain_status_versions
self.sequence_name = :domain_status_version_id_seq
end

View file

@ -1,36 +0,0 @@
class DomainVersion < PaperTrail::Version
include UserEvents
include DomainVersionObserver if Setting.whois_enabled # unless Setting.whois_enabled
scope :deleted, -> { where(event: 'destroy') }
self.table_name = :domain_versions
self.sequence_name = :domain_version_id_seq
def load_snapshot
snapshot ? YAML.load(snapshot) : {}
end
def previous?
return true if previous
false
end
def name
name = reify.try(:name)
name = load_snapshot[:domain].try(:[], :name) unless name
name
end
def changed_elements
return [] unless previous?
@changes = []
@previous_snap = previous.load_snapshot
@snap = load_snapshot
[:owner_contact, :tech_contacts, :admin_contacts, :nameservers, :domain].each do |key|
@changes << key unless @snap[key] == @previous_snap[key]
end
@changes
end
end

View file

@ -1,4 +1,5 @@
class Nameserver < ActiveRecord::Base
include Versions # version/nameserver_version.rb
include EppErrors
belongs_to :registrar
@ -10,9 +11,8 @@ class Nameserver < ActiveRecord::Base
validates :ipv6, format: { with: /(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]).){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]).){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))/, allow_blank: true }
# rubocop: enable Metrics/LineLength
# archiving
has_paper_trail class_name: 'NameserverVersion'
after_destroy :domain_version
# TODO: remove old
# after_destroy :domain_version
before_validation :normalize_attributes
@ -34,13 +34,14 @@ class Nameserver < ActiveRecord::Base
}
end
def snapshot
{
hostname: hostname,
ipv4: ipv4,
ipv6: ipv6
}
end
# TODO: remove old
# def snapshot
# {
# hostname: hostname,
# ipv4: ipv4,
# ipv6: ipv6
# }
# end
def normalize_attributes
self.hostname = hostname.try(:strip).try(:downcase)
@ -48,9 +49,10 @@ class Nameserver < ActiveRecord::Base
self.ipv6 = ipv6.try(:strip).try(:upcase)
end
def domain_version
domain.create_version if domain
end
# TODO: remove old
# def domain_version
# domain.create_version if domain
# end
def to_s
hostname

View file

@ -1,4 +0,0 @@
class NameserverVersion < PaperTrail::Version
self.table_name = :nameserver_versions
self.sequence_name = :nameserver_version_id_seq
end

View file

@ -0,0 +1,3 @@
class AddressVersion < PaperTrail::Version
include LogTable
end

View file

@ -1,8 +1,8 @@
class ContactVersion < PaperTrail::Version
include LogTable
include UserEvents
# self.table_name = :post_versions
# self.sequence_name = :post_version_id_seq
scope :deleted, -> { where(event: 'destroy') }
self.table_name = :contact_versions
self.sequence_name = :contact_version_id_seq
end

View file

@ -0,0 +1,3 @@
class DomainStatusVersion < PaperTrail::Version
include LogTable
end

View file

@ -0,0 +1,39 @@
class DomainVersion < PaperTrail::Version
include LogTable
include UserEvents
# TODO: remove old
# include DomainVersionObserver if Setting.whois_enabled # unless Setting.whois_enabled
scope :deleted, -> { where(event: 'destroy') }
# TODO: remove old
# def load_snapshot
# snapshot ? YAML.load(snapshot) : {}
# end
# TODO: remove old
# def previous?
# return true if previous
# false
# end
# TODO: remove old
# def name
# name = reify.try(:name)
# name = load_snapshot[:domain].try(:[], :name) unless name
# name
# end
# TODO: remove old
# def changed_elements
# return [] unless previous?
# @changes = []
# @previous_snap = previous.load_snapshot
# @snap = load_snapshot
# [:owner_contact, :tech_contacts, :admin_contacts, :nameservers, :domain].each do |key|
# @changes << key unless @snap[key] == @previous_snap[key]
# end
# @changes
# end
end

View file

@ -0,0 +1,3 @@
class NameserverVersion < PaperTrail::Version
include LogTable
end