module ReportsHelper

File

reports_helper.rb

(C)

Hipposoft 2008

Purpose

Support functions for views related to report generation. See controllers/reports_controller.rb for more.


18-Feb-2008 (ADH): Created.

Public Instance Methods

reporthelp_actions( report ) click to toggle source

Return appropriate list view actions for the given report

# File app/helpers/reports_helper.rb, line 407
def reporthelp_actions( report )
  actions = []

  # All links are generated with the *current* user's ID, because the
  # security mechanism is based around "can <this> user do the action
  # to <this other> report", where "<this>" comes from the user ID in
  # the URL. See SavedReportsBaseController.

  actions += [
    {
      :title => :delete,
      :url   => delete_user_saved_report_path( :user_id => @current_user.id, :id => "%s" )
    },
    {
      :title => :edit,
      :url   => edit_user_saved_report_path( :user_id => @current_user.id, :id => "%s" )
    }
  ] if report.can_be_modified_by?( @current_user )

  actions += [
    {
      :title => :copy,
      :url   => user_saved_report_copy_path( :user_id => @current_user.id, :saved_report_id => "%s" )
    },
    {
      :title => :show,
      :url   => user_saved_report_path( :user_id => @current_user.id, :id => "%s" )
    }
  ] if report.is_permitted_for?( @current_user )

  return actions
end
reporthelp_decorated_hours( actual, potential ) click to toggle source

Report a terse number of hours with an ‘overrun’ span if negative or a ‘no_overrun’ span if >= 0 in two sections separated by a “/” - the idea is to pass actual and potential remaining hours for a task in here.

# File app/helpers/reports_helper.rb, line 360
def reporthelp_decorated_hours( actual, potential )
  class_name  = actual    < 0 ? 'overrun' : 'no_overrun'
  output      = "<strong><span class=\"#{ class_name }\">#{ apphelp_terse_hours( actual ) }</span> / "
  class_name  = potential < 0 ? 'overrun' : 'no_overrun'
  output     << "<span class=\"#{ class_name }\">#{ apphelp_terse_hours( potential ) }</span></strong>"

  return output.html_safe()
end
reporthelp_end_date( report ) click to toggle source

List helper - formatted end date for the given report

# File app/helpers/reports_helper.rb, line 397
def reporthelp_end_date( report )
  if ( report.range_end_cache.class != Date )
    apphelp_view_hint( "date_end_#{ report.range_end_cache }" )
  else
    apphelp_date( report.range_end_cache )
  end
end
reporthelp_end_time( form ) click to toggle source

Use the Calendar Date Select plug-in to generate a selector for the end time of a report. Pass the form instance upon which to operate.

# File app/helpers/reports_helper.rb, line 125
def reporthelp_end_time( form )
  return form.calendar_date_select(
    :range_end,
    {
      :embedded   => false,
      :year_range => Timesheet.used_range()
    }
  )
end
reporthelp_export_option( submodule, option ) click to toggle source

Return an input element and label as part of a form used to export a report. Pass the submodule of TrackRecordReportGenerator for which options are being generated and the array entry from the option data the submodule provides in “invocation_options_for”.

# File app/helpers/reports_helper.rb, line 445
def reporthelp_export_option( submodule, option )
  prefix = submodule.name.underscore
  kind   = option.keys.first
  data   = option.values.first

  case kind
    when :checkbox
      id      = "#{ prefix }[#{ data[ :id ] }]"
      output  = check_box_tag( id, "1", data[ :checked ] )
      output << label_tag( id, data[ :label ] )

    when :radio
      id     = data[ :id ]
      name   = "#{ prefix }[#{ data[ :name ] }]"
      output = radio_button_tag( name, id, data[ :checked ] )
      output << label_tag( "#{ name }_#{ id }", data[ :label ] )

    else
      ""
  end
end
reporthelp_frequency_selection( form ) click to toggle source

Return HTML suitable for inclusion in the form passed in the first parameter (i.e. the ‘f’ in “form for … do |f|” ), which provides a selection list allowing the user to choose a report frequency (weekly, monthly etc.).

