Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jc/refactor claims to wizard pattern #1188

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions app/controllers/claims/schools/claims/add_claim_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
class Claims::Schools::Claims::AddClaimController < Claims::ApplicationController
include WizardController
include Claims::BelongsToSchool

before_action :has_school_accepted_grant_conditions?
before_action :set_wizard
before_action :authorize_claim

helper_method :index_path

def update
if [email protected]_step
render "edit"
elsif @wizard.next_step.present?
redirect_to step_path(@wizard.next_step)
elsif @wizard.valid?
@wizard.create_claim
@wizard.reset_state
redirect_to confirmation_claims_school_claim_path(@school, @wizard.claim)
else
redirect_to rejected_claims_school_claims_path(@school)
end
end

private

def set_wizard
state = session[state_key] ||= {}
current_step = params[:step]&.to_sym
@wizard = Claims::AddClaimWizard.new(
school: @school, created_by: current_user, params:, state:, current_step:,
)
end

def authorize_claim
authorize Claims::Claim, :create?
end

def step_path(step)
add_claim_claims_school_claims_path(state_key:, step:)
end

def index_path
claims_school_claims_path(@school)
end
end
14 changes: 2 additions & 12 deletions app/controllers/claims/schools/claims_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class Claims::Schools::ClaimsController < Claims::ApplicationController
include Claims::BelongsToSchool

before_action :has_school_accepted_grant_conditions?
before_action :set_claim, only: %i[show check confirmation submit edit update rejected create_revision remove destroy]
before_action :set_claim, only: %i[show check confirmation submit edit update create_revision remove destroy]
before_action :authorize_claim
before_action :get_valid_revision, only: :check

Expand All @@ -12,16 +12,6 @@ def index
@pagy, @claims = pagy(@school.claims.active.order_created_at_desc)
end

def new; end

def create
if claim_provider_form.save
redirect_to new_claims_school_claim_mentors_path(@school, claim_provider_form.claim)
else
render :new
end
end

def create_revision
revision = Claims::Claim::CreateRevision.call(claim: @claim)
redirect_to edit_claims_school_claim_path(@school, revision)
Expand Down Expand Up @@ -103,7 +93,7 @@ def claim_id
end

def authorize_claim
authorize @claim || Claims::Claim
authorize @claim || Claims::Claim.new
end

def get_valid_revision
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
class Claims::Support::Schools::Claims::AddClaimController < Claims::Support::ApplicationController
include WizardController
include Claims::BelongsToSchool

before_action :has_school_accepted_grant_conditions?
before_action :set_wizard
before_action :authorize_claim

helper_method :index_path

def update
if [email protected]_step
render "edit"
elsif @wizard.next_step.present?
redirect_to step_path(@wizard.next_step)
elsif @wizard.valid?
@wizard.create_claim
@wizard.reset_state
redirect_to claims_support_school_claims_path(@school), flash: {
heading: t(".success"),
}
else
redirect_to rejected_claims_support_school_claims_path(@school)
end
end

private

def set_wizard
state = session[state_key] ||= {}
current_step = params[:step]&.to_sym
@wizard = Claims::AddClaimWizard.new(
school: @school, created_by: current_user, params:, state:, current_step:,
)
end

def authorize_claim
authorize Claims::Claim, :create?
end

def step_path(step)
add_claim_claims_support_school_claims_path(state_key:, step:)
end

def index_path
claims_support_school_claims_path(@school)
end
end
14 changes: 2 additions & 12 deletions app/controllers/claims/support/schools/claims_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class Claims::Support::Schools::ClaimsController < Claims::Support::ApplicationController
include Claims::BelongsToSchool

before_action :set_claim, only: %i[check draft show edit update remove destroy rejected create_revision]
before_action :set_claim, only: %i[check draft show edit update remove destroy create_revision]
before_action :authorize_claim
before_action :get_valid_revision, only: :check

Expand All @@ -11,8 +11,6 @@ def index
@pagy, @claims = pagy(@school.claims.active.order_created_at_desc)
end

def new; end

def show; end

def remove; end
Expand All @@ -25,14 +23,6 @@ def destroy
}
end

def create
if claim_provider_form.save
redirect_to new_claims_support_school_claim_mentors_path(@school, claim_provider_form.claim)
else
render :new
end
end

def create_revision
revision = Claims::Claim::CreateRevision.call(claim: @claim)
redirect_to edit_claims_support_school_claim_path(@school, revision)
Expand Down Expand Up @@ -112,7 +102,7 @@ def claim_provider_form
end

def authorize_claim
authorize @claim || Claims::Claim
authorize @claim || Claims::Claim.new
end

def get_valid_revision
Expand Down
2 changes: 2 additions & 0 deletions app/models/claims/claim.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ class Claims::Claim < ApplicationRecord
has_many :mentor_trainings, dependent: :destroy
has_many :mentors, through: :mentor_trainings

accepts_nested_attributes_for :mentor_trainings

