0

I am looking for advice on how to design a web app that shows a week view for a todo list.

The current layout is a table with days of the week as columns with items to be done listed below. I have gotten something functional by calculating the header dates in the controller with:

@week_starting = Date.today.at_beginning_of_week if @week_starting.blank?

Other weekday headers are shown by incrementing @week_starting.

I have created individual instance variables such as @to_do_on_mon, @to_do_on_tue, etc. to show what is to be done that day (and then iterate through them to list out the items). Here is an example of one:

@to_do_on_mon = BatchTicket.where("Date(load_date) =?", @week_starting.strftime

How would you improve on this design to easily increment/decrement the week shown and list out items to be done?

collenjones
  • 510
  • 5
  • 19
  • the only resolution you have is a week? if that is so, you can save the year, week number in that year and a day in a year. – Elad Meidar Jun 19 '13 at 09:03

1 Answers1

1

First of all, just a personal preference, but I think it would make the code nicer changing the first line to this:

@week_starting ||= Date.today.at_beginning_of_week

It's shorter and it's clearer.

I'm not sure I understand what you're trying to do with the Date(load_date) there, but since you have ActiveSupport available, I would probably do something more readable like this:

@to_do_on_mon = BatchTicket.where date: (@week_starting + 0.days)
@to_do_on_tue = BatchTicket.where date: (@week_starting + 1.days)
# etc.

Or, depending on what your page/app looks like, you can do it in one query if you need to:

BatchTicket.where('your date range').group_by { |b| b.date.strftime("%u") }

Will return an array grouped into days (1..7).

Hope that helps

Amir
  • 1,882
  • 17
  • 22
  • Good suggestion on ||=. Already done. As for the Date(load_date) bit, that's the ONLY means I've discovered to accurately locate my items by date (http://stackoverflow.com/questions/16682526/why-is-my-model-where-returning-a-blank-array-when-im-sure-there-are-matches). Any suggestions on how to update the week shown? – collenjones Jun 19 '13 at 07:34
  • Oh, I've missed that question. I'm not sure I understood what 'increment/decrement the week' means though - You would like to show a week view, and then paginate between weeks? – Amir Jun 19 '13 at 08:02
  • What I mean is that I have my current table that I calculate the headers using @week_starting and then increment it to show Tues, Wed, etc. I have two icons < > that I would like to show the previous / next week when clicked, meaning 'week_starting' would have to -/+ 7 days and then refresh the page. I'm also open to using AJAX but thought I would get this working first to learn how. – collenjones Jun 19 '13 at 14:04
  • You cuold use an ajax call to update the table's data (that you would probably store in a JSON on the initial call) - that would the faster approach. You could also use a normal GET request that will return all of the data. The advantage of that is that users could bookmark/share the link for a specific week. – Amir Jun 20 '13 at 06:03
  • Do you know of any links that would describe either of those approaches? Thanks! – collenjones Jun 21 '13 at 05:37