Skip to content

Commit

Permalink
Fix specs
Browse files Browse the repository at this point in the history
  • Loading branch information
CatalinVoineag committed Apr 24, 2024
1 parent 75392c9 commit fd08e16
Show file tree
Hide file tree
Showing 10 changed files with 216 additions and 43 deletions.
8 changes: 3 additions & 5 deletions app/controllers/claims/schools/claims_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,17 @@ def create
def show; end

def check
valid_revision = @claim.get_valid_revision
last_mentor_training = valid_revision.mentor_trainings.order_by_mentor_full_name.last
@valid_claim = @claim.get_valid_revision
last_mentor_training = @valid_claim.mentor_trainings.order_by_mentor_full_name.last

@back_path = edit_claims_school_claim_mentor_training_path(
@school,
valid_revision,
@valid_claim,
last_mentor_training,
params: {
claims_claim_mentor_training_form: { hours_completed: last_mentor_training.hours_completed },
},
)

render locals: { claim: valid_revision }
end

def edit
Expand Down
8 changes: 3 additions & 5 deletions app/controllers/claims/support/schools/claims_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,17 @@ def create
end

def check
valid_revision = @claim.get_valid_revision
last_mentor_training = valid_revision.mentor_trainings.order_by_mentor_full_name.last
@valid_claim = @claim.get_valid_revision
last_mentor_training = @valid_claim.mentor_trainings.order_by_mentor_full_name.last

@back_path = edit_claims_support_school_claim_mentor_training_path(
@school,
valid_revision,
@valid_claim,
last_mentor_training,
params: {
claims_support_claim_mentor_training_form: { hours_completed: last_mentor_training.hours_completed },
},
)

render locals: { claim: valid_revision }
end

def edit
Expand Down
30 changes: 13 additions & 17 deletions app/models/claims/claim.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,34 +79,31 @@ def ready_to_be_checked?
mentors.present? && mentor_trainings.without_hours.blank?
end

def get_valid_revision
claim_record = self
Claims::Claim::RemoveEmptyMentorTrainingHours.call(claim: claim_record)

claim_record = claim_record.previous_revision while claim_record.present? && !claim_record.ready_to_be_checked?

claim_record
end

def deep_dup
dup_record = dup
dup_record.created_by = created_by
dup_record.submitted_by = submitted_by
dup_record.mentor_trainings = mentor_trainings.map(&:dup)
dup_record.previous_revision_id = id
dup_record.status = :internal_draft
dup_record
end

def create_revision!
dup = deep_dup
revision_record = deep_dup

ActiveRecord::Base.transaction do
dup.save!
update!(next_revision_id: dup.id)
revision_record.save!
update!(next_revision_id: revision_record.id)
end

dup
revision_record
end

def get_valid_revision
claim_record = self
Claims::Claim::RemoveEmptyMentorTrainingHours.call(claim: claim_record)

claim_record = claim_record.previous_revision while claim_record.present? && !claim_record.ready_to_be_checked?
claim_record
end

def has_revision?
Expand All @@ -115,9 +112,8 @@ def has_revision?

def was_draft?
claim_record = self

claim_record = claim_record.previous_revision while claim_record.present? && !claim_record.draft?

claim_record&.draft?
claim_record.nil? ? false : claim_record.draft?
end
end
16 changes: 8 additions & 8 deletions app/views/claims/schools/claims/check.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@

