app/controllers/reports_controller.rb
# TODO: refactor into a model method on Report
#Submitting a shift
refactor into a model method on Report
Submitting a shift
refactor into a model method on Report Submitting a shift
def update
    @report = Report.find(params[:id])
    return unless user_is_owner_or_admin_of(@report.shift, @report.shift.department) || current_user.is_admin_of?(@report.shift.location)
    if (params[:sign_out] and @report.departed.nil?) 
      @report.departed = Time.now
      @report.report_items << ReportItem.new(time: Time.now, content: "#{current_user.name} (#{current_user.login}) logged out from #{request.remote_ip}", ip_address: request.remote_ip)
      @report.shift.update_attribute(:end, Time.now) unless @report.shift.scheduled?
      @add_payform_item = true
    end
    if @report.update_attributes(params[:report]) && @report.shift.update_attribute(:signed_in, false)
      if @add_payform_item 
        @payform_item = PayformItem.new(hours: @report.duration,
                                       category: (@report.shift.location.category || current_department.department_config.default_category),
                                       payform: Payform.build(@report.shift.location.loc_group.department, @report.user, Time.now),
                                       date: Date.today,
                                       description: @report.short_description,
                                       source_url: shift_report_path(@report.shift),
                                       source: current_user)
        UserMailer.delay.shift_report(@report.shift, @report, @report.shift.department)
        if @payform_item.save
          flash[:notice] = "Successfully submitted report and updated payform."
        else
          flash[:notice] = "Successfully submitted report, but payform did not update. Please manually add the job to your payform."
        end
      else
        flash[:error] = "That report has already been submitted."
      end
      respond_to do |format|
        format.html {redirect_to dashboard_path}
        format.js
      end
    else
      flash[:notice] = "Report not submitted.  You may not be the owner of this report."
      render action: 'show'
    end
  end
def update @report = Report . find ( params [ :id ] ) return unless user_is_owner_or_admin_of ( @report . shift , @report . shift . department ) || current_user . is_admin_of? ( @report . shift . location ) if ( params [ :sign_out ] and @report . departed . nil? ) @report . departed = Time . now @report . report_items << ReportItem . new ( time : Time . now , content : "#{current_user.name} (#{current_user.login}) logged out from #{request.remote_ip}" , ip_address : request . remote_ip ) @report . shift . update_attribute ( :end , Time . now ) unless @report . shift . scheduled? @add_payform_item = true end if @report . update_attributes ( params [ :report ] ) && @report . shift . update_attribute ( :signed_in , false ) if @add_payform_item @payform_item = PayformItem . new ( hours : @report . duration , category : ( @report . shift . location . category || current_department . department_config . default_category ) , payform : Payform . build ( @report . shift . location . loc_group . department , @report . user , Time . now ) , date : Date . today , description : @report . short_description , source_url : shift_report_path ( @report . shift ) , source : current_user ) UserMailer . delay . shift_report ( @report . shift , @report , @report . shift . department ) if @payform_item . save flash [ :notice ] = "Successfully submitted report and updated payform." else flash [ :notice ] = "Successfully submitted report, but payform did not update. Please manually add the job to your payform." end else flash [ :error ] = "That report has already been submitted." end respond_to do | format | format . html { redirect_to dashboard_path } format . js end else flash [ :notice ] = "Report not submitted.  You may not be the owner of this report." render action : 'show' end end
TODO: refactor into a model method on Report
Submitting a shift
TODO : refactor into a model method on Report Submitting a shift
#don't allow duplicate signout messages # above method call isn't safe; it works because there are never sub requests on unscheduled shifts, but it'll cause validation problems # with shift.adjust_sub_requests if it ever does run. -ben #don't allow duplicate payform items for a shift