This repository has been archived on 2020-01-20. You can view files and clone it, but cannot push or open issues or pull requests.
evoresa/app/models/item.rb
François Vaux 8d3dad9b8c base de l'application
* Affichage des ressources
* Affichage des évènements
* Affichage des réservations
* Partie d'administration protégée
2010-07-23 16:35:50 +02:00

40 lines
888 B
Ruby

class Item < ActiveRecord::Base
has_many :events
has_many :bookings, :through => :events
validates_presence_of :name, :email
before_create :regenerate_key
def self.find_by_key(*args)
super(*args) or raise NotFound
end
class NotFound < StandardError; end
# Create a random string (128 ^ 32 possibilities)
def regenerate_key
string = Array.new(32) { rand(128).chr }.join
self.key = Digest::MD5.hexdigest(string)
end
def upcoming_events
self.events.map(&:upcoming_bookings).flatten.map(&:event)
end
def bookings_for(date)
bookings_between(date, date)
end
def bookings_between(lower, upper)
bookings_range(lower.at_midnight, 1.day.since(upper).at_midnight - 1)
end
private
def bookings_range(lower, upper)
self.bookings.find :all,
:conditions => ['start_at >= ? AND end_at <= ?', lower, upper]
end
end