Closed J3RN closed 7 years ago
Could you do:
todos = all.sort_by! { |x| -x.priority }
today = todos.reject { |x| x.time_remaining_today.zero? }
not_today = todos.select { |x| x.time_remaining_today.zero? }
today + not_today
@3ygun Thanks! That's definitely DRYer, isn't it?
I'm holding out on a purely database solution to this. What I'd love is if there was some sort of computed column, time_remaining_today. I'm not sure if that's even possible or pragmatic, but it's something I'd definitely like to look into.
UPDATE: It looks like I'd have to use a view or a function. There's some pragmatic issue with this, namely that the logic for the application would be split between the Rails application and the database. What I have seen elsewhere is people putting giant SQL queries in their models, but that hardly seems pragmatic either. I'll look into this more when I have time.
I've decided that your refactor is good enough for now, and I can dig deeper if I need to or have extra time.
This implementation is inelegant, and I would appreciate it if anyone could suggest a better one.