# File app/helpers/reports_helper.rb, line 239
def reporthelp_frequency_selection( form )
  collection = []

  Report.labels.each_index do | index |
    collection.push( [ Report.labels[ index ], index ] )
  end

  form.select( :frequency, collection )
end
reporthelp_grouping_selector( form ) click to toggle source

Generate a menu for the given form allowing the user to choose a grouping option.

# File app/helpers/reports_helper.rb, line 292
def reporthelp_grouping_selector( form )
  return apphelp_select(
    form,
    :task_grouping,
    [
      [ 'No special grouping',                'default'  ],
      [ 'Group billable tasks together',      'billable' ],
      [ 'Group active tasks together',        'active'   ],
      [ 'Group both kinds of tasks together', 'both'     ]
    ],
    false
  )
end
reporthelp_hours( calculator, show_zero = false ) click to toggle source

Return HTML suitable for an ‘hours’ field which gives a total amount, then a committed and not-committed amount wrapped in SPANs to give the correct committed/uncommitted text styles. If the overall total time is zero, “&nbsp;” is returned instead. Pass any object subclassing TrackRecordReport::ReportElementaryCalculator and, optionally, ‘true’ to show “0.0” rather than “&nbsp;” in the zero hours total case.

# File app/helpers/reports_helper.rb, line 328
def reporthelp_hours( calculator, show_zero = false )
  if ( calculator.try( :has_hours? ) )
    output  = ''
    output << apphelp_terse_hours( calculator.total ) if ( @report.include_totals != false )
    output << ' (' if ( @report.include_totals != false and ( @report.include_committed != false or @report.include_not_committed != false ) )

    if ( @report.include_committed != false )
      output << '<span class="timesheet_committed">'
      output << apphelp_terse_hours( calculator.committed )
      output << '</span>'
    end

    output << '/' if ( @report.include_committed != false and @report.include_not_committed != false )

    if ( @report.include_not_committed != false )
      output << '<span class="timesheet_not_committed">'
      output << apphelp_terse_hours( calculator.not_committed )
      output << '</span>'
    end

    output << ')' if ( @report.include_totals != false and ( @report.include_committed != false or @report.include_not_committed != false ) )

    return output.html_safe()
  else
    return ( show_zero ? '0' : '&nbsp;' ).html_safe()
  end
end
reporthelp_month_selection( form, method ) click to toggle source

Return HTML suitable for inclusion in a form which provides a pull-down menu of years for the full permitted time range subdivided into months. Pass the form being constructed and “:range_month_start” or “:range_month_end”.

# File app/helpers/reports_helper.rb, line 140
def reporthelp_month_selection( form, method )
  start_or_end = ( method == :range_month_start ) ? :start : :end

  form.grouped_collection_select(
    method,
    get_year_array(), # Use years for groups
    :months,          # A years's "months" method returns its month list
    :title,           # Use the year 'title' for the group labels
    :id,              # A month's "id" method returns the value for an option tag
    start_or_end,     # A month's "start" or "end" method is used for the option contents
    {
      :include_blank => '-'
    }
  )
end
reporthelp_one_selection( form, method ) click to toggle source

Return HTML suitable for inclusion in a form which provides options such as “this month” or “one week ago” in a selection list. Pass the form being constructed and “:range_one_month” or “:range_one_week”.

# File app/helpers/reports_helper.rb, line 182
def reporthelp_one_selection( form, method )
  now = DateTime.now.utc

  if ( method == :range_one_week )
    strings = [
      apphelp_view_hint( :last_week,     SavedReportsController ),
      apphelp_view_hint( :this_week,     SavedReportsController ),
      apphelp_view_hint( :two_weeks_ago, SavedReportsController ),
    ]
    dates = [
      now - 1.week,
      now,
      now - 2.weeks
    ]
    objects = dates.map do | date |
      ReportWeek.new( date.year, date.cweek )
    end
  else
    strings = [
      apphelp_view_hint( :last_month,     SavedReportsController ),
      apphelp_view_hint( :this_month,     SavedReportsController ),
      apphelp_view_hint( :two_months_ago, SavedReportsController ),
    ]
    dates = [
      now - 1.month,
      now,
      now - 2.months
    ]
    objects = dates.map do | date |
      ReportMonth.new( date.year, date.month )
    end
  end

  keys  = [ "last", "this", "two" ]
  array = strings.map.with_index do | string, index |
    o       = OpenStruct.new
    o.value = keys[ index ]
    o.text  = string % objects[ index ].all # The "all" method on a ReportWeek or ReportMonth gives its name/date in a suitable format
    o
  end

  form.collection_select(
    method,
    array,
    :value,
    :text,
    {
      :include_blank => '-'
    }
  )
