Hot Glue Example #5 – Validation

Example 5: Validation

This example is simpler than the last. We’ll create a Gd controller (an admin controller) for just one table: Events.

On this Events table, we’ll add some special validations — rules for our business logic — and we’ll see that Hot Glue’s scaffold seamlessly re-displays forms with invalid fields shown in red by default.

This feature alone makes Hot Glue a powerful tool in building fault-tolerate apps that don’t frustrate users. How many times do you encounter web interfaces that give you an error but won’t show you where the error is on the page? Hot Glue makes such low-quality interfaces a thing of the past.

Start with

rails _6.1.4.1_ new EventApp --database=postgresql

Then go through Steps 1-7 of the Setup steps.

In Step 5, I will install Hot Glue using the Gmail/Google-like theme:

rails generate hot_glue:install --markup=erb --theme=like_mountain_view

Now let’s generate our model:

rails generate model event name:string start_at:datetime end_at:datetime promote_on:datetime

Our system is going to enforce these rules:

• The end date can’t be before the start_date.

• Our promotion system, which will make our event public, can’t have a promote_on date that’s after the start_date

If you aren’t familiar with doing this in Rails, head on over to the Rails Guide for Validations and familiarize yourself with that now.

Add the code shown in orange below.

class Event < ApplicationRecord
  validates_presence_of :name
  validate :start_at_before_end_at, if: -> {!start_at.nil? && !end_at.nil?}
  validate :promote_on_before_start_at, if: -> {!promote_on.nil? && !start_at.nil?}

  def start_at_before_end_at
    if end_at < start_at
      errors.add(:start_at, "can't be after end at")
      errors.add(:end_at, "can't be before start at")
    end
  end

  def promote_on_before_start_at
    if start_at < promote_on
      errors.add(:promote_on, "can't be after start at")
    end
  end
end

Since Hot Glue detects validation at run-time and not at code generation time (like relationships and field definitions), you can add the validations before or after you generate your code.

Now lets generate a scaffold.

rails generate hot_glue:scaffold Event --gd

NOTE: This is where Hot Glue examines your Event object and also the events table the database. Hot Glue can only build scaffolding if 1) Your event object exists and 2) you have migrated your database. If it can’t find the Ruby object it will crash with this error:

`rescue in initialize’: *** Oops: It looks like there is no object for Event. Please define the object + database table first. (HotGlue::Error)

Note that it may also crash with this error if the Event class can’t be loaded, for example, if you have a syntax error or a parse error in your app/models/event.rb file. If you see this error, be sure your objects load OK on the rails console and in your specs.

Now define a route:

Rails.application.routes.draw do
  # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html

  resources :events
end

Start up your server with ./bin/dev and go to http://localhost:3000/events

Notice that Hot Glue automatically redisplays your forms with the invalid fields (name, start_date, end_date, and promote_on) shown in red when they don’t match the validation rules on the model.