如何在视图中获得所需的输出?

时间:2015-10-16 12:51:32

标签: ruby-on-rails ruby ruby-on-rails-3 controller

我需要一些帮助,我正在制定每周时间表。但我的输出没有显示所需的标准。一切都在显示,但它没有正确分配开始和结束时间,我无法弄清楚原因。我是红宝石的新手所以非常感谢任何帮助。下面是我的控件中的方法和视图中的输出。

 def generate_schedule
        logger.info @generate_schedules
    # create hash for view
        @generate_schedules = Hash.new
    # find current required schedule
        schedule_structure = Availability.where(:addressable_type      =>'CallCenter',:addressable_id => session[:employee].call_centers.first.id).all
    # find all available employee times
        employee_availability = Availability.where('priority > 0').where(:addressable_type => 'Employee', :addressable_id => session[:employee].call_centers.first.employees.collect(&:id)).all
    # create availability to requirement hash to determine order of days
        availability_to_requirement_hash = create_availability_to_requirement_hash(schedule_structure, employee_availability)
    # iterate through the hash day by day, a data is no use to us
        availability_to_requirement_hash.each do |day,a|
      # select the employees with availability for the chosen day, sort them by priority, and then iterate through them to clear the requirements
          employee_availability.select{|b| b.day_increment == day}.sort_by{|c| c.priority}.group_by(&:addressable).each do |employee, availabilities|
        # select the start time for the current day defined from requirement hash
          start_time = schedule_structure.select{|b| b.day_increment == day && b.priority > 0}.first.time_increment
        #define the length of the shift
          shift_length = employee.length_of_shift > availabilities.length ? availabilities.length : employee.length_of_shift
        #define the end time for the shift
          end_time = start_time + shift_length
          logger.info end_time
        #check if employee already defined in hash, if present add the shift as   an array of date, start time and end time, if not then add employee to hash with value as array
          @generate_schedules.has_key?(employee) ? @generate_schedules[employee] << [day, start_time, end_time] : @generate_schedules[employee] = [[day, start_time, end_time]]
          logger.info schedule_structure
        #update the schedule structure to reflect the time removed // do not change database
          (1..shift_length).each do |d|
            schedule_structure.select{|e| e.day_increment == day && e.priority > 0}.first.priority -= 1
            end
          end
        end
      end

以下是我的输出示例:

Example of View

0 个答案:

没有答案