Skip to content

Commit bfc85d8

Browse files
kcdragonclaude
andauthored
Store giving amounts as integers (whole dollars) (#29)
The app only deals with large, whole-dollar giving amounts, so model total_giving_amount and allocation amount as integers instead of decimal(12,2). Adds an only_integer validation, steps amount form inputs by 1, and renders currency without trailing cents. Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
1 parent c99000e commit bfc85d8

7 files changed

Lines changed: 15 additions & 9 deletions

File tree

app/models/allocation/one_time.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class Allocation::OneTime < Allocation
22
validates :amount,
33
presence: true,
4-
numericality: { greater_than: 0 }
4+
numericality: { only_integer: true, greater_than: 0 }
55
validate :within_total_giving_amount
66

77
def ongoing?

app/views/scenarios/_allocation.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
</div>
99
<div class="flex items-center gap-3 shrink-0">
1010
<span class="font-semibold text-ink">
11-
<%= allocation.ongoing? ? "#{allocation.percentage}%" : number_to_currency(allocation.amount) %>
11+
<%= allocation.ongoing? ? "#{allocation.percentage}%" : number_to_currency(allocation.amount, precision: 0) %>
1212
</span>
1313
<button type="button" data-action="dialog#open"
1414
class="text-sm text-ink-faint hover:text-accent cursor-pointer bg-transparent p-0 leading-none">

app/views/scenarios/_allocation_modal.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
<%= label_tag "allocation_amount_#{suffix}", "Amount", class: "block text-sm text-ink-soft" %>
3030
<div class="relative mt-1">
3131
<span class="absolute left-3 top-1/2 -translate-y-1/2 text-ink-faint">$</span>
32-
<%= number_field_tag "allocation[amount]", allocation.amount, id: "allocation_amount_#{suffix}", min: 0, step: "0.01", required: true, placeholder: "0.00",
32+
<%= number_field_tag "allocation[amount]", allocation.amount, id: "allocation_amount_#{suffix}", min: 0, step: "1", required: true, placeholder: "0",
3333
class: "block w-full rounded-md border border-line bg-surface pl-7 pr-3 py-2 shadow-sm focus:border-accent focus:outline-none focus:ring-2 focus:ring-accent/10" %>
3434
</div>
3535
</div>

app/views/scenarios/index.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<%= link_to scenario.name, scenario_path(scenario), class: "font-serif font-medium text-lg text-ink hover:text-brand" %>
2020
</div>
2121
<p class="mt-2 text-ink-soft">
22-
<%= scenario.total_giving_amount.present? ? number_to_currency(scenario.total_giving_amount) : "No total set" %>
22+
<%= scenario.total_giving_amount.present? ? number_to_currency(scenario.total_giving_amount, precision: 0) : "No total set" %>
2323
</p>
2424
<div class="mt-4 flex items-center gap-3 text-sm">
2525
<%= link_to "Open", scenario_path(scenario), class: "text-brand hover:underline" %>

app/views/scenarios/show.html.erb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<%= form.label :total_giving_amount, "Total giving amount", class: "w-40 text-ink-soft" %>
1616
<div class="relative max-w-md w-full">
1717
<span class="absolute left-3 top-1/2 -translate-y-1/2 text-ink-faint">$</span>
18-
<%= form.number_field :total_giving_amount, step: "0.01", min: 0, placeholder: "0.00",
18+
<%= form.number_field :total_giving_amount, step: "1", min: 0, placeholder: "0",
1919
class: "block w-full rounded-md border border-line bg-surface pl-7 pr-3 py-2 shadow-sm focus:border-accent focus:outline-none focus:ring-2 focus:ring-accent/10" %>
2020
</div>
2121
</div>
@@ -73,7 +73,7 @@
7373
<% @scenario.one_time_allocations.each do |allocation| %>
7474
<li class="flex justify-between text-ink-soft">
7575
<span><%= allocation.option %></span>
76-
<span class="font-medium text-ink"><%= number_to_currency(allocation.amount) %></span>
76+
<span class="font-medium text-ink"><%= number_to_currency(allocation.amount, precision: 0) %></span>
7777
</li>
7878
<% end %>
7979
</ul>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class ChangeAmountsToInteger < ActiveRecord::Migration[8.1]
2+
def change
3+
change_column :scenarios, :total_giving_amount, :integer
4+
change_column :allocations, :amount, :integer
5+
end
6+
end

db/schema.rb

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)