# Encapsulates methods used on the Dashboard that need some business logic
module DateRangeHelper
  def date_range_params
    params.dig(:filters, :date_range).presence || this_year
  end

  def date_range_label
    case (params.dig(:filters, :date_range_label).presence || "this year").downcase
    when "yesterday"
      "yesterday"
    when "last 7 days"
      "over the last week"
    when "last 30 days"
      "over the 30 days"
    when "this month"
      "this month, so far"
    when "last month"
      "during the last month"
    else
      selected_range_described
    end
  end

  def this_year
    "01/01/#{Time.zone.today.year} - 12/31/#{Time.zone.today.year}"
  end

  def selected_interval
    date_range_params.split(" - ").map { |d| Date.strptime(d, "%m/%d/%Y") }
  end

  def selected_range
    start_date, end_date = selected_interval
    start_date..end_date
  end

  def selected_range_described
    start_date, end_date = selected_interval
    if start_date == Time.zone.today
      return ""
    elsif end_date == Time.zone.today
      return "since #{start_date}"
    else
      return "during the period #{start_date.to_s(:short)} to #{end_date.to_s(:short)}"
    end
  end
end

# Encapsulates methods used on the Dashboard that need some business logic
module DateRangeHelper
  def date_range_params
    params.dig(:filters, :date_range).presence || this_year
  end

  def date_range_label
    case (params.dig(:filters, :date_range_label).presence || "this year").downcase
    when "yesterday"
      "yesterday"
    when "last 7 days"
      "over the last week"
    when "last 30 days"
      "over the 30 days"
    when "this month"
      "this month, so far"
    when "last month"
      "during the last month"
    else
      selected_range_described
    end
  end

  def this_year
    "01/01/#{Time.zone.today.year} - 12/31/#{Time.zone.today.year}"
  end

  def selected_interval
    date_range_params.split(" - ").map { |d| Date.strptime(d, "%m/%d/%Y") }
  end

  def selected_range
    start_date, end_date = selected_interval
    (start_date.beginning_of_day)..(end_date.end_of_day)
  end

  def selected_range_described
    start_date, end_date = selected_interval
    if start_date == Time.zone.today
      return ""
    elsif end_date == Time.zone.today
      return "since #{start_date}"
    else
      return "during the period #{start_date.to_s(:short)} to #{end_date.to_s(:short)}"
    end
  end
end

Fix tests broken due to zero-length window
Fix tests broken due to zero-length window

Without this, the range is based on a date, without a time. Assuming both dates are the same, for example Jan 1, 1970, when sent to the database, this means that the `start_time` ends up being `Jan 1 1970 00:00:00` and the `end_time` also ends up being `Jan 1 1970 00:00:00`.  The user's intent here is to see all items matching for a given day, so make sure that the `end_time` is set to `23:59:59` and all relevant items are found.
