Я бы начал с отдельного метода для каждого из ваших элементов в @exception_sheet:
def parse_spreadsheet
@exception_sheet.each_with_index 4 do |row, index|
handle_exception_row(row, index)
end
end
def handle_exception_row(row, index)
employee_no = row[1]
department = row[2]
date = row[3].to_datetime
time_in = row[4].to_datetime unless row[4].nil?
time_out = row[5].to_datetime unless row[5].nil?
if department == "Night"
next_row = @exception_sheet.to_a[index + 5]
next_date = next_row[3].to_datetime
next_time_in = next_row[4].to_datetime unless next_row[4].nil?
start_time = set_date_time(date, time_out) unless time_out.nil?
end_time = set_date_time(next_date, next_time_in) unless next_time_in.nil?
else
start_time = set_date_time(date, time_in) unless time_in.nil?
end_time = set_date_time(date, time_out) unless time_out.nil?
end
@employee = Employee.find_by(employee_no: employee_no)
next if @employee.nil?
@attendance_item = AttendanceItem.create(
start_time: start_time, end_time: end_time, employee_id: @employee.id
)
end
end
Это исправляет метод parse_spreadsheet, но создает новые ошибки с handle_exception_row
. Затем уменьшите это:
def handle_exception_row(row, index)
employee_no = row[1]
department = row[2]
date = row[3].to_datetime
time_in = row[4].to_datetime unless row[4].nil?
time_out = row[5].to_datetime unless row[5].nil?
handle_attendance_creation(employee_no: employee_no, department: department, date: date, time_in: time_in, time_out: time_out)
end
def handle_attendance_creation(employee_no:, department:, date:, time_in:, time_out:)
if department == "Night"
next_row = @exception_sheet.to_a[index + 5]
next_date = next_row[3].to_datetime
next_time_in = next_row[4].to_datetime unless next_row[4].nil?
start_time = set_date_time(date, time_out) unless time_out.nil?
end_time = set_date_time(next_date, next_time_in) unless next_time_in.nil?
else
start_time = set_date_time(date, time_in) unless time_in.nil?
end_time = set_date_time(date, time_out) unless time_out.nil?
end
@employee = Employee.find_by(employee_no: employee_no)
next if @employee.nil?
@attendance_item = AttendanceItem.create(
start_time: start_time, end_time: end_time, employee_id: @employee.id
)
end
end
Вероятно, вы все равно получите сообщение об ошибке handle_attendance_creation
, но, надеюсь, вы увидите, куда это идет. Удалите еще более мелкие методы, которые принимают необходимые аргументы, и вы получите там