validates :status, presence: true
validates(
:reference,
Expand Down
2 changes: 1 addition & 1 deletion app/policies/claims/claim_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def confirmation?

# TODO: Remove record.draft? and not create drafts for existing drafts
def draft?
current_claim_window? && user.support_user? && (record.internal_draft? || record.draft?)
current_claim_window? && user.support_user? && (record.internal_draft? || record.draft? || record.new_record?)
end

def check?
Expand Down
2 changes: 1 addition & 1 deletion app/services/claims/claim/calculate_amount.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def call
region = claim.school.region
claims_funding_available_per_hour_pence = region.claims_funding_available_per_hour_pence

total_hours_completed = claim.mentor_trainings.sum(:hours_completed)
total_hours_completed = claim.mentor_trainings.filter_map(&:hours_completed).sum

amount_in_pence = claims_funding_available_per_hour_pence * total_hours_completed

Expand Down
7 changes: 2 additions & 5 deletions app/services/claims/claim/create_draft.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class Claims::Claim::CreateDraft < ApplicationService
include Claims::Claim::Referencable

def initialize(claim:)
@claim = claim
end
Expand Down Expand Up @@ -33,9 +35,4 @@ def updated_claim
claim
end
end

def generate_reference
reference = SecureRandom.random_number(99_999_999) while Claims::Claim.exists?(reference:)
reference
end
end
7 changes: 2 additions & 5 deletions app/services/claims/claim/submit.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class Claims::Claim::Submit < ApplicationService
include Claims::Claim::Referencable

def initialize(claim:, user:)
@claim = claim
@user = user
Expand Down Expand Up @@ -39,9 +41,4 @@ def updated_claim
claim
end
end

def generate_reference
reference = SecureRandom.random_number(99_999_999) while Claims::Claim.exists?(reference:)
reference
end
end
13 changes: 13 additions & 0 deletions app/services/concerns/claims/claim/referencable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Claims::Claim::Referencable
extend ActiveSupport::Concern

included do
def generate_reference
loop do
reference = SecureRandom.random_number(99_999_999)

break reference unless Claims::Claim.exists?(reference:)
end
end
end
end
13 changes: 13 additions & 0 deletions app/views/claims/schools/claims/add_claim/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<% render "claims/schools/primary_navigation", school: @school, current: :claims %>

<%= content_for(:before_content) do %>
<%= govuk_back_link(href: back_link_path) %>
<% end %>

<div class="govuk-width-container">
<%= render_wizard(@wizard, contextual_text: t(".caption")) %>

<p class="govuk-body">
<%= govuk_link_to(t(".cancel"), index_path, no_visited_state: true) %>
</p>
</div>
2 changes: 1 addition & 1 deletion app/views/claims/schools/claims/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<p class="govuk-body"><%= sanitize t(".closing_date_disclaimer", time: l(Claims::ClaimWindow.current.ends_on.end_of_day, format: :time_on_date)) %></p>
<% end %>

<%= govuk_link_to t(".add_claim"), new_claims_school_claim_path, class: "govuk-button" %>
<%= govuk_button_link_to(t(".add_claim"), new_add_claim_claims_school_claims_path) %>
JamieCleare2525 marked this conversation as resolved.
Show resolved Hide resolved
<% else %>
<%= govuk_inset_text text: t(".add_mentor_guidance_html", link_to: govuk_link_to(t(".add_a_mentor"), claims_school_mentors_path(@school))) %>
<% end %>
Expand Down
13 changes: 13 additions & 0 deletions app/views/claims/support/schools/claims/add_claim/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<% render "claims/support/primary_navigation", current: :organisations %>

<%= content_for(:before_content) do %>
<%= govuk_back_link(href: back_link_path) %>
<% end %>

<div class="govuk-width-container">
<%= render_wizard(@wizard, contextual_text: t(".caption", school_name: @school.name)) %>

<p class="govuk-body">
<%= govuk_link_to(t(".cancel"), index_path, no_visited_state: true) %>
</p>
</div>
2 changes: 1 addition & 1 deletion app/views/claims/support/schools/claims/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<% if @school.mentors.any? %>
<%= govuk_inset_text text: t(".guidance", start_year: Claims::ClaimWindow.current.academic_year.starts_on.year, end_year: Claims::ClaimWindow.current.academic_year.ends_on.year) %>

<%= govuk_button_link_to t(".add_claim"), new_claims_support_school_claim_path %>
<%= govuk_button_link_to t(".add_claim"), new_add_claim_claims_support_school_claims_path %>
<% else %>
<%= govuk_inset_text text: t(".add_mentor_guidance_html", link_to: govuk_link_to(t(".add_a_mentor"), claims_support_school_mentors_path(@school))) %>
<% end %>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
</p>

<p class="govuk-body">
<%= govuk_link_to t(".change_provider"), new_claims_school_claim_path(school, id: claim_mentors_form.claim.id) %>
<%= govuk_link_to t(".change_provider"), new_claims_support_school_claim_path(school, id: claim_mentors_form.claim.id, claim_id: claim_mentors_form.claim.id) %>
JamieCleare2525 marked this conversation as resolved.
Show resolved Hide resolved
</p>
<% end %>
</div>
Expand Down
Loading
Loading