-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This adds a service, which works in a similar way to ExpireRequestable and ReceiveRequestable to handle the transition from created to requested.
- Loading branch information
1 parent
dbcb906
commit 0d9902d
Showing
5 changed files
with
103 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# frozen_string_literal: true | ||
|
||
class RequestRequestable | ||
include ServicePattern | ||
|
||
def initialize(requestable:, user:) | ||
@requestable = requestable | ||
@user = user | ||
end | ||
|
||
def call | ||
raise AlreadyRequested if requestable.requested? | ||
|
||
ActiveRecord::Base.transaction do | ||
requestable.requested! | ||
create_timeline_event | ||
ApplicationFormStatusUpdater.call(application_form:, user:) | ||
end | ||
|
||
requestable.after_requested(user:) | ||
end | ||
|
||
class AlreadyRequested < StandardError | ||
end | ||
|
||
private | ||
|
||
attr_reader :requestable, :user | ||
|
||
delegate :application_form, to: :requestable | ||
|
||
def create_timeline_event | ||
creator = user.is_a?(String) ? nil : user | ||
creator_name = user.is_a?(String) ? user : "" | ||
|
||
TimelineEvent.create!( | ||
application_form:, | ||
creator:, | ||
creator_name:, | ||
event_type: "requestable_requested", | ||
requestable:, | ||
) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rails_helper" | ||
|
||
RSpec.describe RequestRequestable do | ||
let(:application_form) { create(:application_form, :submitted) } | ||
let(:requestable) do | ||
create( | ||
:qualification_request, | ||
assessment: create(:assessment, application_form:), | ||
) | ||
end | ||
let(:user) { "John Smith" } | ||
|
||
subject(:call) { described_class.call(requestable:, user:) } | ||
|
||
context "with an already requested requestable" do | ||
before { requestable.requested! } | ||
|
||
it "raises an error" do | ||
expect { call }.to raise_error(RequestRequestable::AlreadyRequested) | ||
end | ||
end | ||
|
||
it "changes the requestable state to requested" do | ||
expect { call }.to change(requestable, :requested?).from(false).to(true) | ||
end | ||
|
||
it "changes the requestable requested at" do | ||
freeze_time do | ||
expect { call }.to change(requestable, :requested_at).from(nil).to( | ||
Time.current, | ||
) | ||
end | ||
end | ||
|
||
it "records a requestable requested timeline event" do | ||
expect { call }.to have_recorded_timeline_event( | ||
:requestable_requested, | ||
requestable:, | ||
) | ||
end | ||
end |