mirror of
https://github.com/we-promise/sure.git
synced 2026-04-19 03:54:08 +00:00
Refactor date validation to DRY and improve test robustness
Co-authored-by: jjmata <187772+jjmata@users.noreply.github.com>
This commit is contained in:
@@ -12,11 +12,7 @@ class ReportsController < ApplicationController
|
||||
@end_date = parse_date_param(:end_date) || default_end_date
|
||||
|
||||
# Validate and fix date range if end_date is before start_date
|
||||
if @start_date > @end_date
|
||||
# Swap the dates to maintain user's intended date range
|
||||
@start_date, @end_date = @end_date, @start_date
|
||||
flash.now[:alert] = t("reports.invalid_date_range")
|
||||
end
|
||||
validate_and_fix_date_range(show_flash: true)
|
||||
|
||||
# Build the period
|
||||
@period = Period.custom(start_date: @start_date, end_date: @end_date)
|
||||
@@ -50,9 +46,8 @@ class ReportsController < ApplicationController
|
||||
@end_date = parse_date_param(:end_date) || default_end_date
|
||||
|
||||
# Validate and fix date range if end_date is before start_date
|
||||
if @start_date > @end_date
|
||||
@start_date, @end_date = @end_date, @start_date
|
||||
end
|
||||
# Don't show flash message since we're returning CSV data
|
||||
validate_and_fix_date_range(show_flash: false)
|
||||
|
||||
@period = Period.custom(start_date: @start_date, end_date: @end_date)
|
||||
|
||||
@@ -106,6 +101,14 @@ class ReportsController < ApplicationController
|
||||
|
||||
private
|
||||
|
||||
def validate_and_fix_date_range(show_flash: false)
|
||||
return unless @start_date > @end_date
|
||||
|
||||
# Swap the dates to maintain user's intended date range
|
||||
@start_date, @end_date = @end_date, @start_date
|
||||
flash.now[:alert] = t("reports.invalid_date_range") if show_flash
|
||||
end
|
||||
|
||||
def ensure_money(value)
|
||||
return value if value.is_a?(Money)
|
||||
# Value is numeric (BigDecimal or Integer) in dollars - pass directly to Money.new
|
||||
|
||||
Reference in New Issue
Block a user