<% summary_list.with_row do |row| %>
<% row.with_key(text: Claims::Claim.human_attribute_name(:accredited_provider)) %>
<% row.with_value(text: claim.provider_name) %>
<% row.with_value(text: @valid_claim.provider_name) %>
<% row.with_action(text: t("change"),
href: edit_claims_school_claim_path(
@school,
claim,
@valid_claim,
params: { revision: true },
),
html_attributes: {
Expand All @@ -37,15 +37,15 @@
<% row.with_key(text: Claims::Claim.human_attribute_name(:mentors)) %>
<% row.with_value do %>
<ul class="govuk-list">
<% claim.mentors.each do |mentor| %>
<% @valid_claim.mentors.each do |mentor| %>
<li><%= mentor.full_name %></li>
<% end %>
</ul>
<% end %>
<% row.with_action(text: t("change"),
href: edit_claims_school_claim_mentor_path(
@school,
claim,
@valid_claim,
params: { revision: true },
),
html_attributes: {
Expand All @@ -57,14 +57,14 @@
<h2 class="govuk-heading-m"><%= t(".hours_of_training") %></h2>

<%= govuk_summary_list do |summary_list| %>
<% claim.mentor_trainings.order_by_mentor_full_name.each do |mentor_training| %>
<% @valid_claim.mentor_trainings.order_by_mentor_full_name.each do |mentor_training| %>
<% summary_list.with_row do |row| %>
<% row.with_key(text: mentor_training.mentor.full_name) %>
<% row.with_value(text: t(".hours", hours: mentor_training.hours_completed)) %>
<% row.with_action(text: t("change"),
href: edit_claims_school_claim_mentor_training_path(
@school,
claim,
@valid_claim,
mentor_training,
params: {
claims_claim_mentor_training_form: { hours_completed: mentor_training.hours_completed },
Expand All @@ -82,7 +82,7 @@
<%= govuk_summary_list do |summary_list| %>
<% summary_list.with_row do |row| %>
<% row.with_key(text: Claims::Claim.human_attribute_name(:claim_amount)) %>
<% row.with_value(text: humanized_money_with_symbol(claim.amount)) %>
<% row.with_value(text: humanized_money_with_symbol(@valid_claim.amount)) %>
<% end %>
<% end %>

Expand All @@ -100,7 +100,7 @@
<strong class="govuk-warning-text__text"><%= t(".warning") %></strong>
</div>

<%= govuk_button_to t(".submit"), submit_claims_school_claim_path(@school, claim) %>
<%= govuk_button_to t(".submit"), submit_claims_school_claim_path(@school, @valid_claim) %>

<p class="govuk-body">
<%= govuk_link_to t("cancel"), claims_school_claims_path(@school), no_visited_state: true %>
Expand Down
16 changes: 8 additions & 8 deletions app/views/claims/support/schools/claims/check.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
<%= govuk_summary_list do |summary_list| %>
<% summary_list.with_row do |row| %>
<% row.with_key(text: Claims::Claim.human_attribute_name(:accredited_provider)) %>
<% row.with_value(text: claim.provider_name) %>
<% row.with_value(text: @valid_claim.provider_name) %>
<% row.with_action(text: t("change"),
href: edit_claims_support_school_claim_path(
@school,
claim,
@valid_claim,
params: { revision: true },
),
html_attributes: {
Expand All @@ -32,14 +32,14 @@
<% row.with_key(text: Claims::Claim.human_attribute_name(:mentors)) %>
<% row.with_value do %>
<ul class="govuk-list">
<% claim.mentors.each do |mentor| %>
<% @valid_claim.mentors.each do |mentor| %>
<li><%= mentor.full_name %></li>
<% end %>
</ul>
<% end %>
<% row.with_action(text: t("change"),
href: edit_claims_support_school_claim_mentor_path(
@school, claim,
@school, @valid_claim,
params: { revision: true }
),
html_attributes: {
Expand All @@ -51,14 +51,14 @@
<h2 class="govuk-heading-m"><%= t(".hours_of_training") %></h2>

<%= govuk_summary_list do |summary_list| %>
<% claim.mentor_trainings.order_by_mentor_full_name.each do |mentor_training| %>
<% @valid_claim.mentor_trainings.order_by_mentor_full_name.each do |mentor_training| %>
<% summary_list.with_row do |row| %>
<% row.with_key(text: mentor_training.mentor.full_name) %>
<% row.with_value(text: t(".hours", hours: mentor_training.hours_completed)) %>
<% row.with_action(text: t("change"),
href: edit_claims_support_school_claim_mentor_training_path(
@school,
claim,
@valid_claim,
mentor_training,
params: {
claims_support_claim_mentor_training_form: { hours_completed: mentor_training.hours_completed },
Expand All @@ -76,11 +76,11 @@
<%= govuk_summary_list do |summary_list| %>
<% summary_list.with_row do |row| %>
<% row.with_key(text: Claims::Claim.human_attribute_name(:claim_amount)) %>
<% row.with_value(text: humanized_money_with_symbol(claim.amount)) %>
<% row.with_value(text: humanized_money_with_symbol(@valid_claim.amount)) %>
<% end %>
<% end %>

<%= govuk_button_to (claim.was_draft? ? t(".update") : t(".submit")), draft_claims_support_school_claim_path(@school, claim) %>
<%= govuk_button_to (@valid_claim.was_draft? ? t(".update") : t(".submit")), draft_claims_support_school_claim_path(@school, @valid_claim) %>

<p class="govuk-body">
<%= govuk_link_to t("cancel"), claims_support_school_claims_path(@school), no_visited_state: true %>
Expand Down
1 change: 1 addition & 0 deletions spec/factories/claims.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
association :school, factory: :claims_school
association :provider
association :created_by, factory: :claims_user
association :submitted_by, factory: :claims_user

status { :internal_draft }

Expand Down
143 changes: 143 additions & 0 deletions spec/models/claims/claim_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
it { is_expected.to belong_to(:school).class_name("Claims::School") }
it { is_expected.to belong_to(:provider) }
it { is_expected.to belong_to(:created_by) }
it { is_expected.to belong_to(:previous_revision).class_name("Claims::Claim").optional }
it { is_expected.to belong_to(:next_revision).class_name("Claims::Claim").optional }
it { is_expected.to belong_to(:submitted_by).optional }
it { is_expected.to have_many(:mentor_trainings).dependent(:destroy) }
it { is_expected.to have_many(:mentors).through(:mentor_trainings) }
Expand Down Expand Up @@ -113,4 +115,145 @@
end
end
end

describe "#ready_to_be_checked?" do
it "returns true if the claim's mentors all have their hours recorded" do
claim = create(:claim)
create(:mentor_training, hours_completed: 20, claim:)

expect(claim.ready_to_be_checked?).to eq(true)
end

it "returns false if the claim does have mentors" do
claim = build(:claim)

expect(claim.ready_to_be_checked?).to eq(false)
end

it "returns false if the claim does have mentor training hours" do
claim = create(:claim)
create(:mentor_training, hours_completed: nil, claim:)

expect(claim.ready_to_be_checked?).to eq(false)
end
end

describe "#deep_dup" do
it "builds a deep duplication of the claim, with associations" do
claim = create(:claim, :draft)
attributes = claim.attributes.except(
"id",
"created_at",
"updated_at",
"status",
"previous_revision_id",
"next_revision_id",
).keys
dup_record = claim.deep_dup

expect(dup_record.mentor_trainings).to eq(claim.mentor_trainings)
expect(dup_record.previous_revision_id).to eq(claim.id)
expect(dup_record.status).to eq("internal_draft")

attributes.each do |attribute|
expect(dup_record.public_send(attribute)).to eq(claim.public_send(attribute))
end
end
end

describe "#create_revision!" do
it "creates a revision of the claim, with associations" do
anne = create(:claims_user, :anne)
claim = create(:claim, :draft, submitted_by: anne, created_by: anne)
attributes = claim.attributes.except(
"id",
"created_at",
"updated_at",
"status",
"previous_revision_id",
"next_revision_id",
).keys
revision = claim.create_revision!

expect(revision.mentor_trainings).to eq(claim.mentor_trainings)
expect(revision.previous_revision_id).to eq(claim.id)
expect(revision.status).to eq("internal_draft")
expect(claim.next_revision_id).to eq(revision.id)

attributes.each do |attribute|
expect(revision.public_send(attribute)).to eq(claim.public_send(attribute))
end
end
end

describe "#get_valid_revision" do
it "gets the last valid revision" do
valid_revision = create(:claim, :draft)
create(:mentor_training, claim: valid_revision, hours_completed: 20)
invalid_revision = create(
:claim,
:draft,
previous_revision: valid_revision,
)
create(:mentor_training, claim: invalid_revision, hours_completed: nil)
claim = create(:claim, :draft, previous_revision: invalid_revision)
revision = claim.get_valid_revision

expect(revision).to eq(valid_revision)
end
end

describe "#has_revision?" do
it "returns true if previous_revision_id is present" do
previous_revision = create(:claim)
claim = build(:claim, previous_revision:)

expect(claim.has_revision?).to eq(true)
end

it "returns false if previous_revision_id is not present" do
claim = build(:claim, previous_revision: nil)

expect(claim.has_revision?).to eq(false)
end

it "returns true if next_revision_id is present" do
next_revision = create(:claim)
claim = build(:claim, next_revision:)

expect(claim.has_revision?).to eq(true)
end

it "returns true if next_revision_id is not present" do
claim = build(:claim, next_revision: nil)

expect(claim.has_revision?).to eq(false)
end
end

describe "#was_draft?" do
it "returns true if any previous revision was draft" do
draft_revision = create(:claim, :draft)
revision = create(
:claim,
:internal_draft,
previous_revision: draft_revision,
)
claim = create(:claim, :submitted, previous_revision: revision)

expect(claim.was_draft?).to eq(true)
end

it "returns false if no previous revision was draft" do
second_revision = create(:claim, :internal_draft)
revision = create(
:claim,
:internal_draft,
previous_revision: second_revision,
)
claim = create(:claim, :submitted, previous_revision: revision)

expect(claim.was_draft?).to eq(false)
end
end
end
Loading

0 comments on commit fd08e16

Please sign in to comment.