class ReportsHelper::ReportYear

Helper class for reporthelp_week_selection and reporthelp_month_selection. Initialize by passing a year for a full set of months, or by passing a year and a range of accurate dates. If the year matches the first or last date in that range, then the months associated with the year will be restricted according to the matching start and/or end date. The range, if given, MUST have a “first” date which is earlier in the year, or at worst the same date as the “last” date (i.e. don’t pass in “backwards” ranges).

Commercial numbered weeks within which a given date falls may start or end on the previous or next year. If the caller provides a limiting date range that exhibits this (e.g. passes in 31st December 2008, which actually lies in week 1, 2009) then the ReportYear object can do nothing other than represent a full set of its own numbered weeks. If you want to create an object for the extra year, where applicable, you must do so manually.



Public Class Methods

new( year, date_range = nil ) click to toggle source
# File app/helpers/reports_helper.rb, line 57
def initialize( year, date_range = nil )
  @title  = year.to_s
  @weeks  = []
  @months = []

  # "limit" is set if the object is being built for the current year.
  # Weeks and months are checked and any that lie in the future are
  # not included.

  today = Date.current
  limit = ( year == today.year )
  range = ( 1..( Timesheet.get_last_week_number( year ) ) )

  # Work out the weeks, limited as above, or perhaps by an optional date
  # range passed in by the caller.

  range.each do | week |
    first_day_of_week = Timesheet.date_for( year, week, TimesheetRow::FIRST_DAY, true )
    last_day_of_week  = first_day_of_week + 6

    break if (
      ( limit      and first_day_of_week > today           ) or
      ( date_range and first_day_of_week > date_range.last )

    next if (
      ( date_range and last_day_of_week < date_range.first )

    @weeks.unshift( year, week ) )

  # Work out the months, again limited as above.

  ( 1..12 ).each do | month |

    break if (
      ( limit      and month > today.month           ) or
      ( date_range and month > date_range.last.month and date_range.last.year == year )

    next if (
      ( date_range and month < date_range.first.month and date_range.first.year == year  )

    @months.unshift( year, month ) )