Skip to content

Commit

Permalink
ensure backwards compat works properly
Browse files Browse the repository at this point in the history
  • Loading branch information
stakach committed Feb 29, 2024
1 parent 9b85852 commit 28fed1c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
36 changes: 34 additions & 2 deletions spec/booking_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ module PlaceOS::Model
booking.asset_ids.should eq ["desk3", "desk4"]
booking.persisted?.should be_true

# mismatch
# mismatch 1
booking = Booking.new(
booking_type: "desk",
asset_id: "desk5",
Expand All @@ -104,7 +104,39 @@ module PlaceOS::Model
booked_by_id: "user-1234",
history: [] of Booking::History
).save!
booking.asset_ids.should eq ["desk5", "desk6", "desk7"]
booking.asset_ids.should eq ["desk6", "desk7"]
booking.asset_id.should eq "desk6"
booking.persisted?.should be_true

# mismatch 2
booking = Booking.new(
booking_type: "desk",
asset_ids: ["desk8"],
booking_start: 1.hour.from_now.to_unix,
booking_end: 2.hours.from_now.to_unix,
user_email: PlaceOS::Model::Email.new(user_email),
user_name: "Steve",
booked_by_email: PlaceOS::Model::Email.new(user_email),
booked_by_name: "Steve",
tenant_id: tenant_id,
booked_by_id: "user-1234",
history: [] of Booking::History
).save!
booking.asset_ids.should eq ["desk8"]
booking.asset_id.should eq "desk8"
booking.persisted?.should be_true

booking.asset_id = "desk9"
booking.save!
booking.asset_ids.should eq ["desk9"]
booking.asset_id.should eq "desk9"
booking.persisted?.should be_true

booking.asset_id = "desk10"
booking.asset_ids = ["desk11"]
booking.save!
booking.asset_ids.should eq ["desk11"]
booking.asset_id.should eq "desk11"
booking.persisted?.should be_true
end

Expand Down
9 changes: 5 additions & 4 deletions src/placeos-models/booking.cr
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,12 @@ module PlaceOS::Model
end

def update_assets
if (single_id = self.asset_id) && !self.asset_ids.includes?(single_id)
self.asset_ids = [single_id] + self.asset_ids
@asset_ids_changed = true
if asset_ids.size == 1 && !@asset_ids_changed && @asset_id_changed
asset_ids[0] = asset_id
elsif asset_ids.empty?
asset_ids.insert(0, asset_id)
end
self.asset_id = self.asset_ids.first
@asset_id = asset_ids.first
end

def asset_ids=(vals : Array(String))
Expand Down

0 comments on commit 28fed1c

Please sign in to comment.