Skip to content

Commit

Permalink
Update all pfmps params through manager to make it consistent
Browse files Browse the repository at this point in the history
  • Loading branch information
pskl committed Nov 4, 2024
1 parent 8b850cd commit 15f9235
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
6 changes: 2 additions & 4 deletions app/controllers/pfmps_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ def edit; end
def create
@pfmp = Pfmp.new(pfmp_params.merge(schooling: @schooling))

# TODO: use new API
if @pfmp.save
if PfmpManager.new(@pfmp).update(pfmp_params)
redirect_to student_path(@schooling.student),
notice: t("pfmps.new.success")
else
Expand All @@ -32,8 +31,7 @@ def create
end

def update
# TODO: use new API
if @pfmp.update(pfmp_params)
if PfmpManager.new(@pfmp).update(pfmp_params)
redirect_to school_year_class_schooling_pfmp_path(selected_school_year, @classe, @schooling, @pfmp),
notice: t("pfmps.edit.success")
else
Expand Down
9 changes: 7 additions & 2 deletions app/services/pfmp_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@ def initialize(pfmp)
@pfmp = pfmp
end

def update!(new_day_count:)
recalculate_amounts! if pfmp.day_count != new_day_count
def update(params)
pfmp.update(params.except(:day_count))
end

def update!(params)
recalculate_amounts! if pfmp.day_count != params[:day_count]
pfmp.update!(params.except(:day_count))
end

def recalculate_amounts!
Expand Down
10 changes: 6 additions & 4 deletions spec/services/pfmp_manager_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@
let(:pfmp) { create(:asp_payment_request, :sent).pfmp.reload }

it "throws an error" do
expect { PfmpManager.new(pfmp).update!(day_count: 15) }.to raise_error(PfmpManager::PfmpNotModifiableError)
expect do
described_class.new(pfmp).update!(day_count: 15)
end.to raise_error(PfmpManager::PfmpNotModifiableError)
end
end
end
Expand All @@ -83,7 +85,7 @@

it "recalculates the other modifiable pfmps amounts" do
expect do
PfmpManager.new(pfmp).update!(day_count: pfmp.day_count + 8)
described_class.new(pfmp).update!(day_count: pfmp.day_count + 8)
end.to change {
[pfmp.amount] + described_class.new(pfmp).send(:rebalancable_pfmps).pluck(:amount)
}.from([40, 120, 80]).to([200, 120, 80])
Expand Down Expand Up @@ -132,15 +134,15 @@
end

context "when the PFMP doesn't have a day count" do # rubocop:disable RSpec/MultipleMemoizedHelpers
before { PfmpManager.new(pfmp).update!(day_count: nil) }
before { described_class.new(pfmp).update!(day_count: nil) }

it { is_expected.to be_zero }
end

it_behaves_like "the original amount"

context "when the PFMP goes over the yearly cap" do # rubocop:disable RSpec/MultipleMemoizedHelpers
before { PfmpManager.new(pfmp).update!(day_count: 200) }
before { described_class.new(pfmp).update!(day_count: 200) }

it_behaves_like "the yearly-capped amount"
end
Expand Down

0 comments on commit 15f9235

Please sign in to comment.