Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

feat(bills): custom repeat interval, dated search rows and FR price notice fix (#3636)

Anthony34bdc7855bc9
diff --git a/app/controllers/recurring_transactions_controller.rb b/app/controllers/recurring_transactions_controller.rb
index 914b742b0a..fb218a93fd 100644
--- a/app/controllers/recurring_transactions_controller.rb
+++ b/app/controllers/recurring_transactions_controller.rb
@@ -362,13 +362,14 @@ class RecurringTransactionsController < ApplicationController
:payment_url, :autopay, :notes, :bill_type, :category_id,
:renews_on, :trial_ends_on, :cancelled_on, :end_after_count,
:frequency_preset, :frequency_day_of_month, :frequency_second_day_of_month,
- :frequency_weekday, :frequency_month_of_year
+ :frequency_weekday, :frequency_month_of_year, :frequency_interval, :frequency_interval_unit
)
end
def new_recurring_transaction_params
params.require(:recurring_transaction).permit(
:name, :amount, :account_id, :first_due_on, :frequency_preset,
+ :frequency_interval, :frequency_interval_unit,
:payment_url, :autopay, :notes, :is_income
)
end
@@ -395,6 +396,8 @@ class RecurringTransactionsController < ApplicationController
@recurring_transaction.frequency_second_day_of_month = detection.second_day_of_month
@recurring_transaction.frequency_weekday = detection.weekday
@recurring_transaction.frequency_month_of_year = detection.month_of_year
+ @recurring_transaction.frequency_interval = detection.interval
+ @recurring_transaction.frequency_interval_unit = detection.interval_unit
end
# A bill outliving its own price is the normal case, and the only way to
@@ -447,7 +450,9 @@ class RecurringTransactionsController < ApplicationController
day_of_month: @recurring_transaction.frequency_day_of_month,
second_day_of_month: @recurring_transaction.frequency_second_day_of_month,
weekday: @recurring_transaction.frequency_weekday,
- month_of_year: @recurring_transaction.frequency_month_of_year
+ month_of_year: @recurring_transaction.frequency_month_of_year,
+ interval: @recurring_transaction.frequency_interval,
+ interval_unit: @recurring_transaction.frequency_interval_unit
)
# A hand-set cadence is intent, not a guess for detection to correct.
diff --git a/app/helpers/bills_helper.rb b/app/helpers/bills_helper.rb
index 33ec6a0e5f..82bcd5ccdf 100644
--- a/app/helpers/bills_helper.rb
+++ b/app/helpers/bills_helper.rb
@@ -1,4 +1,12 @@
module BillsHelper
+ # A bill's next due date reads "Oct 5" when it falls this year and gains the
+ # year otherwise, so an every-2-years bill never shows "Sep 1" for a date
+ # that is years out. Same rule as Period#label. Payment dates are historical
+ # and always carry the year instead.
+ def bills_upcoming_date(date)
+ l(date, format: date.year == Date.current.year ? :short : :short_with_year)
+ end
+
# One-shot AI features (smart-fill, smart-configure) need both the user's
# consent AND a resolvable LLM provider -- an unconfigured self-hosted
# install renders no AI affordances at all, following the Rules registry's
@@ -244,7 +252,7 @@ module BillsHelper
def occurrence_due_label(occurrence)
due = occurrence.effective_due_on
days = (due - Date.current).to_i
- date = l(due, format: :short)
+ date = bills_upcoming_date(due)
# A settled cycle is not late. This label only ever looked at dates, so a
# bill paid three weeks after its due date reported "Overdue by 20 days"
diff --git a/app/helpers/recurring_transactions_helper.rb b/app/helpers/recurring_transactions_helper.rb
index d1f94d9c73..eab4956ce2 100644
--- a/app/helpers/recurring_transactions_helper.rb
+++ b/app/helpers/recurring_transactions_helper.rb
@@ -18,11 +18,19 @@ module RecurringTransactionsHelper
[ t("recurring_transactions.frequency_presets.#{preset}"), preset ]
end
+ # The two custom options sit together after the named presets: "keep this
+ # schedule" only when the picker cannot express it, then "set my own".
if RecurringTransaction::FrequencyPreset.detect(recurring_transaction).key == RecurringTransaction::FrequencyPreset::CUSTOM
- options.unshift([ t("recurring_transactions.frequency_presets.custom"), RecurringTransaction::FrequencyPreset::CUSTOM ])
+ options << [ t("recurring_transactions.frequency_presets.custom"), RecurringTransaction::FrequencyPreset::CUSTOM ]
end
- options
+ options << [ t("recurring_transactions.frequency_presets.interval"), RecurringTransaction::FrequencyPreset::INTERVAL ]
+ end
+
+ def frequency_interval_unit_options
+ RecurringTransaction::FrequencyPreset::INTERVAL_UNITS.map do |unit|
+ [ t("recurring_transactions.frequency_interval_units.#{unit}"), unit ]
+ end
end
def frequency_day_options
diff --git a/app/javascript/controllers/frequency_fields_controller.js b/app/javascript/controllers/frequency_fields_controller.js
index f2cdf1c47a..45a9dffd71 100644
--- a/app/javascript/controllers/frequency_fields_controller.js
+++ b/app/javascript/controllers/frequency_fields_controller.js
@@ -1,9 +1,14 @@
import { Controller } from "@hotwired/stimulus";
+// The preset that reveals the unit picker; mirrors FrequencyPreset::INTERVAL.
+const INTERVAL_PRESET = "interval";
+
// Shows the frequency-picker field groups relevant to the selected preset.
-// Display logic only; the preset-to-rules translation is server-side.
+// Under the custom interval preset, groups listing the chosen unit in
+// data-units show as well. Display logic only; the preset-to-rules
+// translation is server-side.
export default class extends Controller {
- static targets = ["preset", "group"];
+ static targets = ["preset", "unit", "group"];
connect() {
this.update();
@@ -11,10 +16,25 @@ export default class extends Controller {
update() {
const preset = this.presetTarget.value;
+ const unit =
+ preset === INTERVAL_PRESET && this.hasUnitTarget
+ ? this.unitTarget.value
+ : null;
this.groupTargets.forEach((group) => {
const presets = (group.dataset.presets || "").split(",");
- group.classList.toggle("hidden", !presets.includes(preset));
+ const units = (group.dataset.units || "").split(",");
+ const visible =
+ presets.includes(preset) || (unit !== null && units.includes(unit));
+
+ group.classList.toggle("hidden", !visible);
+
+ // A hidden field still submits and is still constraint-validated, so a
+ // stale out-of-range count behind a hidden group blocked Save with no
+ // visible error. Disabled fields do neither.
+ for (const field of group.querySelectorAll("input, select, textarea")) {
+ field.disabled = !visible;
+ }
});
}
}
diff --git a/app/models/assistant/function/bills_support.rb b/app/models/assistant/function/bills_support.rb
index adf955c73d..776a33a91f 100644
--- a/app/models/assistant/function/bills_support.rb
+++ b/app/models/assistant/function/bills_support.rb
@@ -82,7 +82,9 @@ module Assistant::Function::BillsSupport
status: display_status(series),
amount: series.amount_money.abs.format,
currency: series.currency,
- frequency: detection.key || "custom",
+ # The tools' frequency enum is PRESETS; an every-N cadence outside it
+ # reads as custom there, as it did before the picker could set one.
+ frequency: detection.key.presence_in(RecurringTransaction::FrequencyPreset::PRESETS) || "custom",
next_due_date: series.next_due_date&.iso8601,
autopay: series.autopay,
detected_automatically: !series.manual,
diff --git a/app/models/recurring_transaction.rb b/app/models/recurring_transaction.rb
index 7d088bc2a1..52efa4079d 100644
--- a/app/models/recurring_transaction.rb
+++ b/app/models/recurring_transaction.rb
@@ -80,7 +80,8 @@ class RecurringTransaction < ApplicationRecord
# Form state for the frequency picker and the create-bill dialog;
# FrequencyPreset and the controller translate these on save. Not persisted.
attr_accessor :frequency_preset, :frequency_day_of_month, :frequency_second_day_of_month,
- :frequency_weekday, :frequency_month_of_year, :first_due_on, :is_income
+ :frequency_weekday, :frequency_month_of_year, :frequency_interval, :frequency_interval_unit,
+ :first_due_on, :is_income
# A scheme, followed by either "//" or by something that is not a port number.
# "example.com:8080" is a host and port, not a scheme, so it does not match.
diff --git a/app/models/recurring_transaction/ai_setup_suggester.rb b/app/models/recurring_transaction/ai_setup_suggester.rb
index 23cb9499de..12c88fea2a 100644
--- a/app/models/recurring_transaction/ai_setup_suggester.rb
+++ b/app/models/recurring_transaction/ai_setup_suggester.rb
@@ -77,7 +77,8 @@ class RecurringTransaction
{
name: series.display_name,
amount: series.amount.abs.to_s,
- frequency: detection.key,
+ # Only PRESETS are offered back; anything else reads as custom.
+ frequency: detection.key.presence_in(FrequencyPreset::PRESETS) || FrequencyPreset::CUSTOM,
day_of_month: detection.day_of_month,
weekday: detection.weekday,
month_of_year: detection.month_of_year,
diff --git a/app/models/recurring_transaction/declared_bill.rb b/app/models/recurring_transaction/declared_bill.rb
index 16ccb027f4..54c692c940 100644
--- a/app/models/recurring_transaction/declared_bill.rb
+++ b/app/models/recurring_transaction/declared_bill.rb
@@ -51,6 +51,8 @@ class RecurringTransaction
occurrence_count: 0
)
recurring.frequency_preset = attrs[:frequency_preset]
+ recurring.frequency_interval = attrs[:frequency_interval]
+ recurring.frequency_interval_unit = attrs[:frequency_interval_unit]
recurring.first_due_on = attrs[:first_due_on]
# A chosen account that does not resolve to something writable is said
@@ -81,7 +83,9 @@ class RecurringTransaction
preset: attrs[:frequency_preset],
day_of_month: due.day,
weekday: due.wday,
- month_of_year: due.month
+ month_of_year: due.month,
+ interval: attrs[:frequency_interval],
+ interval_unit: attrs[:frequency_interval_unit]
)
recurring
diff --git a/app/models/recurring_transaction/frequency_preset.rb b/app/models/recurring_transaction/frequency_preset.rb
index 36df1901f8..bdb6530f64 100644
--- a/app/models/recurring_transaction/frequency_preset.rb
+++ b/app/models/recurring_transaction/frequency_preset.rb
@@ -7,7 +7,15 @@ class RecurringTransaction
PRESETS = %w[monthly weekly biweekly semimonthly quarterly semiannual annual].freeze
CUSTOM = "custom"
- Detection = Data.define(:key, :day_of_month, :second_day_of_month, :weekday, :month_of_year)
+ # "Every N weeks, months or years": one rule of any interval, for the
+ # cadences no named preset covers. Kept out of PRESETS, which the AI tools
+ # publish as their frequency enum and which carries no interval.
+ INTERVAL = "interval"
+ INTERVAL_UNITS = %w[weekly monthly yearly].freeze
+ MAX_INTERVAL = 99
+
+ Detection = Data.define(:key, :day_of_month, :second_day_of_month, :weekday, :month_of_year,
+ :interval, :interval_unit)
class << self
# Reads the series' rules back into picker values. Zero rules is the
@@ -38,16 +46,28 @@ class RecurringTransaction
#
# Returns true only when it actually rewrote the cadence, so a caller can
# tell a deliberate schedule change from an unrelated edit.
- def apply(recurring, preset:, day_of_month: nil, second_day_of_month: nil, weekday: nil, month_of_year: nil)
+ #
+ # An out-of-range interval or unknown unit is reported on the record and
+ # rewrites nothing; callers check errors before saving.
+ def apply(recurring, preset:, day_of_month: nil, second_day_of_month: nil, weekday: nil, month_of_year: nil,
+ interval: nil, interval_unit: nil)
return false if preset.blank? || preset == CUSTOM
- return false unless PRESETS.include?(preset)
+ return false unless PRESETS.include?(preset) || preset == INTERVAL
+
+ interval = strict_int(interval)
+ if preset == INTERVAL && !valid_interval?(interval, interval_unit)
+ recurring.errors.add(:frequency_interval, :invalid)
+ return false
+ end
reference = recurring.anchor_date || recurring.last_occurrence_date || Date.current
target = target_detection(recurring, reference, preset,
day_of_month: presence_int(day_of_month),
second_day_of_month: presence_int(second_day_of_month),
weekday: presence_int(weekday),
- month_of_year: presence_int(month_of_year))
+ month_of_year: presence_int(month_of_year),
+ interval: interval,
+ interval_unit: interval_unit)
return false if target == detect(recurring)
write(recurring, target, reference)
@@ -75,49 +95,59 @@ class RecurringTransaction
when "annual"
I18n.t("recurring_transactions.frequency.annual",
month: I18n.t("date.month_names")[found.month_of_year], day: day_phrase(found.day_of_month))
+ when INTERVAL
+ interval_label(found)
else
I18n.t("recurring_transactions.frequency.custom")
end
end
private
- def detection(key:, day_of_month: nil, second_day_of_month: nil, weekday: nil, month_of_year: nil)
- Detection.new(key:, day_of_month:, second_day_of_month:, weekday:, month_of_year:)
+ def detection(key:, day_of_month: nil, second_day_of_month: nil, weekday: nil, month_of_year: nil,
+ interval: nil, interval_unit: nil)
+ Detection.new(key:, day_of_month:, second_day_of_month:, weekday:, month_of_year:, interval:, interval_unit:)
end
+ # Monthly and yearly rules anchored on an nth weekday ("3rd Friday")
+ # have no picker shape.
def detect_single(rule)
- case rule.frequency
- when "weekly"
- case rule.interval
- when 1 then detection(key: "weekly", weekday: rule.weekday)
- when 2 then detection(key: "biweekly", weekday: rule.weekday)
- else detection(key: CUSTOM)
- end
- when "monthly"
- return detection(key: CUSTOM) unless rule.day_of_month.present?
+ return detection(key: CUSTOM) if rule.frequency != "weekly" && rule.day_of_month.blank?
- case rule.interval
- when 1 then detection(key: "monthly", day_of_month: rule.day_of_month)
- when 3 then detection(key: "quarterly", day_of_month: rule.day_of_month)
- when 6 then detection(key: "semiannual", day_of_month: rule.day_of_month)
- else detection(key: CUSTOM)
- end
- when "yearly"
- if rule.interval == 1 && rule.day_of_month.present?
- detection(key: "annual", day_of_month: rule.day_of_month, month_of_year: rule.month_of_year)
- else
- detection(key: CUSTOM)
- end
+ every(rule.frequency, rule.interval, day_of_month: rule.day_of_month,
+ weekday: rule.weekday, month_of_year: rule.month_of_year)
+ end
+
+ # One rule of `interval` units, read as the named preset it is when one
+ # exists, so "every 3 months" and "quarterly" are the same schedule
+ # whichever way it was entered, and reapplying either is a no-op.
+ def every(unit, interval, day_of_month:, weekday:, month_of_year:)
+ case [ unit, interval ]
+ when [ "weekly", 1 ] then detection(key: "weekly", weekday: weekday)
+ when [ "weekly", 2 ] then detection(key: "biweekly", weekday: weekday)
+ when [ "monthly", 1 ] then detection(key: "monthly", day_of_month: day_of_month)
+ when [ "monthly", 3 ] then detection(key: "quarterly", day_of_month: day_of_month)
+ when [ "monthly", 6 ] then detection(key: "semiannual", day_of_month: day_of_month)
+ when [ "yearly", 1 ] then detection(key: "annual", day_of_month: day_of_month, month_of_year: month_of_year)
+ else
+ detection(key: INTERVAL, interval: interval, interval_unit: unit,
+ weekday: (weekday if unit == "weekly"),
+ day_of_month: (day_of_month unless unit == "weekly"),
+ month_of_year: (month_of_year if unit == "yearly"))
end
end
+ def valid_interval?(interval, unit)
+ INTERVAL_UNITS.include?(unit) && interval.present? && interval.between?(1, MAX_INTERVAL)
+ end
+
def live_rules(recurring)
recurring.recurrence_rules.reject(&:marked_for_destruction?)
end
# The Detection the submitted form values resolve to, with the same
# defaulting write() will use, so equality against detect() is exact.
- def target_detection(recurring, reference, preset, day_of_month:, second_day_of_month:, weekday:, month_of_year:)
+ def target_detection(recurring, reference, preset, day_of_month:, second_day_of_month:, weekday:, month_of_year:,
+ interval:, interval_unit:)
case preset
when "monthly", "quarterly", "semiannual"
detection(key: preset, day_of_month: day_of_month || recurring.expected_day_of_month)
@@ -133,6 +163,11 @@ class RecurringTransaction
when "annual"
detection(key: preset, day_of_month: day_of_month || reference.day,
month_of_year: month_of_year || reference.month)
+ when INTERVAL
+ every(interval_unit, interval,
+ day_of_month: day_of_month || (interval_unit == "yearly" ? reference.day : recurring.expected_day_of_month),
+ weekday: weekday || reference.wday,
+ month_of_year: month_of_year || reference.month)
end
end
@@ -160,9 +195,14 @@ class RecurringTransaction
when "annual"
build_rule(recurring, frequency: "yearly", day_of_month: target.day_of_month,
month_of_year: target.month_of_year)
+ when INTERVAL
+ recurring.anchor_date ||= reference
+ build_rule(recurring, frequency: target.interval_unit, interval: target.interval,
+ day_of_month: target.day_of_month, weekday: target.weekday,
+ month_of_year: target.month_of_year)
end
- recurring.expected_day_of_month = authoritative_day(target.key, target.day_of_month, reference)
+ recurring.expected_day_of_month = authoritative_day(target, reference)
end
# LAST sorts as the day it stands for, the end of the month, so
@@ -183,19 +223,32 @@ class RecurringTransaction
# expected_day_of_month stays NOT NULL and authoritative for monthly
# cadences; other cadences populate it but do not schedule from it.
- def authoritative_day(preset, day, reference)
- case preset
- when "monthly", "semimonthly", "quarterly", "semiannual", "annual"
- day == RecurrenceRule::LAST ? 31 : day
- else
- reference.day
+ def authoritative_day(target, reference)
+ day_anchored = case target.key
+ when "monthly", "semimonthly", "quarterly", "semiannual", "annual" then true
+ when INTERVAL then target.interval_unit != "weekly"
+ else false
end
+
+ return reference.day unless day_anchored
+
+ target.day_of_month == RecurrenceRule::LAST ? 31 : target.day_of_month
end
def presence_int(value)
value.present? ? value.to_i : nil
end
+ # The interval is typed, not picked from a list, so it is parsed whole:
+ # to_i would read "2.5" as 2 and "3abc" as 3 and save a cadence nobody
+ # asked for. Base 10 because Integer alone reads "010" as octal.
+ def strict_int(value)
+ return value if value.is_a?(Integer)
+ return nil if value.blank?
+
+ Integer(value.to_s, 10, exception: false)
+ end
+
def day_phrase(day)
return I18n.t("recurring_transactions.frequency.last_day") if day == RecurrenceRule::LAST
@@ -205,6 +258,21 @@ class RecurringTransaction
def weekday_name(weekday)
I18n.t("date.day_names")[weekday]
end
+
+ def interval_label(found)
+ case found.interval_unit
+ when "weekly"
+ I18n.t("recurring_transactions.frequency.every_n_weeks",
+ interval: found.interval, weekday: weekday_name(found.weekday))
+ when "monthly"
+ I18n.t("recurring_transactions.frequency.every_n_months",
+ interval: found.interval, day: day_phrase(found.day_of_month))
+ when "yearly"
+ I18n.t("recurring_transactions.frequency.every_n_years",
+ interval: found.interval, month: I18n.t("date.month_names")[found.month_of_year],
+ day: day_phrase(found.day_of_month))
+ end
+ end
end
end
end
diff --git a/app/views/bills/_detail.html.erb b/app/views/bills/_detail.html.erb
index f5957f0a28..9b8ec2d2a2 100644
--- a/app/views/bills/_detail.html.erb
+++ b/app/views/bills/_detail.html.erb
@@ -30,7 +30,7 @@
<%= "~" if series.has_amount_variance? %><%= format_money(Money.new(series.amount.abs, series.currency)) %>
</p>
<% if series.next_expected_date.present? %>
- <p class="text-xs text-secondary"><%= t("bills.detail.around", date: l(series.next_expected_date, format: :short)) %></p>
+ <p class="text-xs text-secondary"><%= t("bills.detail.around", date: bills_upcoming_date(series.next_expected_date)) %></p>
<% end %>
</div>
<% end %>
@@ -177,7 +177,7 @@
<div class="min-w-0">
<p class="text-sm text-primary truncate"><%= allocation.entry&.name.presence || t("bills.detail.manual_payment") %></p>
<% if allocation.paid_on %>
- <p class="text-xs text-secondary"><%= l(allocation.paid_on, format: :short) %></p>
+ <p class="text-xs text-secondary"><%= l(allocation.paid_on, format: :short_with_year) %></p>
<% end %>
</div>
<p class="text-sm text-primary privacy-sensitive shrink-0"><%= format_money(allocation.allocated_amount_money) %></p>
diff --git a/app/views/bills/_summary.html.erb b/app/views/bills/_summary.html.erb
index 6bb09a9ffe..d7ed94c1c7 100644
--- a/app/views/bills/_summary.html.erb
+++ b/app/views/bills/_summary.html.erb
@@ -35,7 +35,7 @@
<%= "~" if series.has_amount_variance? %><%= format_money(Money.new(series.amount.abs, series.currency)) %>
</p>
<p class="text-xs text-secondary">
- <%= series.next_expected_date.present? ? t("bills.detail.around", date: l(series.next_expected_date, format: :short)) : t("bills.detail.next_payment") %>
+ <%= series.next_expected_date.present? ? t("bills.detail.around", date: bills_upcoming_date(series.next_expected_date)) : t("bills.detail.next_payment") %>
</p>
</div>
<% end %>
@@ -60,7 +60,7 @@
<div class="min-w-0">
<p class="text-sm text-primary truncate"><%= allocation.entry&.name.presence || t("bills.detail.manual_payment") %></p>
<% if allocation.paid_on %>
- <p class="text-xs text-secondary"><%= l(allocation.paid_on, format: :short) %></p>
+ <p class="text-xs text-secondary"><%= l(allocation.paid_on, format: :short_with_year) %></p>
<% end %>
</div>
<p class="text-sm text-primary privacy-sensitive shrink-0"><%= format_money(allocation.allocated_amount_money) %></p>
diff --git a/app/views/bills/all.html.erb b/app/views/bills/all.html.erb
index 83979339bd..a65af1529a 100644
--- a/app/views/bills/all.html.erb
+++ b/app/views/bills/all.html.erb
@@ -90,7 +90,7 @@
<p class="text-xs text-secondary truncate">
<%= frequency_label(series) %>
<% if series.next_expected_date %>
- &middot; <%= t(".next_short", date: l(series.next_expected_date, format: :short)) %>
+ &middot; <%= t(".next_short", date: bills_upcoming_date(series.next_expected_date)) %>
<% end %>
</p>
</div>
@@ -169,7 +169,7 @@
<p class="text-xs text-secondary"><%= t(".monthly_equivalent", amount: format_money(monthly)) %></p>
<% end %>
</td>
- <td class="py-3 px-2 text-sm text-secondary"><%= series.next_expected_date ? l(series.next_expected_date, format: :short) : "–" %></td>
+ <td class="py-3 px-2 text-sm text-secondary"><%= series.next_expected_date ? bills_upcoming_date(series.next_expected_date) : "–" %></td>
<td class="py-3 px-2 text-sm">
<%= render DS::Pill.new(label: t("recurring_transactions.status.#{series.status}"), tone: recurring_status_pill_tone(series.status), marker: false) %>
</td>
diff --git a/app/views/bills/index.html.erb b/app/views/bills/index.html.erb
index 6c9e150868..ceb4734a27 100644
--- a/app/views/bills/index.html.erb
+++ b/app/views/bills/index.html.erb
@@ -84,7 +84,7 @@
) %>
<p class="text-xs text-secondary privacy-sensitive">
<%= format_money(suggestion.allocated_amount_money) %>
- &middot; <%= l(suggestion.paid_on, format: :short) %><%= " · #{reasons.join(" · ")}" if reasons.any? %>
+ &middot; <%= l(suggestion.paid_on, format: :short_with_year) %><%= " · #{reasons.join(" · ")}" if reasons.any? %>
</p>
</div>
<div class="flex items-center gap-2 shrink-0">
diff --git a/app/views/recurring_occurrences/_candidate.html.erb b/app/views/recurring_occurrences/_candidate.html.erb
index 5fcec48e87..c118f0ba5f 100644
--- a/app/views/recurring_occurrences/_candidate.html.erb
+++ b/app/views/recurring_occurrences/_candidate.html.erb
@@ -22,7 +22,7 @@
<div class="min-w-0 space-y-0.5">
<p class="text-sm font-medium text-primary truncate"><%= entry.name %></p>
<p class="text-xs text-secondary">
- <%= l(entry.date, format: :short) %>
+ <%= l(entry.date, format: :short_with_year) %>
<% if entry.account %>
&middot; <%= entry.account.name %>
<% end %>
@@ -55,7 +55,7 @@
<span class="min-w-0">
<span class="block text-sm text-primary truncate"><%= entry.name %></span>
<span class="block text-xs text-secondary truncate">
- <%= l(entry.date, format: :short) %><%= " · #{reasons.join(" · ")}" if reasons.any? %>
+ <%= l(entry.date, format: :short_with_year) %><%= " · #{reasons.join(" · ")}" if reasons.any? %>
</span>
</span>
<span class="text-sm text-primary privacy-sensitive shrink-0"><%= amount %></span>
diff --git a/app/views/recurring_occurrences/show.html.erb b/app/views/recurring_occurrences/show.html.erb
index d3267d46b4..67409556e0 100644
--- a/app/views/recurring_occurrences/show.html.erb
+++ b/app/views/recurring_occurrences/show.html.erb
@@ -83,7 +83,7 @@
<%= allocation.entry&.name.presence || t(".manual_payment") %>
</p>
<% if allocation.paid_on %>
- <p class="text-xs text-secondary"><%= l(allocation.paid_on, format: :short) %></p>
+ <p class="text-xs text-secondary"><%= l(allocation.paid_on, format: :short_with_year) %></p>
<% end %>
</div>
<div class="flex items-center gap-2 shrink-0">
@@ -115,7 +115,7 @@
<%= @pending_suggestion.entry&.name.presence || t(".manual_payment") %>
</p>
<% if @pending_suggestion.paid_on %>
- <p class="text-xs text-secondary"><%= l(@pending_suggestion.paid_on, format: :short) %></p>
+ <p class="text-xs text-secondary"><%= l(@pending_suggestion.paid_on, format: :short_with_year) %></p>
<% end %>
</div>
<p class="text-sm font-medium text-primary privacy-sensitive shrink-0">
diff --git a/app/views/recurring_transactions/_form.html.erb b/app/views/recurring_transactions/_form.html.erb
index 1399f2dfeb..aac3b1c046 100644
--- a/app/views/recurring_transactions/_form.html.erb
+++ b/app/views/recurring_transactions/_form.html.erb
@@ -79,18 +79,39 @@
{ label: income ? t(".income_frequency_label") : t(".frequency_label") },
{ data: { frequency_fields_target: "preset", action: "change->frequency-fields#update" } } %>
+ <%# Every N weeks, months or years, for the cadences no preset names.
+ Shown on create too: a new bill takes its day details from the
+ first due date, but the count and unit are only asked here. %>
+ <div data-frequency-fields-target="group" data-presets="interval" class="hidden">
+ <div class="grid grid-cols-2 gap-3">
+ <%= f.number_field :frequency_interval,
+ label: t(".frequency_interval_label"),
+ value: recurring_transaction.frequency_interval.presence || 2,
+ min: 1,
+ max: RecurringTransaction::FrequencyPreset::MAX_INTERVAL,
+ step: 1 %>
+ <%= f.select :frequency_interval_unit,
+ frequency_interval_unit_options,
+ { label: t(".frequency_interval_unit_label"),
+ selected: recurring_transaction.frequency_interval_unit.presence || "monthly" },
+ { data: { frequency_fields_target: "unit", action: "change->frequency-fields#update" } } %>
+ </div>
+ </div>
+
<% if recurring_transaction.persisted? %>
+ <%# data-units shows a group for the custom interval in that unit, so
+ "every 3 weeks" asks for a weekday and "every 2 months" a day. %>
<div class="grid grid-cols-2 gap-3">
- <div data-frequency-fields-target="group" data-presets="monthly,semimonthly,quarterly,semiannual,annual" class="hidden">
+ <div data-frequency-fields-target="group" data-presets="monthly,semimonthly,quarterly,semiannual,annual" data-units="monthly,yearly" class="hidden">
<%= f.select :frequency_day_of_month, frequency_day_options, { label: t(".frequency_day_label") } %>
</div>
<div data-frequency-fields-target="group" data-presets="semimonthly" class="hidden">
<%= f.select :frequency_second_day_of_month, frequency_day_options, { label: t(".frequency_second_day_label") } %>
</div>
- <div data-frequency-fields-target="group" data-presets="weekly,biweekly" class="hidden">
+ <div data-frequency-fields-target="group" data-presets="weekly,biweekly" data-units="weekly" class="hidden">
<%= f.select :frequency_weekday, frequency_weekday_options, { label: t(".frequency_weekday_label") } %>
</div>
- <div data-frequency-fields-target="group" data-presets="annual" class="hidden">
+ <div data-frequency-fields-target="group" data-presets="annual" data-units="yearly" class="hidden">
<%= f.select :frequency_month_of_year, frequency_month_options, { label: t(".frequency_month_label") } %>
</div>
</div>
diff --git a/app/views/recurring_transactions/_picker_row.html.erb b/app/views/recurring_transactions/_picker_row.html.erb
index 7b2d382cb2..e317852f2b 100644
--- a/app/views/recurring_transactions/_picker_row.html.erb
+++ b/app/views/recurring_transactions/_picker_row.html.erb
@@ -6,7 +6,7 @@
<span class="min-w-0">
<span class="block text-sm text-primary truncate"><%= entry.name %></span>
<span class="block text-xs text-secondary truncate">
- <%= l(entry.date, format: :short) %><% if entry.account %> &middot; <%= entry.account.name %><% end %><% if claimed_by %> &middot; <%= t(".claimed", name: claimed_by) %><% end %>
+ <%= l(entry.date, format: :short_with_year) %><% if entry.account %> &middot; <%= entry.account.name %><% end %><% if claimed_by %> &middot; <%= t(".claimed", name: claimed_by) %><% end %>
</span>
</span>
<span class="text-sm text-primary privacy-sensitive shrink-0">
diff --git a/app/views/recurring_transactions/new.html.erb b/app/views/recurring_transactions/new.html.erb
index 5af751c674..974e63ed30 100644
--- a/app/views/recurring_transactions/new.html.erb
+++ b/app/views/recurring_transactions/new.html.erb
@@ -13,7 +13,7 @@
<div class="min-w-0">
<p class="text-sm text-primary truncate"><%= candidate[:name] %></p>
<p class="text-xs text-secondary">
- <%= t(".candidate_meta", count: candidate[:count], date: l(candidate[:last_date], format: :short)) %>
+ <%= t(".candidate_meta", count: candidate[:count], date: l(candidate[:last_date], format: :short_with_year)) %>
</p>
</div>
<p class="text-sm text-primary privacy-sensitive shrink-0">
diff --git a/config/brakeman.ignore b/config/brakeman.ignore
index c9563c300f..8ddff990ae 100644
--- a/config/brakeman.ignore
+++ b/config/brakeman.ignore
@@ -199,13 +199,13 @@
{
"warning_type": "Mass Assignment",
"warning_code": 105,
- "fingerprint": "bfdd7e2efadaa4d5cb9740a250aab482c1e73bd0f46fd291af544da1c142104e",
+ "fingerprint": "dc0c96660c7e4fd22afb88b441ad90dda58879fbdbabfcb422ba072b1f5168ed",
"check_name": "PermitAttributes",
"message": "Potentially dangerous key allowed for mass assignment",
"file": "app/controllers/recurring_transactions_controller.rb",
- "line": 202,
+ "line": 371,
"link": "https://brakemanscanner.org/docs/warning_types/mass_assignment/",
- "code": "params.require(:recurring_transaction).permit(:name, :amount, :account_id, :first_due_on, :frequency_preset, :payment_url, :autopay, :notes, :is_income)",
+ "code": "params.require(:recurring_transaction).permit(:name, :amount, :account_id, :first_due_on, :frequency_preset, :frequency_interval, :frequency_interval_unit, :payment_url, :autopay, :notes, :is_income)",
"render_path": null,
"location": {
"type": "controller",
diff --git a/config/locales/models/recurring_transaction/en.yml b/config/locales/models/recurring_transaction/en.yml
index 70768adc27..d92aada6cd 100644
--- a/config/locales/models/recurring_transaction/en.yml
+++ b/config/locales/models/recurring_transaction/en.yml
@@ -10,6 +10,7 @@ en:
attributes:
recurring_transaction:
payment_url: "Payment link"
+ frequency_interval: "Repeat interval"
errors:
models:
recurring_transaction:
diff --git a/config/locales/models/recurring_transaction/fr.yml b/config/locales/models/recurring_transaction/fr.yml
index 56ae486ddd..f9539603e7 100644
--- a/config/locales/models/recurring_transaction/fr.yml
+++ b/config/locales/models/recurring_transaction/fr.yml
@@ -3,6 +3,7 @@ fr:
activerecord:
attributes:
recurring_transaction:
+ frequency_interval: Intervalle de répétition
payment_url: Lien de paiement
errors:
models:
diff --git a/config/locales/views/bills/fr.yml b/config/locales/views/bills/fr.yml
index f3bd8932c3..6140d8d6f6 100644
--- a/config/locales/views/bills/fr.yml
+++ b/config/locales/views/bills/fr.yml
@@ -146,7 +146,7 @@ fr:
other: "%{count} requièrent votre attention"
needs_attention: Requiert votre attention
needs_review: À vérifier
- notice_price: "%{name} a changé de prix : %{from} → %{to} (%{percent} %)"
+ notice_price: "%{name} a changé de prix : %{from} → %{to} (%{percent})"
notice_renewal: "%{name} se renouvelle le %{date}"
notice_trial: L'essai de %{name} se termine le %{date}
notices_routine:
diff --git a/config/locales/views/recurring_transactions/en.yml b/config/locales/views/recurring_transactions/en.yml
index 7e454ba6bb..ab3adcddf7 100644
--- a/config/locales/views/recurring_transactions/en.yml
+++ b/config/locales/views/recurring_transactions/en.yml
@@ -19,6 +19,11 @@ en:
semiannual: Every 6 months
annual: Yearly
custom: Custom schedule
+ interval: Custom interval
+ frequency_interval_units:
+ weekly: weeks
+ monthly: months
+ yearly: years
frequency:
monthly: "Monthly on the %{day}"
weekly: "Weekly on %{weekday}"
@@ -27,6 +32,9 @@ en:
quarterly: "Quarterly on the %{day}"
semiannual: "Every 6 months on the %{day}"
annual: "Yearly on %{month} %{day}"
+ every_n_weeks: "Every %{interval} weeks on %{weekday}"
+ every_n_months: "Every %{interval} months on the %{day}"
+ every_n_years: "Every %{interval} years on %{month} %{day}"
custom: Custom schedule
last_day: last day
identify_patterns: Identify Patterns
@@ -126,6 +134,8 @@ en:
frequency_label: How often
frequency_hint: How often this bill comes due. Pick the day details to match your statement.
frequency_day_label: On day
+ frequency_interval_label: Repeats every
+ frequency_interval_unit_label: Unit
frequency_second_day_label: And day
frequency_weekday_label: "On"
frequency_month_label: "In"
diff --git a/config/locales/views/recurring_transactions/fr.yml b/config/locales/views/recurring_transactions/fr.yml
index e2c8ab8b5f..7328ff37bb 100644
--- a/config/locales/views/recurring_transactions/fr.yml
+++ b/config/locales/views/recurring_transactions/fr.yml
@@ -59,6 +59,8 @@ fr:
first_due_on_label: Prochaine échéance
frequency_day_label: Le jour
frequency_hint: À quelle fréquence cette facture arrive à échéance. Choisissez les détails du jour pour correspondre à votre relevé.
+ frequency_interval_label: Intervalle
+ frequency_interval_unit_label: Unité
frequency_label: Fréquence
frequency_month_label: En
frequency_new_hint: Les détails du jour proviennent de la date d'échéance ci-dessus. Vous pourrez affiner l'échéancier après l'enregistrement.
@@ -94,6 +96,9 @@ fr:
annual: Chaque année le %{day} %{month}
biweekly: Toutes les 2 semaines, le %{weekday}
custom: Échéancier personnalisé
+ every_n_months: Tous les %{interval} mois, le %{day}
+ every_n_weeks: Toutes les %{interval} semaines, le %{weekday}
+ every_n_years: Tous les %{interval} ans, le %{day} %{month}
last_day: dernier jour
monthly: Chaque mois, le %{day}
quarterly: Chaque trimestre, le %{day}
@@ -104,11 +109,16 @@ fr:
annual: Annuelle
biweekly: Toutes les 2 semaines
custom: Échéancier personnalisé
+ interval: Intervalle personnalisé
monthly: Mensuelle
quarterly: Trimestrielle
semiannual: Tous les 6 mois
semimonthly: Deux fois par mois
weekly: Hebdomadaire
+ frequency_interval_units:
+ monthly: mois
+ weekly: semaines
+ yearly: années
identified: "%{count} modèles de transactions récurrentes identifiés"
identify_already_running: La détection est déjà en cours. Revenez dans un instant.
identify_patterns: Identifier les modèles
diff --git a/test/controllers/recurring_transactions_controller_test.rb b/test/controllers/recurring_transactions_controller_test.rb
index 048325da6b..bf7f13128f 100644
--- a/test/controllers/recurring_transactions_controller_test.rb
+++ b/test/controllers/recurring_transactions_controller_test.rb
@@ -413,6 +413,23 @@ class RecurringTransactionsControllerTest < ActionDispatch::IntegrationTest
assert_equal due, bill.anchor_date
end
+ test "create with a custom interval takes its day from the due date" do
+ due = Date.current + 4
+
+ post recurring_transactions_url, params: {
+ recurring_transaction: {
+ name: "Haircut", amount: "16", account_id: accounts(:depository).id,
+ first_due_on: due.iso8601, frequency_preset: "interval",
+ frequency_interval: "3", frequency_interval_unit: "weekly"
+ }
+ }
+
+ bill = @family.recurring_transactions.order(:created_at).last
+ rule = bill.recurrence_rules.sole
+ assert_equal [ "weekly", 3, due.wday ], [ rule.frequency, rule.interval, rule.weekday ]
+ assert_equal due, bill.anchor_date
+ end
+
test "create without a due date re-renders with an error" do
assert_no_difference "@family.recurring_transactions.count" do
post recurring_transactions_url, params: {
@@ -521,6 +538,41 @@ class RecurringTransactionsControllerTest < ActionDispatch::IntegrationTest
assert_empty @recurring_transaction.reload.recurrence_rules
end
+ test "update applies a custom interval as one rule every N units" do
+ patch recurring_transaction_url(@recurring_transaction),
+ params: { recurring_transaction: {
+ frequency_preset: "interval", frequency_interval: "3", frequency_interval_unit: "weekly", frequency_weekday: "5"
+ } }
+
+ assert_redirected_to recurring_transactions_url
+ rule = @recurring_transaction.reload.recurrence_rules.sole
+ assert_equal [ "weekly", 3, 5 ], [ rule.frequency, rule.interval, rule.weekday ]
+ assert @recurring_transaction.schedule_pinned?
+ end
+
+ test "update with an out-of-range interval re-renders without touching the rules" do
+ patch recurring_transaction_url(@recurring_transaction),
+ params: { recurring_transaction: {
+ frequency_preset: "interval", frequency_interval: "0", frequency_interval_unit: "monthly"
+ } },
+ headers: { "Turbo-Frame" => "modal" }
+
+ assert_response :unprocessable_entity
+ assert_empty @recurring_transaction.reload.recurrence_rules
+ end
+
+ test "edit pre-fills a custom interval" do
+ @recurring_transaction.update!(anchor_date: Date.current)
+ @recurring_transaction.recurrence_rules.create!(frequency: "monthly", interval: 2, day_of_month: 12)
+
+ get edit_recurring_transaction_url(@recurring_transaction)
+
+ assert_response :success
+ assert_select "select[name='recurring_transaction[frequency_preset]'] option[selected][value='interval']"
+ assert_select "input[name='recurring_transaction[frequency_interval]'][value='2']"
+ assert_select "select[name='recurring_transaction[frequency_interval_unit]'] option[selected][value='monthly']"
+ end
+
test "update rejects a non-http scheme instead of storing it" do
patch recurring_transaction_url(@recurring_transaction),
params: { recurring_transaction: { payment_url: "javascript:alert(1)" } }
diff --git a/test/helpers/bills_helper_test.rb b/test/helpers/bills_helper_test.rb
index 005e710e96..dae5f9e975 100644
--- a/test/helpers/bills_helper_test.rb
+++ b/test/helpers/bills_helper_test.rb
@@ -6,6 +6,13 @@ class BillsHelperTest < ActionView::TestCase
# ApplicationHelper rather than this module.
include ApplicationHelper
+ test "an upcoming date gains the year only when it falls in another one" do
+ travel_to Date.new(2026, 9, 25) do
+ assert_equal I18n.l(Date.new(2026, 10, 5), format: :short), bills_upcoming_date(Date.new(2026, 10, 5))
+ assert_equal I18n.l(Date.new(2028, 9, 1), format: :short_with_year), bills_upcoming_date(Date.new(2028, 9, 1))
+ end
+ end
+
# The matcher has always stored WHY it matched something, in match_signals.
# Nothing rendered it, so the app showed a bare percentage instead of the
# facts the percentage is made of.
@@ -195,6 +202,19 @@ class BillsHelperTest < ActionView::TestCase
assert_match(/due/i, label)
end
+ # An every-2-years bill's current cycle can be a year or more out, and
+ # "Due in 707 days, Sep 1" did not say which September.
+ test "a cycle due in another year names the year" do
+ travel_to Date.new(2026, 9, 25) do
+ later = build_occurrence(due_on: Date.new(2028, 9, 1), status: "scheduled")
+ soon = build_occurrence(due_on: Date.new(2026, 10, 5), status: "scheduled")
+
+ assert_includes occurrence_due_label(later), I18n.l(Date.new(2028, 9, 1), format: :short_with_year)
+ assert_includes occurrence_due_label(soon), I18n.l(Date.new(2026, 10, 5), format: :short)
+ assert_not_includes occurrence_due_label(soon), "2026"
+ end
+ end
+
test "a cycle past its grace is still labelled overdue" do
occurrence = build_occurrence(due_on: Date.current - 30, status: "scheduled")
assert_equal :overdue, occurrence.derived_state, "precondition: grace exhausted"
diff --git a/test/helpers/recurring_transactions_helper_test.rb b/test/helpers/recurring_transactions_helper_test.rb
index eccd7143ee..3731a0bc24 100644
--- a/test/helpers/recurring_transactions_helper_test.rb
+++ b/test/helpers/recurring_transactions_helper_test.rb
@@ -13,6 +13,17 @@ class RecurringTransactionsHelperTest < ActionView::TestCase
end
end
+ test "the two custom options follow the named presets, together" do
+ series = recurring_transactions(:netflix_subscription)
+ assert_equal "interval", frequency_preset_options(series).last.last
+
+ series.recurrence_rules.create!(frequency: "monthly", weekday: 5, weekday_ordinal: 3)
+ values = frequency_preset_options(series.reload).map(&:last)
+
+ assert_equal RecurringTransaction::FrequencyPreset::PRESETS, values.first(RecurringTransaction::FrequencyPreset::PRESETS.size)
+ assert_equal %w[custom interval], values.last(2)
+ end
+
test "day options end with the last-day sentinel" do
label, value = frequency_day_options.last
diff --git a/test/models/assistant/function/get_bills_test.rb b/test/models/assistant/function/get_bills_test.rb
index dd957440ba..e1bf07063b 100644
--- a/test/models/assistant/function/get_bills_test.rb
+++ b/test/models/assistant/function/get_bills_test.rb
@@ -22,6 +22,22 @@ class Assistant::Function::GetBillsTest < ActiveSupport::TestCase
assert_not_includes names, "Paused bill"
end
+ # The tools' frequency enum is FrequencyPreset::PRESETS, which has no
+ # interval. update_bill would pass an "interval" it read here straight back
+ # to FrequencyPreset.apply without a count or unit, and report success on an
+ # unchanged schedule.
+ test "an every-N cadence is reported as custom, never as a value outside the enum" do
+ series = create_series(name: "Water", amount: 60, anchor_date: Date.current)
+ RecurringTransaction::FrequencyPreset.apply(series, preset: "interval", interval: 2, interval_unit: "monthly",
+ day_of_month: Date.current.day)
+ series.save!
+
+ row = call_tool[:bills].find { |bill| bill[:name] == "Water" }
+
+ assert_equal "interval", RecurringTransaction::FrequencyPreset.detect(series).key
+ assert_equal "custom", row[:frequency]
+ end
+
test "the paused filter speaks the UI vocabulary over the stored value" do
create_series(name: "Paused bill", amount: 30, status: "inactive")
diff --git a/test/models/recurring_transaction/ai_setup_suggester_test.rb b/test/models/recurring_transaction/ai_setup_suggester_test.rb
index 24fe54b752..d3e1b8671f 100644
--- a/test/models/recurring_transaction/ai_setup_suggester_test.rb
+++ b/test/models/recurring_transaction/ai_setup_suggester_test.rb
@@ -110,6 +110,37 @@ class RecurringTransaction::AiSetupSuggesterTest < ActiveSupport::TestCase
refute suggestion.any_proposal?, "all-null fields mean the configuration is already right"
end
+ # The model answers in PRESETS; an "interval" it had never been offered as a
+ # value would come back as an unknown preset.
+ test "configure mode reports an every-N cadence to the provider as custom" do
+ series = @family.recurring_transactions.create!(
+ name: "Water", account: accounts(:depository), amount: 60, currency: "USD",
+ expected_day_of_month: 9, anchor_date: Date.current,
+ last_occurrence_date: 2.months.ago.to_date, next_expected_date: Date.current,
+ status: "active", manual: true
+ )
+ RecurringTransaction::FrequencyPreset.apply(series, preset: "interval", interval: 2, interval_unit: "monthly",
+ day_of_month: 9)
+ series.save!
+ create_entry(name: "Water", amount: 60, date: Date.current.beginning_of_month + 8.days - 2.months)
+
+ captured = nil
+ provider = Object.new
+ provider.define_singleton_method(:suggest_bill_setup) do |**kwargs|
+ captured = kwargs
+ Provider::Response.new(success?: true, data: RawSuggestion.new(
+ name: nil, amount: nil, frequency: nil, day_of_month: nil, weekday: nil,
+ month_of_year: nil, category_name: nil, bill_type: nil, autopay: nil,
+ confidence: 0.9, rationale: "already right"
+ ), error: nil)
+ end
+ Provider::Registry.stubs(:preferred_llm_provider).returns(provider)
+
+ Suggester.new(@family, user: @user).suggest_configuration(series)
+
+ assert_equal "custom", captured[:current_config][:frequency]
+ end
+
private
def raw(**overrides)
diff --git a/test/models/recurring_transaction/frequency_preset_test.rb b/test/models/recurring_transaction/frequency_preset_test.rb
index 04a0c7d84b..7d585d8509 100644
--- a/test/models/recurring_transaction/frequency_preset_test.rb
+++ b/test/models/recurring_transaction/frequency_preset_test.rb
@@ -140,4 +140,78 @@ class RecurringTransaction::FrequencyPresetTest < ActiveSupport::TestCase
assert_equal "monthly", Preset.detect(@recurring.reload).key
end
+
+ test "apply interval writes one weekly rule every N weeks and reads it back" do
+ @recurring.update!(anchor_date: nil)
+ assert Preset.apply(@recurring, preset: "interval", interval: "3", interval_unit: "weekly", weekday: "5")
+ @recurring.save!
+
+ rule = @recurring.recurrence_rules.reload.sole
+ assert_equal [ "weekly", 3, 5 ], [ rule.frequency, rule.interval, rule.weekday ]
+ assert_equal @recurring.last_occurrence_date, @recurring.anchor_date, "an every-N cadence needs a phase"
+
+ found = Preset.detect(@recurring)
+ assert_equal [ "interval", 3, "weekly", 5 ], [ found.key, found.interval, found.interval_unit, found.weekday ]
+ assert_equal "Every 3 weeks on Friday", Preset.label(@recurring)
+
+ occurrences = @recurring.schedule.occurrences_between(@recurring.anchor_date, @recurring.anchor_date + 70)
+ assert occurrences.all?(&:friday?)
+ assert_equal [ 21 ], occurrences.each_cons(2).map { |a, b| (b - a).to_i }.uniq
+ end
+
+ test "apply interval in months and years anchors on the chosen day" do
+ Preset.apply(@recurring, preset: "interval", interval: 2, interval_unit: "monthly", day_of_month: 12)
+ @recurring.save!
+
+ rule = @recurring.recurrence_rules.reload.sole
+ assert_equal [ "monthly", 2, 12 ], [ rule.frequency, rule.interval, rule.day_of_month ]
+ assert_equal 12, @recurring.expected_day_of_month
+ assert_equal "Every 2 months on the 12th", Preset.label(@recurring)
+
+ Preset.apply(@recurring, preset: "interval", interval: 2, interval_unit: "yearly",
+ day_of_month: 1, month_of_year: 9)
+ @recurring.save!
+
+ rule = @recurring.recurrence_rules.reload.sole
+ assert_equal [ "yearly", 2, 1, 9 ], [ rule.frequency, rule.interval, rule.day_of_month, rule.month_of_year ]
+ assert_equal "Every 2 years on September 1st", Preset.label(@recurring)
+ end
+
+ test "an interval a named preset covers is that preset" do
+ Preset.apply(@recurring, preset: "interval", interval: 3, interval_unit: "monthly", day_of_month: 10)
+ @recurring.save!
+ rule_ids = @recurring.recurrence_rules.reload.map(&:id)
+
+ assert_equal "quarterly", Preset.detect(@recurring).key
+ assert_not Preset.apply(@recurring, preset: "quarterly", day_of_month: "10"),
+ "quarterly entered either way is one schedule"
+ assert_equal rule_ids, @recurring.recurrence_rules.reload.map(&:id)
+ end
+
+ test "reapplying a detected interval is a no-op" do
+ Preset.apply(@recurring, preset: "interval", interval: 5, interval_unit: "weekly", weekday: 2)
+ @recurring.save!
+ found = Preset.detect(@recurring.reload)
+
+ assert_not Preset.apply(@recurring, preset: "interval", interval: found.interval.to_s,
+ interval_unit: found.interval_unit, weekday: found.weekday.to_s)
+ end
+
+ test "apply interval reads the count as a whole base-10 number" do
+ Preset.apply(@recurring, preset: "interval", interval: "08", interval_unit: "weekly", weekday: "5")
+ @recurring.save!
+
+ assert_equal 8, @recurring.recurrence_rules.reload.sole.interval, "a leading zero is not octal"
+ end
+
+ test "apply interval rejects an out-of-range, fractional or malformed count or an unknown unit" do
+ [ [ "0", "weekly" ], [ "", "monthly" ], [ (Preset::MAX_INTERVAL + 1).to_s, "monthly" ], [ "2", "daily" ],
+ [ "2.5", "monthly" ], [ "3abc", "weekly" ] ].each do |interval, unit|
+ recurring = RecurringTransaction.find(@recurring.id)
+
+ assert_not Preset.apply(recurring, preset: "interval", interval: interval, interval_unit: unit)
+ assert recurring.errors.added?(:frequency_interval, :invalid), "#{interval} #{unit} must be refused"
+ assert_empty recurring.recurrence_rules
+ end
+ end
end
diff --git a/test/system/recurring_transaction_frequency_test.rb b/test/system/recurring_transaction_frequency_test.rb
index cbaa972a0e..8c37aac9eb 100644
--- a/test/system/recurring_transaction_frequency_test.rb
+++ b/test/system/recurring_transaction_frequency_test.rb
@@ -34,4 +34,54 @@ class RecurringTransactionFrequencyTest < ApplicationSystemTestCase
assert_equal [ [ "weekly", 2, 5 ] ],
@recurring.reload.recurrence_rules.map { |rule| [ rule.frequency, rule.interval, rule.weekday ] }
end
+
+ # A hidden field still submits and is still constraint-validated: an
+ # out-of-range count left behind a hidden group made the browser refuse to
+ # submit, with no visible error and nothing to focus.
+ test "a count left out of range behind a hidden group does not block Save" do
+ visit edit_recurring_transaction_url(@recurring)
+
+ select I18n.t("recurring_transactions.frequency_presets.interval"),
+ from: I18n.t("recurring_transactions.form.frequency_label")
+ fill_in I18n.t("recurring_transactions.form.frequency_interval_label"), with: "150"
+
+ select I18n.t("recurring_transactions.frequency_presets.weekly"),
+ from: I18n.t("recurring_transactions.form.frequency_label")
+ select I18n.t("date.day_names")[5],
+ from: I18n.t("recurring_transactions.form.frequency_weekday_label")
+ click_button I18n.t("recurring_transactions.form.submit")
+
+ visit bills_url(view: "all")
+ assert_text I18n.t("recurring_transactions.frequency.weekly", weekday: I18n.t("date.day_names")[5])
+ assert_equal [ [ "weekly", 1, 5 ] ],
+ @recurring.reload.recurrence_rules.map { |rule| [ rule.frequency, rule.interval, rule.weekday ] }
+ end
+
+ test "a custom interval asks for the day that fits its unit and saves" do
+ visit edit_recurring_transaction_url(@recurring)
+
+ interval_group = find("[data-presets='interval']", visible: :all)
+ weekday_field = find("[data-presets='weekly,biweekly']", visible: :all)
+ assert_not interval_group.visible?
+
+ select I18n.t("recurring_transactions.frequency_presets.interval"),
+ from: I18n.t("recurring_transactions.form.frequency_label")
+ assert interval_group.visible?
+
+ # Months is the default unit, so the day-of-month group shows first.
+ assert_not weekday_field.visible?
+ select I18n.t("recurring_transactions.frequency_interval_units.weekly"),
+ from: I18n.t("recurring_transactions.form.frequency_interval_unit_label")
+ assert weekday_field.visible?
+
+ fill_in I18n.t("recurring_transactions.form.frequency_interval_label"), with: "3"
+ select I18n.t("date.day_names")[5],
+ from: I18n.t("recurring_transactions.form.frequency_weekday_label")
+ click_button I18n.t("recurring_transactions.form.submit")
+
+ visit bills_url(view: "all")
+ assert_text I18n.t("recurring_transactions.frequency.every_n_weeks", interval: 3, weekday: I18n.t("date.day_names")[5])
+ assert_equal [ [ "weekly", 3, 5 ] ],
+ @recurring.reload.recurrence_rules.map { |rule| [ rule.frequency, rule.interval, rule.weekday ] }
+ end
end