end
reporthelp_owner( report ) click to toggle source

List helper - owner of the given report

# File app/helpers/reports_helper.rb, line 375
def reporthelp_owner( report )
  return link_to( report.user.name, user_path( report.user ) )
end
reporthelp_sorting_selector( form, method ) click to toggle source

Generate a sort field selector menu for the given form which will use the given method to set the chosen option in the report - one of “:customer_sort_field”, “:project_sort_field” or “:task_sort_field”.

# File app/helpers/reports_helper.rb, line 276
def reporthelp_sorting_selector( form, method )
  return apphelp_select(
    form,
    method,
    [
      [ 'Sort by name',          'title'      ],
      [ 'Sort by code',          'code'       ],
      [ 'Sort by addition date', 'created_at' ]
    ],
    false
  )
end
reporthelp_start_date( report ) click to toggle source

List helper - formatted start date for the given report

# File app/helpers/reports_helper.rb, line 387
def reporthelp_start_date( report )
  if ( report.range_start_cache.class != Date )
    apphelp_view_hint( "date_start_#{ report.range_start_cache }" )
  else
    apphelp_date( report.range_start_cache )
  end
end
reporthelp_start_time( form ) click to toggle source

Use the Calendar Date Select plug-in to generate a selector for the start time of a report. Pass the form instance upon which to operate.

# File app/helpers/reports_helper.rb, line 112
def reporthelp_start_time( form )
  return form.calendar_date_select(
    :range_start,
    {
      :embedded   => false,
      :year_range => Timesheet.used_range()
    }
  )
end
reporthelp_updated_at( report ) click to toggle source

List helper - formatted ‘updated at’ date for the given report

# File app/helpers/reports_helper.rb, line 381
def reporthelp_updated_at( report )
  return apphelp_date( report.updated_at )
end
reporthelp_user_selection( form, users ) click to toggle source

Return HTML suitable for inclusion in the form passed in the first parameter (i.e. the ‘f’ in “form for … do |f|” ), based on the user array given in the second parameter, which provides:

  • A <select> tag with options listing all users in the array in the order in which they are stored in that array.

  • An empty string if the input users array is itself empty.

# File app/helpers/reports_helper.rb, line 258
def reporthelp_user_selection( form, users )
  if ( users.empty? )
    return ''
  else
    return apphelp_collection_select(
      form,
      'reportable_user_ids',
      users,
      :id,
      :name
    )
  end
end
reporthelp_week_selection( form, method ) click to toggle source

Return HTML suitable for inclusion in a form which provides a pull-down menu of years for the full permitted time range subdivided into weeks. Pass the form being constructed and “:range_week_start” or “:range_week_end”.

# File app/helpers/reports_helper.rb, line 161
def reporthelp_week_selection( form, method )
  start_or_end = ( method == :range_week_start ) ? :start : :end

  form.grouped_collection_select(
    method,
    get_year_array(), # Use years for groups
    :weeks,           # A years's "weeks" method returns its week list
    :title,           # Use the year 'title' for the group labels
    :id,              # A week's "id" method returns the value for an option tag
    start_or_end,     # A week's "start" or "end" method is used for the option contents
    {
      :include_blank => '-'
    }
  )
end
reporthelp_work_breakdown_item_name( item ) click to toggle source

Pass a hash. The “:item” entry is read. If a User, a string of “by <name>” is returned. Otherwise, a string of “on ‘<title>’” is returned. Useful for indicating hours are done by a user, or were worked on some task.

# File app/helpers/reports_helper.rb, line 311
def reporthelp_work_breakdown_item_name( item )
  item = item[ :item ]

  if ( item.class == User )
    return "by #{ h( item.name ) }".html_safe()
  else
    return "on '#{ h( item.augmented_title ) }'".html_safe()
  end
end