Tasks were appearing out of order. On the shift occurrence show page the tasks are listed by calling occurrence.tasks.each which sorts them by event_tasks.order. That's good! But when editing, the tasks were listed with Task.where(id: event_params.fetch(:task_ids, []) + @occurrence.task_ids). Because not all the tasks are saved in the database, we can't take the event_tasks.order into account.
To solve this is a bit complex, but what we do is:
- If there are no
task_idin the params, use what's in the database, inevent_tasks.order. But if there aretask_idin the params, we can use the tasks there as they represent all the tasks we want on the event in the correct order. - When fetching the tasks for the edit page, we use rails'
in_order_ofmethod to ensure they are sorted. - Because they are saved in that order the show page will order them correctly.
Tasks were appearing out of order. On the shift occurrence show page the tasks are listed by calling `occurrence.tasks.each` which sorts them by `event_tasks.order`. That's good! But when editing, the tasks _were_ listed with `Task.where(id: event_params.fetch(:task_ids, []) + @occurrence.task_ids)`. Because not all the tasks are saved in the database, we can't take the `event_tasks.order` into account.
To solve this is a bit complex, but what we do is:
- If there are no `task_id` in the params, use what's in the database, in `event_tasks.order`. But if there are `task_id` in the params, we can use the tasks there as they represent all the tasks we want on the event in the correct order.
- When fetching the tasks for the edit page, we use rails' `in_order_of` method to ensure they are sorted.
- Because they are saved in that order the show page will order them correctly.