Skip to content

Commit

Permalink
specs anpassen
Browse files Browse the repository at this point in the history
  • Loading branch information
Widu Wittekindt committed Mar 14, 2012
1 parent d9c8ef9 commit 3e21a26
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 8 deletions.
9 changes: 9 additions & 0 deletions app/models/booking.rb
Expand Up @@ -9,6 +9,7 @@ class Booking < ActiveRecord::Base
validates_numericality_of :machine_id, :only_integer => true
validates_presence_of :starts_at, :ends_at
validate :not_to_long
validate :not_to_short
validate :end_after_start
validate :no_overlaps
validate :non_optional_options
Expand Down Expand Up @@ -188,6 +189,14 @@ def not_to_long
end
end

def not_to_short
if machine && machine.min_duration
duration = ends_at - starts_at
text = I18n.t('human_time_units.' + machine.min_booking_time_unit, :count => machine.min_booking_time)
errors.add(:ends_at, :to_short, :min => text) if duration < machine.min_duration
end
end

def non_optional_options
return unless machine
machine.options.group_by(&:option_group).each do |group, opts|
Expand Down
1 change: 1 addition & 0 deletions config/locales/active_record.de.yml
Expand Up @@ -70,6 +70,7 @@ de:
attributes:
ends_at:
to_long: "Buchung ist zu lang (Maximal: %{max})"
to_short: "Buchung ist zu kurz (Minimal: %{min})"
base:
to_many: "Aus der Gruppe '%{name}' darf nur eine Option ausgewählt sein"
one_necessary: "Eine Option aus der Gruppe '%{name}' muss ausgewählt sein"
Expand Down
11 changes: 9 additions & 2 deletions spec/models/booking_spec.rb
Expand Up @@ -89,7 +89,7 @@

describe "next and previous booking" do
before :each do
@booking1 = FactoryGirl.create(:booking, :starts_at => "2011-12-01 00:00:00", :ends_at => "2011-12-01 00:02:00")
@booking1 = FactoryGirl.create(:booking, :starts_at => "2011-12-01 00:00:00", :ends_at => "2011-12-02 00:02:00")
@booking2 = FactoryGirl.create(:booking, :starts_at => "2011-12-01 00:02:00", :ends_at => "2011-12-01 00:05:00")
@booking3 = FactoryGirl.create(:booking, :starts_at => "2011-12-03 00:02:02", :ends_at => "2011-12-04 00:03:00", :machine => @booking1.machine)
end
Expand Down Expand Up @@ -128,13 +128,20 @@
it {should validate_presence_of(:ends_at)}
it {should validate_presence_of(:starts_at)}
it {should_not accept_values_for(:ends_at, booking.starts_at - 1.minute, booking.starts_at - 1.day)}
it "does not accept bookings exceeding the maximum" do
it "does not accept bookings exceeding the maximum duration" do
@machine = FactoryGirl.create(:machine, :max_duration => 2, :max_duration_unit => 'day')
@now = DateTime.now
@booking = FactoryGirl.build(:booking, :machine => @machine, :starts_at => @now, :ends_at => @now + @machine.real_max_duration + 1.minute)
@booking.should_not be_valid
end

it "does not accept bookings shorter than the minimum duration" do
@machine = FactoryGirl.create(:machine, :min_booking_time => 2, :min_booking_time_unit => 'hour')
@now = DateTime.now
@booking = FactoryGirl.build(:booking, :machine => @machine, :starts_at => @now, :ends_at => @now + @machine.min_duration - 1.minutes)
@booking.should_not be_valid
end

describe "bookings may not overlap" do
before :each do
@now = Time.now.beginning_of_day
Expand Down
13 changes: 7 additions & 6 deletions spec/models/calendar_spec.rb
Expand Up @@ -36,12 +36,13 @@
before :each do
first_day = @calendar.days.first.to_datetime
last_day = @calendar.days.last.to_datetime
@to_early_booking = FactoryGirl.create(:booking, :starts_at => first_day - 1.day, :ends_at => first_day - 1.day + 1.second)
@started_booking = FactoryGirl.create(:booking, :starts_at => first_day - 3.days, :ends_at => first_day + 1.minute)
@today_booking = FactoryGirl.create(:booking, :starts_at => first_day, :ends_at => first_day + 1.second)
@future_booking = FactoryGirl.create(:booking, :starts_at => last_day, :ends_at => last_day + 1.second)
@future_booking2 = FactoryGirl.create(:booking, :starts_at => last_day - 2.days, :ends_at => last_day + 2.days)
@to_late_booking = FactoryGirl.create(:booking, :starts_at => last_day + 1.day, :ends_at => last_day + 1.day + 1.second)
@machines = FactoryGirl.create_list(:machine, 6, :min_booking_time => 1, :min_booking_time_unit => 'minute')
@to_early_booking = FactoryGirl.create(:booking, :machine => @machines[0], :starts_at => first_day - 1.day, :ends_at => first_day - 1.day + 1.minute)
@started_booking = FactoryGirl.create(:booking, :machine => @machines[1], :starts_at => first_day - 3.days, :ends_at => first_day + 1.minute)
@today_booking = FactoryGirl.create(:booking, :machine => @machines[2], :starts_at => first_day, :ends_at => first_day + 1.minute)
@future_booking = FactoryGirl.create(:booking, :machine => @machines[3], :starts_at => last_day, :ends_at => last_day + 1.minute)
@future_booking2 = FactoryGirl.create(:booking, :machine => @machines[4], :starts_at => last_day - 2.days, :ends_at => last_day + 2.days)
@to_late_booking = FactoryGirl.create(:booking, :machine => @machines[5], :starts_at => last_day + 1.day, :ends_at => last_day + 1.day + 1.minute)
@calendar = Calendar.new
end

Expand Down
2 changes: 2 additions & 0 deletions test/factories/machines.rb
Expand Up @@ -5,5 +5,7 @@
sequence(:name){|n| "Name#{n}"}
max_duration ""
description "Text"
min_booking_time 1
min_booking_time_unit 'hour'
end
end

0 comments on commit 3e21a26

Please sign in to comment.