Skip to content

Instantly share code, notes, and snippets.

@sidonath
Last active August 29, 2015 14:02
Show Gist options
  • Save sidonath/6d8bff4a444bf058c178 to your computer and use it in GitHub Desktop.
Save sidonath/6d8bff4a444bf058c178 to your computer and use it in GitHub Desktop.
class Create
include Lotus::Action
expose :booking, :errors
def call(params)
@booking = Booking.new
# BookingForm validates datetime entered and converts the values into a single Time object
form = BookingForm.new(booking)
# Syncs the form values to the entity
form.sync(params)
# The contract validates that the booking is possible (no overlaps with other bookings)
contract = BookingContract.new(booking)
conract.validate
@errors = Errors.new(form, contract)
if errors.empty?
BookingRepository.save(booking)
else
throw 422
end
end
end
<form action="/bookings" method="post">
<% errors.each do |error| %>
<%= error %>
<% end %>
<label>When do you want to book a room?</label>
<select name="booked_for[day]">...</select>
<select name="booked_for[month]">...</select>
<select name="booked_for[year]">...</select>
<select name="booked_for[hour]">...</select>
<select name="booked_for[minute]">...</select>
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment