Move interactor from Interactor gem to ActiveInteraction

This commit is contained in:
Alex Sherman 2020-11-11 15:44:00 +05:00
parent 3c7fa88463
commit bce39e3404
17 changed files with 70 additions and 65 deletions

View file

@ -1,9 +1,8 @@
source 'https://rubygems.org'
# core
gem 'active_interaction', '~> 3.8'
gem 'bootsnap', '>= 1.1.0', require: false
gem 'interactor', '~> 3.0'
gem 'interactor-rails', '~> 2.0'
gem 'iso8601', '0.12.1' # for dates and times
gem 'rails', '~> 6.0'
gem 'rest-client'

View file

@ -112,6 +112,8 @@ GEM
erubi (~> 1.4)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.1, >= 1.2.0)
active_interaction (3.8.3)
activemodel (>= 4, < 7)
activejob (6.0.3.3)
activesupport (= 6.0.3.3)
globalid (>= 0.3.6)
@ -251,10 +253,6 @@ GEM
i18n (1.8.5)
concurrent-ruby (~> 1.0)
i18n_data (0.10.0)
interactor (3.1.2)
interactor-rails (2.2.1)
interactor (~> 3.0)
rails (>= 4.2)
isikukood (0.1.2)
iso8601 (0.12.1)
jquery-rails (4.4.0)
@ -525,6 +523,7 @@ PLATFORMS
ruby
DEPENDENCIES
active_interaction (~> 3.8)
activerecord-import
airbrake
bootsnap (>= 1.1.0)
@ -548,8 +547,6 @@ DEPENDENCIES
figaro (= 1.1.1)
grape
haml (~> 5.0)
interactor (~> 3.0)
interactor-rails (~> 2.0)
isikukood
iso8601 (= 0.12.1)
jquery-rails

View file

@ -4,11 +4,15 @@ module Admin
def create
authorize! :manage, domain
notice = t('.scheduled')
domain.transaction do
domain.schedule_force_delete(type: force_delete_type, notify_by_email: notify_by_email?)
result = domain.schedule_force_delete(type: force_delete_type,
notify_by_email: notify_by_email?)
notice = result.errors.messages[:domain].first unless result.valid?
end
redirect_to edit_admin_domain_url(domain), notice: t('.scheduled')
redirect_to edit_admin_domain_url(domain), notice: notice
end
def destroy

View file

@ -0,0 +1,17 @@
class Domain
module ForceDeleteInteraction
class Base < ActiveInteraction::Base
object :domain,
class: Domain,
description: 'Domain to set ForceDelete on'
symbol :type,
default: :fast_track,
description: 'Force delete type, might be :fast_track or :soft'
boolean :notify_by_email,
default: false,
description: 'Do we need to send email notification'
validates :type, inclusion: { in: %i[fast_track soft] }
end
end
end

View file

@ -1,11 +1,11 @@
class Domain
module ForceDeleteInteractor
module ForceDeleteInteraction
class CheckDiscarded < Base
def call
return unless domain.discarded?
def execute
return true unless domain.discarded?
message = 'Force delete procedure cannot be scheduled while a domain is discarded'
context.fail!(message: message)
errors.add(:domain, message)
end
end
end

View file

@ -1,14 +1,14 @@
class Domain
module ForceDeleteInteractor
module ForceDeleteInteraction
class NotifyByEmail < Base
def call
return unless context.notify_by_email
def execute
return unless notify_by_email
if context.type == :fast_track
if type == :fast_track
send_email
domain.update(contact_notification_sent_date: Time.zone.today)
else
domain.update(template_name: context.domain.notification_template)
domain.update(template_name: domain.notification_template)
end
end

View file

