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

Refactor edit claim to wizard pattern #1212

Open
wants to merge 12 commits into
base: jc/refactor_claims_to_wizard_pattern
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
class Claims::Claim::MentorsForm::DisclaimerComponent < ApplicationComponent
attr_reader :mentors_form
class Claims::AddClaimWizard::MentorStep::DisclaimerComponent < ApplicationComponent
attr_reader :mentor_step

def initialize(mentors_form:, classes: [], html_attributes: {})
def initialize(mentor_step:, classes: [], html_attributes: {})
super(classes:, html_attributes:)

@mentors_form = mentors_form
@mentor_step = mentor_step
end

def call
Expand All @@ -15,13 +15,13 @@ def call
end

def render?
!mentors_form.all_school_mentors_visible?
!mentor_step.all_school_mentors_visible?
end

private

def provider_name
mentors_form.claim.provider_name
mentor_step.claim.provider_name
end

def email_link
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
class Claims::Claim::MentorTrainingForm::DisclaimerComponent < ApplicationComponent
attr_reader :mentor_training_form
class Claims::AddClaimWizard::MentorTrainingStep::DisclaimerComponent < ApplicationComponent
attr_reader :mentor_training_step

delegate :training_allowance, to: :mentor_training_form
delegate :training_allowance, to: :mentor_training_step

def initialize(mentor_training_form:, classes: [], html_attributes: {})
def initialize(mentor_training_step:, classes: [], html_attributes: {})
super(classes:, html_attributes:)

@mentor_training_form = mentor_training_form
@mentor_training_step = mentor_training_step
end

def call
Expand All @@ -23,11 +23,11 @@ def render?
private

def mentor_name
mentor_training_form.mentor_full_name
mentor_training_step.mentor_full_name
end

def provider_name
mentor_training_form.provider_name
mentor_training_step.provider_name
end

def support_link
Expand Down
55 changes: 55 additions & 0 deletions app/controllers/claims/schools/claims/edit_claim_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
class Claims::Schools::Claims::EditClaimController < 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 new
@wizard.setup_state
redirect_to step_path(params.require(:step).to_sym)
end

def update
if [email protected]_step
render "edit"
elsif @wizard.next_step.present?
redirect_to step_path(@wizard.next_step)
elsif @wizard.valid?
@wizard.update_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 claim
@claim = @school.claims.find(params.require(:id))
end

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

def authorize_claim
authorize claim, :update?
end

def step_path(step)
edit_claim_claims_school_claim_path(@school, claim, state_key:, step:)
end

def index_path
claims_school_claim_path(@school, claim)
end
end

This file was deleted.

64 changes: 0 additions & 64 deletions app/controllers/claims/schools/claims/mentors_controller.rb

This file was deleted.

65 changes: 4 additions & 61 deletions app/controllers/claims/schools/claims_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,19 @@ 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 create_revision remove destroy]
before_action :set_claim, only: %i[show confirmation remove destroy]
before_action :authorize_claim
before_action :get_valid_revision, only: :check

helper_method :claim_provider_form
helper_method :edit_attribute_path

def index
@pagy, @claims = pagy(@school.claims.active.order_created_at_desc)
end

def create_revision
revision = Claims::Claim::CreateRevision.call(claim: @claim)
redirect_to edit_claims_school_claim_path(@school, revision)
end

def show; end

def check
last_mentor_training = @claim.mentor_trainings.order_by_mentor_full_name.last
Claims::Claim::Review.call(claim: @claim)

@back_path = edit_claims_school_claim_mentor_training_path(
@school,
@claim,
last_mentor_training,
)
Claims::Claim::RemoveEmptyMentorTrainingHours.call(claim: @claim)
end

def edit; end

def update
if claim_provider_form.save
redirect_to claim_provider_form.update_success_path
else
render :edit
end
end

def confirmation; end

def submit
return redirect_to rejected_claims_school_claim_path unless @claim.valid_mentor_training_hours?

Claims::Claim::Submit.call(claim: @claim, user: current_user)

redirect_to confirmation_claims_school_claim_path(@school, @claim)
end

def rejected; end

def remove; end
Expand All @@ -65,25 +29,6 @@ def destroy

private

def claim_params
params.require(:claims_claim_provider_form)
.permit(:id, :provider_id)
.merge(default_params)
end

def default_params
{ school: @school, current_user:, claim_window: Claims::ClaimWindow.current }
end

def claim_provider_form
@claim_provider_form ||=
if params[:claims_claim_provider_form].present?
Claims::Claim::ProviderForm.new(claim_params)
else
Claims::Claim::ProviderForm.new(default_params.merge(id: claim_id))
end
end

def set_claim
@claim = @school.claims.find(claim_id)
end
Expand All @@ -96,9 +41,7 @@ def authorize_claim
authorize @claim || Claims::Claim.new
end

def get_valid_revision
revision = @claim.get_valid_revision

redirect_to check_claims_school_claim_path(@school, revision) if revision != @claim
def edit_attribute_path(attribute)
new_edit_claim_claims_school_claim_path(@school, @claim, step: attribute)
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
class Claims::Support::Schools::Claims::EditClaimController < 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 new
@wizard.setup_state
redirect_to step_path(params.require(:step).to_sym)
end

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

private

def claim
@claim = @school.claims.find(params.require(:id))
end

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

def authorize_claim
authorize claim, :update?
end

def step_path(step)
edit_claim_claims_support_school_claim_path(@school, claim, state_key:, step:)
end

def index_path
claims_support_school_claim_path(@school, claim)
end
end
Loading
Loading