Skip to content

Commit

Permalink
Fix double versioning on Domain & Contact models
Browse files Browse the repository at this point in the history
  • Loading branch information
yulgolem committed Feb 12, 2020
1 parent bf67180 commit a1651ca
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 10 deletions.
8 changes: 7 additions & 1 deletion app/models/concerns/versions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ module Versions

included do
attr_accessor :version_loader
has_paper_trail class_name: "#{model_name}Version"
WITH_CHILDREN = %w[Domain Contact].freeze

if WITH_CHILDREN.include?(model_name.name)
has_paper_trail class_name: "#{model_name}Version", meta: { children: :children_log }
else
has_paper_trail class_name: "#{model_name}Version"
end

# add creator and updator
before_create :add_creator
Expand Down
2 changes: 0 additions & 2 deletions app/models/contact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ class Contact < ApplicationRecord
has_many :registrant_domains, class_name: 'Domain', foreign_key: 'registrant_id'
has_many :actions, dependent: :destroy

has_paper_trail class_name: "ContactVersion", meta: { children: :children_log }

attr_accessor :legal_document_id
alias_attribute :kind, :ident_type
alias_attribute :copy_from_id, :original_id # Old attribute name; for PaperTrail
Expand Down
2 changes: 0 additions & 2 deletions app/models/domain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ class Domain < ApplicationRecord
include Concerns::Domain::RegistryLockable
include Concerns::Domain::Releasable

has_paper_trail class_name: "DomainVersion", meta: { children: :children_log }

attr_accessor :roles

attr_accessor :legal_document_id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def test_can_lock_a_not_locked_domain
end

def test_locking_a_domain_creates_a_version_record
assert_difference '@domain.versions.count', 2 do
assert_difference '@domain.versions.count', 1 do
post '/api/v1/registrant/domains/2df2c1a1-8f6a-490a-81be-8bdf29866880/registry_lock',
headers: @auth_headers
end
Expand Down
2 changes: 1 addition & 1 deletion test/integration/contact/audit_log_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ def test_stores_metadata
contact = contacts(:john)

contact.legal_document_id = 1
assert_difference 'contact.versions.count', 2 do
assert_difference 'contact.versions.count', 1 do
contact.save!
end

Expand Down
2 changes: 1 addition & 1 deletion test/integration/domain/audit_log_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def test_stores_metadata
assert_equal registrant_id, domain.registrant_id
domain.legal_document_id = legal_document_id

assert_difference 'domain.versions.count', 2 do
assert_difference 'domain.versions.count', 1 do
domain.save!
end

Expand Down
19 changes: 19 additions & 0 deletions test/learning/paper_trail_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,25 @@ def test_returns_version_list
assert_respond_to @record.versions.first, :item_id
end

def test_returns_version_count_on_domains
@domain = domains(:airport)
@domain.save

assert_equal 1, @domain.versions.count

@domain.name = 'domain.test'
@domain.save!
assert_equal 2, @domain.versions.count
end

def test_returns_version_count_on_users
@user = users(:registrant)

@user.email = '[email protected]'
@user.save!
assert_equal 1, @user.versions.count
end

def test_creates_new_version_upon_update
@record = Post.create!(title: 'old title')
original_record = @record.clone
Expand Down
4 changes: 2 additions & 2 deletions test/models/domain/domain_version_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def test_assigns_creator_to_paper_trail_whodunnit
duplicate_domain = prepare_duplicate_domain

PaperTrail.whodunnit = @user.id_role_username
assert_difference 'duplicate_domain.versions.count', 2 do
assert_difference 'duplicate_domain.versions.count', 1 do
duplicate_domain.save!
end

Expand All @@ -30,7 +30,7 @@ def test_assigns_creator_to_paper_trail_whodunnit
def test_assigns_updator_to_paper_trail_whodunnit
PaperTrail.whodunnit = @user.id_role_username

assert_difference '@domain.versions.count', 2 do
assert_difference '@domain.versions.count', 1 do
@domain.apply_registry_lock
end

Expand Down

0 comments on commit a1651ca

Please sign in to comment.