app/controllers/employees_controller.rb
def update
    total_service_allocation = 0.0
    total_product_allocation = 0.0
    # If a new group was sent with the params, adds it to the employee's list of groups
    if params[:employee_group]
      unless params[:employee_group][:group_id].blank?
        Group.find(params[:employee_group][:group_id]).add_employee_to_group(@employee)
      end
    end
    # If a new service and allocation were sent with the params, adds them to the employee
    if params[:employee_allocations]
      unless (params[:employee_allocations][:service_id].blank?) ||
             (params[:employee_allocations][:allocation].blank?) ||
             (params[:employee_allocations][:fiscal_year_id].blank?)
        new_employee_allocation = @employee.employee_allocations.new(params[:employee_allocations])
        unless new_employee_allocation.save
          flash[:error] = t(:service) + t(:add) + t(:error)
          render :edit
          return
        end
        total_service_allocation += new_employee_allocation.allocation
      end
    end
    # If a new product and allocation were sent with the params, adds them to the employee
    if params[:employee_products]
      unless (params[:employee_products][:product_id].blank?) ||
             (params[:employee_products][:fiscal_year_id].blank?)
        new_employee_product = @employee.employee_products.new(params[:employee_products])
        unless new_employee_product.save
          flash[:error] = t(:product) + t(:add) + t(:error)
          render :edit
          return
        end
        allocation = new_employee_product.allocation
        total_product_allocation += allocation unless allocation.blank?
      end
    end
    # Calculates the total service allocation passed in params.
    if params["employee"] && params[:employee]["employee_allocations_attributes"]
      params["employee"]["employee_allocations_attributes"].to_a.each do |new_allocation|
        if new_allocation.last["_destroy"].to_f != 1
          total_service_allocation += new_allocation.last["allocation"].to_f
        end
      end
    end
    # Calculates the total product eallocation passed in params.
    if params["employee"] && params[:employee]["employee_products_attributes"]
      params["employee"]["employee_products_attributes"].to_a.each do |new_allocation|
        if new_allocation.last["_destroy"].to_f != 1 && !new_allocation.last["allocation"].blank?
          total_product_allocation += new_allocation.last["allocation"].to_f
        end
      end
    end
    # Validates that the total allocation passed in params does not exceed 1FTE. It is only possible
    # to over-allocate a user if javascript is disabled.
    if (total_service_allocation <= 1) && (total_product_allocation <=1) && (@employee.update_attributes(params[:employee]))        
      flash[:notice] = t(:employee) + t(:updated)      
      redirect_to edit_employee_path(@employee.id)
      return
    end
    flash[:error] = t(:over_allocated)
    render :edit
  end
# Updates an employee based on info entered on the "edit" page.
Updates an employee based on info entered on the "edit" page.
Updates an employee based on info entered on the " edit " page .
def update
    total_service_allocation = 0.0
    total_product_allocation = 0.0
    if params[:employee_group]
      unless params[:employee_group][:group_id].blank?
        Group.find(params[:employee_group][:group_id]).add_employee_to_group(@employee)
      end
    end
    if params[:employee_allocations]
      unless (params[:employee_allocations][:service_id].blank?) ||
             (params[:employee_allocations][:allocation].blank?) ||
             (params[:employee_allocations][:fiscal_year_id].blank?)
        new_employee_allocation = @employee.employee_allocations.new(params[:employee_allocations])
        unless new_employee_allocation.save
          flash[:error] = t(:service) + t(:add) + t(:error)
          render :edit
          return
        end
        total_service_allocation += new_employee_allocation.allocation
      end
    end
    if params[:employee_products]
      unless (params[:employee_products][:product_id].blank?) ||
             (params[:employee_products][:fiscal_year_id].blank?)
        new_employee_product = @employee.employee_products.new(params[:employee_products])
        unless new_employee_product.save
          flash[:error] = t(:product) + t(:add) + t(:error)
          render :edit
          return
        end
        allocation = new_employee_product.allocation
        total_product_allocation += allocation unless allocation.blank?
      end
    end
    if params["employee"] && params[:employee]["employee_allocations_attributes"]
      params["employee"]["employee_allocations_attributes"].to_a.each do |new_allocation|
        if new_allocation.last["_destroy"].to_f != 1
          total_service_allocation += new_allocation.last["allocation"].to_f
        end
      end
    end
    if params["employee"] && params[:employee]["employee_products_attributes"]
      params["employee"]["employee_products_attributes"].to_a.each do |new_allocation|
        if new_allocation.last["_destroy"].to_f != 1 && !new_allocation.last["allocation"].blank?
          total_product_allocation += new_allocation.last["allocation"].to_f
        end
      end
    end
    if (total_service_allocation <= 1) && (total_product_allocation <=1) && (@employee.update_attributes(params[:employee]))        
      flash[:notice] = t(:employee) + t(:updated)      
      redirect_to edit_employee_path(@employee.id)
      return
    end
    flash[:error] = t(:over_allocated)
    render :edit
  end
