views:

23

answers:

1

I'm struggling with how to model a resource and its availability using the tools, gems or plugins available to us in Rails.

Basically each resource will have a typical availability of 0..24 hours for each day (0,1,2,3,4,5,6) of the week. BUT then there will be exceptions that need to be considered for example a holiday or special event that should overrule the default for that day of the week.

I can think of two broad approaches (though I'm sure there are more). One which would query the DB on every request and test the logic to see if the resource is available at that moment. This may become cumbersome (time consuming) as system grows. The other would run a process (cron?) every x (5?) min to set a boolean (AVAILABLE) on each resource that is derived depending on the rules for its availability.

Those are broad approaches that could, conceivably, work but how to approach the model/classes is where I could really use some advice.

Any thoughts? Experience? Gems/Plugins I should be aware of?

A: 

Not sure of the applicability in your application but there is a pretty nice Ruby gem called ice_cube with can be used to model recurring dates and times. You could set up a schedule for your resources specifying their availability rules and the exception cases. The schedule can be serialized to the database as YAML (say in a text field) for each resource.

When you need to see if a particular resource is available you just pull it's schedule de-serialized it and ask ice_cube if a date/time in question intersects with it's rules or not. It's very fast and efficient too.

bjg