1
0
Fork 0

Add interactor MergeContactLists

This commit is contained in:
Alex Kotov 2019-08-14 18:10:09 +05:00
parent 1f9cd376b0
commit c228de9be7
Signed by: kotovalexarian
GPG Key ID: 553C0EBBEB5D5F08
5 changed files with 78 additions and 0 deletions

View File

@ -0,0 +1,18 @@
# frozen_string_literal: true
class MergeContactLists
include Interactor
after do
context.from.contacts.reload
context.to.contacts.reload
end
def call
ActiveRecord::Base.transaction do
context.from.contacts.each do |contact|
contact.update! contact_list: context.to
end
end
end
end

View File

@ -8,4 +8,6 @@ class ContactList < ApplicationRecord
has_one :account
has_one :person
has_many :contacts, dependent: :restrict_with_exception
end

View File

@ -2,4 +2,16 @@
FactoryBot.define do
factory :empty_contact_list, class: ContactList
factory :some_contact_list, parent: :empty_contact_list do
transient do
contacts_count { rand 1..5 }
end
after :create do |contact_list, evaluator|
create_list :some_contact,
evaluator.contacts_count,
contact_list: contact_list
end
end
end

View File

@ -0,0 +1,38 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe MergeContactLists do
subject { described_class.call from: from, to: to }
let(:from) { create :some_contact_list }
let(:to) { create :some_contact_list }
specify do
expect { subject }.to \
change(from.contacts, :count)
.from(from.contacts.count)
.to(0)
end
specify do
expect { subject }.to \
change(to.contacts, :count)
.from(to.contacts.count)
.to(from.contacts.count + to.contacts.count)
end
specify do
expect { subject }.to \
change(from.contacts, :to_a)
.from(from.contacts.to_a)
.to([])
end
specify do
expect { subject }.to \
change(to.contacts, :to_a)
.from(to.contacts.to_a)
.to(to.contacts.to_a + from.contacts.to_a)
end
end

View File

@ -20,4 +20,12 @@ RSpec.describe ContactList do
.dependent(:restrict_with_exception)
end
end
describe '#contacts' do
it do
is_expected.to \
have_many(:contacts)
.dependent(:restrict_with_exception)
end
end
end