views:

67

answers:

1

Well I don't actually want to make a calendar but I need a view for each day of the year which I guess is sort of the same. Say I have a view where you see e.g. "July 1st" in the top of the page and you have links to the day before and the day after. Beneath this there is a list of - in my example - rooms and they have different states - either available or reserved.

How could I make something like this using Sinatra and Datamapper? Do I put it in the url or what possibilities do I have?

get '/rooms/:date' do
  "List of rooms for " + params[:date]
end

So to recap. I'm trying to create a calendar where for each day you get a list of something that has a state of either available or reserved for every day of the year - but I don't know where to start.

A: 

I'd start with database design. What are your central tables? How do they incorporate temporal data? Let's say you have list of rooms. Then you would have to have another table (say room_reservations), each record having start_date, end_date and room_id. A query looking for a list of rooms occupied on a certain date should be trivial.

As for implementing date-centric view in Sinatra, it can be as simple as:

require 'rubygems'
require 'sinatra'
require 'date'
require 'haml'

get '/' do
  redirect "/rooms/#{Date.today}"
end

get '/rooms/:date' do |d|
  @date = Date.parse d
  haml :rooms
end

__END__

@@rooms
%a{:href => "/rooms/#{@date - 1}"}
  =@date-1
%span
  =@date
%a{:href => "/rooms/#{@date + 1}"}
  =@date+1
Mladen Jablanović
If I were to model a motel it would have a name and many guests and many sections. Each section (A, B, C etc.) has many rooms (1, 2, 3, etc.) and I guess each room has a reservation and a reservation has one or more guests. During the guest's stay at the motel it should be possible for the staff to move the guest to another room, so I'm not sure start_date and end_date is the right way to go.
theory
Well, it all depends on how complex you want to build your app, and whether you plan to start complex right away, or start simple, and make it complex over iterations (versions etc). In any case, what you should do first is 1) design your database 2) design your interface workflow. Ruby coding and Sinatra routing should come at the very end.
Mladen Jablanović
I thought about making it pretty simple to begin with. It could be a table called rooms belonging to a section. The rooms can of course have multiple reservation but only one for a given date. Or maybe it's better to link the reservations to multiple rooms. I guess each reservation would have a start date and an end date but I still want to be able to link one reservation to different rooms in the days between the start date and the end date.
theory
When you want to move an occupant from a room A to a room B, you can simply change `date_to` field in A room reservation to the current date, and open another reservation for room B _from current date_ to the original `date_to`. Anyway, this discussion is pretty far from the original question, you can open a new one about DB design.
Mladen Jablanović
I actually wanted to ask how to model a motel - but I thought I might be able to figure some stuff out myself if I could just get around this "calendar" problem. But you say it's trivial. Could you give a hint at how trivial it is?
theory
I mean how trivial is i to make a view and a navigation for each day of the year?
theory
As trivial as you like. I have added a trivial implementation in the answer.
Mladen Jablanović
Well that is fairly easy :) Thanks.
theory