1
0
Fork 0

Add interactor MergeAccountPerson

This commit is contained in:
Alex Kotov 2019-08-14 19:47:55 +05:00
parent 8a416a15f0
commit fb42db9fd1
Signed by: kotovalexarian
GPG Key ID: 553C0EBBEB5D5F08
4 changed files with 93 additions and 2 deletions

View File

@ -0,0 +1,19 @@
# frozen_string_literal: true
class MergeAccountPerson
include Interactor::Organizer
organize MergeContactLists, SetAccountPerson, DestroyContactList
around do |interactor|
ActiveRecord::Base.transaction do
interactor.call
end
end
before do
context.from = context.account.contact_list
context.to = context.person.contact_list
context.destroyable_contact_list = context.account.contact_list
end
end

View File

@ -4,7 +4,11 @@ class SetAccountPerson
include Interactor
def call
old_contact_list = context.account.contact_list
context.account.update! person: context.person,
contact_list: context.new_contact_list
contact_list: context.person.contact_list
old_contact_list.reload
end
end

View File

@ -3,6 +3,6 @@
FactoryBot.define do
factory :contact_network, class: ContactNetwork do
nickname { Faker::Internet.username 3..36, %w[_] }
public_name { Faker::Company.name }
public_name { Faker::Company.unique.name }
end
end

View File

@ -0,0 +1,68 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe MergeAccountPerson do
subject { described_class.call account: account, person: person }
let(:account) { create :usual_account, contact_list: old_contact_list }
let(:person) { create :initial_person, contact_list: new_contact_list }
let(:old_contact_list) { create :some_contact_list }
let(:new_contact_list) { create :some_contact_list }
specify do
expect { subject }.to \
change(account, :person)
.from(nil)
.to(person)
end
specify do
expect { subject }.to \
change(person, :account)
.from(nil)
.to(account)
end
specify do
expect { subject }.to \
change(account, :contact_list)
.from(old_contact_list)
.to(new_contact_list)
end
specify do
expect { subject }.not_to \
change(person, :contact_list)
.from(new_contact_list)
end
specify do
expect { subject }.to \
change(old_contact_list.contacts, :count)
.from(old_contact_list.contacts.count)
.to(0)
end
specify do
expect { subject }.to \
change(new_contact_list.contacts, :count)
.from(new_contact_list.contacts.count)
.to(old_contact_list.contacts.count + new_contact_list.contacts.count)
end
specify do
expect { subject }.to \
change(old_contact_list.contacts, :to_a)
.from(old_contact_list.contacts.to_a)
.to([])
end
specify do
expect { subject }.to \
change(new_contact_list.contacts, :to_a)
.from(new_contact_list.contacts.to_a)
.to(new_contact_list.contacts.to_a + old_contact_list.contacts.to_a)
end
end