def update total_service_allocation = 0.0 total_product_allocation = 0.0 if params [ :employee_group ] unless params [ :employee_group ] [ :group_id ] . blank? Group . find ( params [ :employee_group ] [ :group_id ] ) . add_employee_to_group ( @employee ) end end if params [ :employee_allocations ] unless ( params [ :employee_allocations ] [ :service_id ] . blank? ) || ( params [ :employee_allocations ] [ :allocation ] . blank? ) || ( params [ :employee_allocations ] [ :fiscal_year_id ] . blank? ) new_employee_allocation = @employee . employee_allocations . new ( params [ :employee_allocations ] ) unless new_employee_allocation . save flash [ :error ] = t ( :service ) + t ( :add ) + t ( :error ) render :edit return end total_service_allocation += new_employee_allocation . allocation end end if params [ :employee_products ] unless ( params [ :employee_products ] [ :product_id ] . blank? ) || ( params [ :employee_products ] [ :fiscal_year_id ] . blank? ) new_employee_product = @employee . employee_products . new ( params [ :employee_products ] ) unless new_employee_product . save flash [ :error ] = t ( :product ) + t ( :add ) + t ( :error ) render :edit return end allocation = new_employee_product . allocation total_product_allocation += allocation unless allocation . blank? end end if params [ "employee" ] && params [ :employee ] [ "employee_allocations_attributes" ] params [ "employee" ] [ "employee_allocations_attributes" ] . to_a . each do | new_allocation | if new_allocation . last [ "_destroy" ] . to_f != 1 total_service_allocation += new_allocation . last [ "allocation" ] . to_f end end end if params [ "employee" ] && params [ :employee ] [ "employee_products_attributes" ] params [ "employee" ] [ "employee_products_attributes" ] . to_a . each do | new_allocation | if new_allocation . last [ "_destroy" ] . to_f != 1 && ! new_allocation . last [ "allocation" ] . blank? total_product_allocation += new_allocation . last [ "allocation" ] . to_f end end end if ( total_service_allocation <= 1 ) && ( total_product_allocation <= 1 ) && ( @employee . update_attributes ( params [ :employee ] ) ) flash [ :notice ] = t ( :employee ) + t ( :updated ) redirect_to edit_employee_path ( @employee . id ) return end flash [ :error ] = t ( :over_allocated ) render :edit end
Updates an employee based on info entered on the "edit" page.
Updates an employee based on info entered on the " edit " page .
# If a new group was sent with the params, adds it to the employee's list of groups # If a new service and allocation were sent with the params, adds them to the employee # If a new product and allocation were sent with the params, adds them to the employee # Calculates the total service allocation passed in params. # Calculates the total product eallocation passed in params. # Validates that the total allocation passed in params does not exceed 1FTE. It is only possible # to over-allocate a user if javascript is disabled.