Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions app/models/credit_card.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ class CreditCard < ApplicationRecord
"credit_card" => { short: "Credit Card", long: "Credit Card" }
}.freeze

validate :expiration_date_not_in_past

class << self
def color
"#F13636"
Expand All @@ -30,4 +32,13 @@ def minimum_payment_money
def annual_fee_money
annual_fee ? Money.new(annual_fee, account.currency) : nil
end

private

def expiration_date_not_in_past
return if expiration_date.blank?
return if expiration_date >= Date.current

errors.add(:expiration_date, :greater_than_or_equal_to, count: Date.current)
end
end
3 changes: 2 additions & 1 deletion app/views/credit_cards/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@

<div class="flex items-center gap-2">
<%= credit_card_form.date_field :expiration_date,
label: t("credit_cards.form.expiration_date") %>
label: t("credit_cards.form.expiration_date"),
min: Date.current %>
<%= credit_card_form.number_field :annual_fee,
label: t("credit_cards.form.annual_fee"),
placeholder: t("credit_cards.form.annual_fee_placeholder"),
Expand Down
2 changes: 1 addition & 1 deletion app/views/transactions/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<%= render DS::Dialog.new(scrollable: false) do |dialog| %>
<%= render DS::Dialog.new(scrollable: false, content_class: "lg:max-h-none") do |dialog| %>
<% dialog.with_header(title: "New transaction") %>
<% dialog.with_body do %>
<%= render "form", entry: @entry, categories: @categories %>
Expand Down
18 changes: 18 additions & 0 deletions test/models/credit_card_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require "test_helper"

class CreditCardTest < ActiveSupport::TestCase
test "is invalid when expiration date is in the past" do
card = credit_cards(:one)
card.expiration_date = Date.yesterday

assert_not card.valid?
assert_includes card.errors.details[:expiration_date].map { |error| error[:error] }, :greater_than_or_equal_to
end

test "is valid when expiration date is today or later" do
card = credit_cards(:one)
card.expiration_date = Date.current

assert card.valid?
end
end