@ -1,7 +1,7 @@
class Domain
module ForceDeleteInteractor
module ForceDeleteInteraction
class NotifyRegistrar < Base
def call
def execute
domain.registrar.notifications.create!(text: I18n.t('force_delete_set_on_domain',
domain_name: domain.name,
outzone_date: domain.outzone_date,

View file

@ -1,7 +1,7 @@
class Domain
module ForceDeleteInteractor
module ForceDeleteInteraction
class PostSetProcess < Base
def call
def execute
statuses = domain.statuses
# Stop all pending actions
statuses.delete(DomainStatus::PENDING_UPDATE)

View file

@ -1,11 +1,11 @@
class Domain
module ForceDeleteInteractor
module ForceDeleteInteraction
class PrepareDomain < Base
STATUSES_TO_SET = [DomainStatus::FORCE_DELETE,
DomainStatus::SERVER_RENEW_PROHIBITED,
DomainStatus::SERVER_TRANSFER_PROHIBITED].freeze
def call
def execute
domain.statuses_before_force_delete = domain.statuses
domain.statuses |= STATUSES_TO_SET
domain.save(validate: false)

View file

@ -0,0 +1,14 @@
class Domain
module ForceDeleteInteraction
class SetForceDelete < Base
def execute
compose(CheckDiscarded, inputs)
compose(PrepareDomain, inputs)
compose(SetStatus, inputs)
compose(PostSetProcess, inputs)
compose(NotifyRegistrar, inputs)
compose(NotifyByEmail, inputs)
end
end
end
end

View file

@ -1,9 +1,9 @@
class Domain
module ForceDeleteInteractor
module ForceDeleteInteraction
class SetStatus < Base
def call
domain.force_delete_type = context.type
context.type == :fast_track ? force_delete_fast_track : force_delete_soft
def execute
domain.force_delete_type = type
type == :fast_track ? force_delete_fast_track : force_delete_soft
domain.save(validate: false)
end
@ -19,6 +19,8 @@ class Domain
soft_forcedelete_dates(years) if years.positive?
end
private
def soft_forcedelete_dates(years)
domain.force_delete_start = domain.valid_to - years.years
domain.force_delete_date = domain.force_delete_start +
@ -26,8 +28,6 @@ class Domain
Setting.redemption_grace_period.days
end
private
def redemption_grace_period_days
Setting.redemption_grace_period.days + 1.day
end

View file

@ -1,13 +0,0 @@
class Domain
module ForceDeleteInteractor
class Base
include Interactor
private
def domain
@domain ||= context.domain
end
end
end
end

View file

@ -1,16 +0,0 @@
class Domain
module ForceDeleteInteractor
class SetForceDelete
include Interactor::Organizer
# As per https://github.com/collectiveidea/interactor#organizers
organize Domain::ForceDeleteInteractor::CheckDiscarded,
Domain::ForceDeleteInteractor::PrepareDomain,
Domain::ForceDeleteInteractor::SetStatus,
Domain::ForceDeleteInteractor::PostSetProcess,
Domain::ForceDeleteInteractor::NotifyRegistrar,
Domain::ForceDeleteInteractor::NotifyByEmail
end
end
end

View file

@ -53,7 +53,7 @@ module Concerns::Domain::ForceDelete # rubocop:disable Metrics/ModuleLength
end
def schedule_force_delete(type: :fast_track, notify_by_email: false)
Domain::ForceDeleteInteractor::SetForceDelete.call(domain: self,
Domain::ForceDeleteInteraction::SetForceDelete.run(domain: self,
type: type,
notify_by_email: notify_by_email)
end

View file

@ -36,6 +36,7 @@ module DomainNameRegistry
# Autoload all model subdirs
config.autoload_paths += Dir[Rails.root.join('app', 'models', '**/')]
config.autoload_paths += Dir[Rails.root.join('app', 'interactions', '**/')]
config.eager_load_paths << config.root.join('lib', 'validators')
config.watchable_dirs['lib'] = %i[rb]

View file

@ -111,10 +111,12 @@ class NewDomainForceDeleteTest < ActiveSupport::TestCase
def test_force_delete_cannot_be_scheduled_when_a_domain_is_discarded
@domain.update!(statuses: [DomainStatus::DELETE_CANDIDATE])
context = Domain::ForceDeleteInteractor::SetForceDelete.call(domain: @domain, type: :fast_track)
result = Domain::ForceDeleteInteraction::SetForceDelete.run(domain: @domain, type: :fast_track)
assert_not context.success?
assert_equal 'Force delete procedure cannot be scheduled while a domain is discarded', context.message
assert_not result.valid?
assert_not @domain.force_delete_scheduled?
message = ["Force delete procedure cannot be scheduled while a domain is discarded"]
assert_equal message, result.errors.messages[:domain]
end
def test_cancels_force_delete

View file

@ -414,7 +414,7 @@ class DomainTest < ActiveSupport::TestCase
force_delete_date: nil)
@domain.update(template_name: 'legal_person')
travel_to Time.zone.parse('2010-07-05')
Domain::ForceDeleteInteractor::SetForceDelete.call(domain: @domain, type: :fast_track)
Domain::ForceDeleteInteraction::SetForceDelete.run!(domain: @domain, type: :fast_track)
assert(@domain.force_delete_scheduled?)
other_registrant = Registrant.find_by(code: 'jane-001')
@domain.pending_json['new_registrant_id'] = other_registrant.id