Skip to content

Commit cea7db1

Browse files
committed
fixed tests and added total percentage amount validation
1 parent 94b848a commit cea7db1

3 files changed

Lines changed: 10 additions & 3 deletions

File tree

app/controllers/projects_controller.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ def decide_tip_amounts
4848
authorize! :decide_tip_amounts, @project
4949
if request.patch?
5050
@project.available_amount # preload anything required to get the amount, otherwise it's loaded during the assignation and there are undesirable consequences
51+
percentages = params[:project][:tips_attributes].values.map{|tip| tip['amount_percentage'].to_f}
52+
if percentages.sum > 100
53+
redirect_to decide_tip_amounts_project_path(@project), alert: "You can't assign more than 100% of available funds."
54+
return
55+
end
56+
raise "wrong data" if percentages.min < 0
5157
@project.attributes = params.require(:project).permit(tips_attributes: [:id, :amount_percentage])
5258
if @project.save
5359
message = "The tip amounts have been defined"

app/models/tip.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def check_amount_against_project
104104
available_amount = project.available_amount
105105
available_amount -= amount_was if amount_was
106106
if amount > available_amount
107-
raise "Not enough funds on project to save #{inspect} (available: #{available_amount})"
107+
raise "Not enough funds on project to save #{inspect} (available: #{available_amount}). Project #{project.inspect} available_amount: #{project.available_amount} #{project.tips.count} tips: #{project.tips.map(&:amount).join(', ')}"
108108
end
109109
end
110110
end

features/tip_modifier_interface.feature

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,9 @@ Feature: A project collaborator can change the tips of commits
101101
And I click on "Decide tip amounts"
102102
And I choose the amount "Huge: 5%" on all commits
103103
And I click on "Send the selected tip amounts"
104-
Then there should be a tip of "25" for commit "BBB"
105-
And there should be a tip of "8.08833862" for commit "last"
104+
Then I should see "You can't assign more than 100% of available funds."
105+
And the tip amount for commit "BBB" should be undecided
106+
And the tip amount for commit "CCC" should be undecided
106107

107108
Scenario Outline: A collaborator changes the amount of a tip on another project
108109
Given the project holds tips

0 commit comments

Comments
 (0)