brut 0.9.0 → 0.9.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +1 -1
- data/CHANGELOG.md +6 -0
- data/Gemfile.lock +75 -69
- data/brut-css/package-lock.json +2 -2
- data/brut-css/package.json +1 -1
- data/brut-js/package-lock.json +2 -2
- data/brut-js/package.json +1 -1
- data/brut-js/specs/ConfirmSubmit.spec.js +32 -1
- data/brut-js/src/ConfirmSubmit.js +28 -7
- data/brutrb.com/images/tutorial/basic-form-with-violations.png +0 -0
- data/brutrb.com/images/tutorial/basic-form.png +0 -0
- data/brutrb.com/images/tutorial/initial-home-page.png +0 -0
- data/brutrb.com/images/tutorial/new-post-editor.png +0 -0
- data/brutrb.com/images/tutorial/new-post-home-page.png +0 -0
- data/brutrb.com/images/tutorial/styled-form-with-server-side-violations.png +0 -0
- data/brutrb.com/images/tutorial/styled-form-with-violations.png +0 -0
- data/brutrb.com/images/tutorial/styled-home-page-with-posts.png +0 -0
- data/brutrb.com/images/tutorial/styled-home-page.png +0 -0
- data/brutrb.com/images/tutorial/welcome-to-brut.png +0 -0
- data/brutrb.com/tutorial.md +1728 -3
- data/docs/404.html +2 -2
- data/docs/adrs.html +3 -3
- data/docs/ai.html +3 -3
- data/docs/api/Brut/BackEnd/SeedData.html +1 -1
- data/docs/api/Brut/BackEnd/Sidekiq/Middlewares/Server/FlushSpans.html +1 -1
- data/docs/api/Brut/BackEnd/Sidekiq/Middlewares/Server.html +1 -1
- data/docs/api/Brut/BackEnd/Sidekiq/Middlewares.html +1 -1
- data/docs/api/Brut/BackEnd/Sidekiq.html +1 -1
- data/docs/api/Brut/BackEnd/Validators/FormValidator.html +1 -1
- data/docs/api/Brut/BackEnd/Validators.html +1 -1
- data/docs/api/Brut/BackEnd.html +1 -1
- data/docs/api/Brut/CLI/App.html +1 -1
- data/docs/api/Brut/CLI/AppRunner.html +1 -1
- data/docs/api/Brut/CLI/Apps/BuildAssets/All.html +1 -1
- data/docs/api/Brut/CLI/Apps/BuildAssets/CSS.html +1 -1
- data/docs/api/Brut/CLI/Apps/BuildAssets/Images.html +1 -1
- data/docs/api/Brut/CLI/Apps/BuildAssets/JS.html +1 -1
- data/docs/api/Brut/CLI/Apps/BuildAssets.html +1 -1
- data/docs/api/Brut/CLI/Apps/DB/Create.html +1 -1
- data/docs/api/Brut/CLI/Apps/DB/Drop.html +1 -1
- data/docs/api/Brut/CLI/Apps/DB/Migrate.html +1 -1
- data/docs/api/Brut/CLI/Apps/DB/NewMigration.html +1 -1
- data/docs/api/Brut/CLI/Apps/DB/Rebuild.html +1 -1
- data/docs/api/Brut/CLI/Apps/DB/Seed.html +1 -1
- data/docs/api/Brut/CLI/Apps/DB/Status.html +1 -1
- data/docs/api/Brut/CLI/Apps/DB.html +1 -1
- data/docs/api/Brut/CLI/Apps/DeployBase/GitChecks.html +1 -1
- data/docs/api/Brut/CLI/Apps/DeployBase.html +1 -1
- data/docs/api/Brut/CLI/Apps/HerokuContainerBasedDeploy/Deploy.html +1 -1
- data/docs/api/Brut/CLI/Apps/HerokuContainerBasedDeploy.html +1 -1
- data/docs/api/Brut/CLI/Apps/Scaffold/Action/Route.html +1 -1
- data/docs/api/Brut/CLI/Apps/Scaffold/Action.html +1 -1
- data/docs/api/Brut/CLI/Apps/Scaffold/Component.html +1 -1
- data/docs/api/Brut/CLI/Apps/Scaffold/CustomElementTest.html +1 -1
- data/docs/api/Brut/CLI/Apps/Scaffold/DbModel.html +1 -1
- data/docs/api/Brut/CLI/Apps/Scaffold/E2ETest.html +1 -1
- data/docs/api/Brut/CLI/Apps/Scaffold/Form.html +1 -1
- data/docs/api/Brut/CLI/Apps/Scaffold/Page/Route.html +1 -1
- data/docs/api/Brut/CLI/Apps/Scaffold/Page.html +1 -1
- data/docs/api/Brut/CLI/Apps/Scaffold/RoutesEditor.html +1 -1
- data/docs/api/Brut/CLI/Apps/Scaffold/Test.html +1 -1
- data/docs/api/Brut/CLI/Apps/Scaffold.html +1 -1
- data/docs/api/Brut/CLI/Apps/Test/Audit.html +1 -1
- data/docs/api/Brut/CLI/Apps/Test/E2e.html +1 -1
- data/docs/api/Brut/CLI/Apps/Test/JS.html +1 -1
- data/docs/api/Brut/CLI/Apps/Test/Run.html +1 -1
- data/docs/api/Brut/CLI/Apps/Test.html +1 -1
- data/docs/api/Brut/CLI/Apps.html +1 -1
- data/docs/api/Brut/CLI/Command.html +1 -1
- data/docs/api/Brut/CLI/Error.html +1 -1
- data/docs/api/Brut/CLI/ExecutionResults/Result.html +1 -1
- data/docs/api/Brut/CLI/ExecutionResults.html +1 -1
- data/docs/api/Brut/CLI/Executor.html +1 -1
- data/docs/api/Brut/CLI/InvalidOption.html +1 -1
- data/docs/api/Brut/CLI/Options.html +1 -1
- data/docs/api/Brut/CLI/Output.html +1 -1
- data/docs/api/Brut/CLI/SystemExecError.html +1 -1
- data/docs/api/Brut/CLI.html +1 -1
- data/docs/api/Brut/FactoryBot.html +1 -1
- data/docs/api/Brut/Framework/App.html +1 -1
- data/docs/api/Brut/Framework/Config.html +11 -21
- data/docs/api/Brut/Framework/Container.html +1 -1
- data/docs/api/Brut/Framework/Error.html +1 -1
- data/docs/api/Brut/Framework/Errors/AbstractMethod.html +1 -1
- data/docs/api/Brut/Framework/Errors/Bug.html +1 -1
- data/docs/api/Brut/Framework/Errors/MissingConfiguration.html +1 -1
- data/docs/api/Brut/Framework/Errors/MissingParameter.html +1 -1
- data/docs/api/Brut/Framework/Errors/NoClassForPath.html +1 -1
- data/docs/api/Brut/Framework/Errors/NotFound.html +1 -1
- data/docs/api/Brut/Framework/Errors/NotImplemented.html +1 -1
- data/docs/api/Brut/Framework/Errors.html +1 -1
- data/docs/api/Brut/Framework/FussyTypeEnforcement.html +1 -1
- data/docs/api/Brut/Framework/MCP.html +1 -1
- data/docs/api/Brut/Framework/ProjectEnvironment.html +1 -1
- data/docs/api/Brut/Framework.html +1 -1
- data/docs/api/Brut/FrontEnd/AssetPathResolver.html +1 -1
- data/docs/api/Brut/FrontEnd/Component/Helpers.html +1 -1
- data/docs/api/Brut/FrontEnd/Component.html +1 -1
- data/docs/api/Brut/FrontEnd/Components/ConstraintViolations.html +1 -1
- data/docs/api/Brut/FrontEnd/Components/FormTag.html +1 -1
- data/docs/api/Brut/FrontEnd/Components/I18nTranslations.html +1 -1
- data/docs/api/Brut/FrontEnd/Components/Input.html +1 -1
- data/docs/api/Brut/FrontEnd/Components/Inputs/CsrfToken.html +1 -1
- data/docs/api/Brut/FrontEnd/Components/Inputs/InputTag.html +1 -1
- data/docs/api/Brut/FrontEnd/Components/Inputs/RadioButton.html +1 -1
- data/docs/api/Brut/FrontEnd/Components/Inputs/SelectTagWithOptions.html +1 -1
- data/docs/api/Brut/FrontEnd/Components/Inputs/TextareaTag.html +1 -1
- data/docs/api/Brut/FrontEnd/Components/Inputs.html +1 -1
- data/docs/api/Brut/FrontEnd/Components/LocaleDetection.html +1 -1
- data/docs/api/Brut/FrontEnd/Components/PageIdentifier.html +1 -1
- data/docs/api/Brut/FrontEnd/Components/TimeTag.html +1 -1
- data/docs/api/Brut/FrontEnd/Components/Traceparent.html +1 -1
- data/docs/api/Brut/FrontEnd/Components.html +1 -1
- data/docs/api/Brut/FrontEnd/CsrfProtector.html +1 -1
- data/docs/api/Brut/FrontEnd/Download.html +1 -1
- data/docs/api/Brut/FrontEnd/Flash.html +1 -1
- data/docs/api/Brut/FrontEnd/Form.html +1 -1
- data/docs/api/Brut/FrontEnd/Forms/ConstraintViolation.html +1 -1
- data/docs/api/Brut/FrontEnd/Forms/Input/Color.html +1 -1
- data/docs/api/Brut/FrontEnd/Forms/Input/TimeOfDay.html +1 -1
- data/docs/api/Brut/FrontEnd/Forms/Input.html +1 -1
- data/docs/api/Brut/FrontEnd/Forms/InputDeclarations.html +1 -1
- data/docs/api/Brut/FrontEnd/Forms/InputDefinition.html +1 -1
- data/docs/api/Brut/FrontEnd/Forms/RadioButtonGroupInput.html +1 -1
- data/docs/api/Brut/FrontEnd/Forms/RadioButtonGroupInputDefinition.html +1 -1
- data/docs/api/Brut/FrontEnd/Forms/SelectInput.html +1 -1
- data/docs/api/Brut/FrontEnd/Forms/SelectInputDefinition.html +1 -1
- data/docs/api/Brut/FrontEnd/Forms/ValidityState.html +1 -1
- data/docs/api/Brut/FrontEnd/Forms.html +1 -1
- data/docs/api/Brut/FrontEnd/GenericResponse.html +1 -1
- data/docs/api/Brut/FrontEnd/Handler.html +1 -1
- data/docs/api/Brut/FrontEnd/Handlers/CspReportingHandler.html +1 -1
- data/docs/api/Brut/FrontEnd/Handlers/InstrumentationHandler/TraceParent.html +1 -1
- data/docs/api/Brut/FrontEnd/Handlers/InstrumentationHandler.html +1 -1
- data/docs/api/Brut/FrontEnd/Handlers/LocaleDetectionHandler.html +1 -1
- data/docs/api/Brut/FrontEnd/Handlers/MissingHandler/Form.html +1 -1
- data/docs/api/Brut/FrontEnd/Handlers/MissingHandler.html +1 -1
- data/docs/api/Brut/FrontEnd/Handlers.html +1 -1
- data/docs/api/Brut/FrontEnd/HandlingResults.html +1 -1
- data/docs/api/Brut/FrontEnd/HttpMethod.html +1 -1
- data/docs/api/Brut/FrontEnd/HttpStatus.html +1 -1
- data/docs/api/Brut/FrontEnd/InlineSvgLocator.html +1 -1
- data/docs/api/Brut/FrontEnd/Layout.html +1 -1
- data/docs/api/Brut/FrontEnd/Middleware.html +1 -1
- data/docs/api/Brut/FrontEnd/Middlewares/AnnotateBrutOwnedPaths.html +1 -1
- data/docs/api/Brut/FrontEnd/Middlewares/Favicon.html +1 -1
- data/docs/api/Brut/FrontEnd/Middlewares/OpenTelemetrySpan.html +1 -1
- data/docs/api/Brut/FrontEnd/Middlewares/ReloadApp.html +1 -1
- data/docs/api/Brut/FrontEnd/Middlewares.html +1 -1
- data/docs/api/Brut/FrontEnd/Page.html +1 -1
- data/docs/api/Brut/FrontEnd/Pages/MissingPage.html +1 -1
- data/docs/api/Brut/FrontEnd/Pages.html +1 -1
- data/docs/api/Brut/FrontEnd/RequestContext.html +1 -1
- data/docs/api/Brut/FrontEnd/RouteHook.html +1 -1
- data/docs/api/Brut/FrontEnd/RouteHooks/AgeFlash.html +1 -1
- data/docs/api/Brut/FrontEnd/RouteHooks/CSPNoInlineScripts.html +1 -1
- data/docs/api/Brut/FrontEnd/RouteHooks/CSPNoInlineStylesOrScripts/ReportOnly.html +1 -1
- data/docs/api/Brut/FrontEnd/RouteHooks/CSPNoInlineStylesOrScripts.html +1 -1
- data/docs/api/Brut/FrontEnd/RouteHooks/LocaleDetection.html +1 -1
- data/docs/api/Brut/FrontEnd/RouteHooks/SetupRequestContext.html +1 -1
- data/docs/api/Brut/FrontEnd/RouteHooks.html +1 -1
- data/docs/api/Brut/FrontEnd/Routing/FormHandlerRoute.html +1 -1
- data/docs/api/Brut/FrontEnd/Routing/FormRoute.html +1 -1
- data/docs/api/Brut/FrontEnd/Routing/MissingForm.html +1 -1
- data/docs/api/Brut/FrontEnd/Routing/MissingHandler.html +1 -1
- data/docs/api/Brut/FrontEnd/Routing/MissingPage.html +1 -1
- data/docs/api/Brut/FrontEnd/Routing/MissingPath.html +1 -1
- data/docs/api/Brut/FrontEnd/Routing/PageRoute.html +1 -1
- data/docs/api/Brut/FrontEnd/Routing/Route.html +1 -1
- data/docs/api/Brut/FrontEnd/Routing.html +1 -1
- data/docs/api/Brut/FrontEnd/Session.html +1 -1
- data/docs/api/Brut/FrontEnd.html +1 -1
- data/docs/api/Brut/I18n/BaseMethods.html +1 -1
- data/docs/api/Brut/I18n/ForBackEnd.html +1 -1
- data/docs/api/Brut/I18n/ForCLI.html +1 -1
- data/docs/api/Brut/I18n/ForHTML.html +1 -1
- data/docs/api/Brut/I18n/HTTPAcceptLanguage/AlwaysEnglish.html +1 -1
- data/docs/api/Brut/I18n/HTTPAcceptLanguage.html +1 -1
- data/docs/api/Brut/I18n.html +1 -1
- data/docs/api/Brut/Instrumentation/LoggerSpanExporter.html +1 -1
- data/docs/api/Brut/Instrumentation/OpenTelemetry/NormalizedAttributes.html +1 -1
- data/docs/api/Brut/Instrumentation/OpenTelemetry/Span.html +1 -1
- data/docs/api/Brut/Instrumentation/OpenTelemetry.html +1 -1
- data/docs/api/Brut/Instrumentation.html +1 -1
- data/docs/api/Brut/RubocopConfig.html +1 -1
- data/docs/api/Brut/SinatraHelpers/ClassMethods.html +1 -1
- data/docs/api/Brut/SinatraHelpers.html +1 -1
- data/docs/api/Brut/SpecSupport/ClockSupport.html +1 -1
- data/docs/api/Brut/SpecSupport/ComponentSupport.html +1 -1
- data/docs/api/Brut/SpecSupport/E2ETestServer.html +1 -1
- data/docs/api/Brut/SpecSupport/E2eSupport.html +1 -1
- data/docs/api/Brut/SpecSupport/EnhancedNode.html +1 -1
- data/docs/api/Brut/SpecSupport/FlashSupport.html +1 -1
- data/docs/api/Brut/SpecSupport/GeneralSupport/ClassMethods.html +1 -1
- data/docs/api/Brut/SpecSupport/GeneralSupport.html +1 -1
- data/docs/api/Brut/SpecSupport/HandlerSupport.html +1 -1
- data/docs/api/Brut/SpecSupport/Matchers/BeABug.html +1 -1
- data/docs/api/Brut/SpecSupport/Matchers/BePageFor.html +1 -1
- data/docs/api/Brut/SpecSupport/Matchers/BeRoutingFor.html +1 -1
- data/docs/api/Brut/SpecSupport/Matchers/HaveConstraintViolation.html +1 -1
- data/docs/api/Brut/SpecSupport/Matchers/HaveGenerated.html +1 -1
- data/docs/api/Brut/SpecSupport/Matchers/HaveHTMLAttribute.html +1 -1
- data/docs/api/Brut/SpecSupport/Matchers/HaveI18nString.html +1 -1
- data/docs/api/Brut/SpecSupport/Matchers/HaveLinkTo.html +1 -1
- data/docs/api/Brut/SpecSupport/Matchers/HaveRedirectedTo.html +1 -1
- data/docs/api/Brut/SpecSupport/Matchers/HaveReturnedHttpStatus.html +1 -1
- data/docs/api/Brut/SpecSupport/Matchers/HaveReturnedRackResponse.html +1 -1
- data/docs/api/Brut/SpecSupport/Matchers.html +1 -1
- data/docs/api/Brut/SpecSupport/RSpecSetup/OptionalSidekiqSupport.html +1 -1
- data/docs/api/Brut/SpecSupport/RSpecSetup.html +1 -1
- data/docs/api/Brut/SpecSupport/SessionSupport.html +1 -1
- data/docs/api/Brut/SpecSupport.html +1 -1
- data/docs/api/Brut.html +1 -1
- data/docs/api/Clock.html +1 -1
- data/docs/api/ModuleName.html +1 -1
- data/docs/api/RichString.html +1 -1
- data/docs/api/SemanticLogger/Appender/Async.html +1 -1
- data/docs/api/Sequel/Extensions/BrutInstrumentation.html +1 -1
- data/docs/api/Sequel/Extensions/BrutMigrations.html +1 -1
- data/docs/api/Sequel/Extensions.html +1 -1
- data/docs/api/Sequel/Plugins/CreatedAt/InstanceMethods.html +1 -1
- data/docs/api/Sequel/Plugins/CreatedAt.html +1 -1
- data/docs/api/Sequel/Plugins/ExternalId/ClassMethods.html +1 -1
- data/docs/api/Sequel/Plugins/ExternalId/InstanceMethods.html +1 -1
- data/docs/api/Sequel/Plugins/ExternalId.html +1 -1
- data/docs/api/Sequel/Plugins/FindBang/ClassMethods.html +1 -1
- data/docs/api/Sequel/Plugins/FindBang.html +1 -1
- data/docs/api/Sequel/Plugins.html +1 -1
- data/docs/api/Sequel.html +1 -1
- data/docs/api/_index.html +1 -1
- data/docs/api/file.README.html +1 -1
- data/docs/api/index.html +1 -1
- data/docs/api/top-level-namespace.html +1 -1
- data/docs/assets/{app.CmkhEsw7.js → app.BuBdZoUg.js} +1 -1
- data/docs/assets/basic-form-with-violations.Cv6Y9-Q_.png +0 -0
- data/docs/assets/basic-form.DbHnu0oW.png +0 -0
- data/docs/assets/chunks/@localSearchIndexroot.BZ_a3X0T.js +1 -0
- data/docs/assets/chunks/{VPLocalSearchBox.CDh583Oj.js → VPLocalSearchBox.UtnyvkX-.js} +1 -1
- data/docs/assets/chunks/{theme.B0_7GeW8.js → theme.DqwvuBEK.js} +2 -2
- data/docs/assets/{components.md.-22ecBxM.js → components.md.Bu80E2Nr.js} +3 -3
- data/docs/assets/{configuration.md.CLKL1lFf.js → configuration.md.CuIxVsSf.js} +1 -1
- data/docs/assets/{forms.md.CBI5--o2.js → forms.md.DnLbzVDa.js} +1 -1
- data/docs/assets/{getting-started.md.D5nzBZ4J.js → getting-started.md.DdQLmU3C.js} +2 -2
- data/docs/assets/initial-home-page.DNIaYmgP.png +0 -0
- data/docs/assets/new-post-editor.DrHr-5oh.png +0 -0
- data/docs/assets/new-post-home-page.Bm34lyMg.png +0 -0
- data/docs/assets/styled-form-with-server-side-violations.Bjxd8Dpv.png +0 -0
- data/docs/assets/styled-form-with-violations.Bv_sa9tg.png +0 -0
- data/docs/assets/styled-home-page-with-posts.Dd4kG89D.png +0 -0
- data/docs/assets/styled-home-page.BzdI7dWz.png +0 -0
- data/docs/assets/tutorial.md.C4zR5XPG.js +728 -0
- data/docs/assets/tutorial.md.C4zR5XPG.lean.js +1 -0
- data/docs/assets/welcome-to-brut.VSWzl17-.png +0 -0
- data/docs/assets.html +3 -3
- data/docs/brut-js/api/AjaxSubmit.html +1 -1
- data/docs/brut-js/api/AjaxSubmit.js.html +1 -1
- data/docs/brut-js/api/Autosubmit.html +1 -1
- data/docs/brut-js/api/Autosubmit.js.html +1 -1
- data/docs/brut-js/api/BaseCustomElement.html +1 -1
- data/docs/brut-js/api/BaseCustomElement.js.html +1 -1
- data/docs/brut-js/api/BrutCustomElements.html +1 -1
- data/docs/brut-js/api/BufferedLogger.html +1 -1
- data/docs/brut-js/api/ConfirmSubmit.html +18 -10
- data/docs/brut-js/api/ConfirmSubmit.js.html +29 -8
- data/docs/brut-js/api/ConfirmationDialog.html +1 -1
- data/docs/brut-js/api/ConfirmationDialog.js.html +1 -1
- data/docs/brut-js/api/ConstraintViolationMessage.html +1 -1
- data/docs/brut-js/api/ConstraintViolationMessage.js.html +1 -1
- data/docs/brut-js/api/ConstraintViolationMessages.html +1 -1
- data/docs/brut-js/api/ConstraintViolationMessages.js.html +1 -1
- data/docs/brut-js/api/CopyToClipboard.html +1 -1
- data/docs/brut-js/api/CopyToClipboard.js.html +1 -1
- data/docs/brut-js/api/Form.html +1 -1
- data/docs/brut-js/api/Form.js.html +1 -1
- data/docs/brut-js/api/I18nTranslation.html +1 -1
- data/docs/brut-js/api/I18nTranslation.js.html +1 -1
- data/docs/brut-js/api/LocaleDetection.html +1 -1
- data/docs/brut-js/api/LocaleDetection.js.html +1 -1
- data/docs/brut-js/api/Logger.html +1 -1
- data/docs/brut-js/api/Logger.js.html +1 -1
- data/docs/brut-js/api/Message.html +1 -1
- data/docs/brut-js/api/Message.js.html +1 -1
- data/docs/brut-js/api/PrefixedLogger.html +1 -1
- data/docs/brut-js/api/RichString.html +1 -1
- data/docs/brut-js/api/RichString.js.html +1 -1
- data/docs/brut-js/api/Tabs.html +1 -1
- data/docs/brut-js/api/Tabs.js.html +1 -1
- data/docs/brut-js/api/Tracing.html +1 -1
- data/docs/brut-js/api/Tracing.js.html +1 -1
- data/docs/brut-js/api/external-CustomElementRegistry.html +1 -1
- data/docs/brut-js/api/external-Performance.html +1 -1
- data/docs/brut-js/api/external-Promise.html +1 -1
- data/docs/brut-js/api/external-ValidityState.html +1 -1
- data/docs/brut-js/api/external-Window.html +1 -1
- data/docs/brut-js/api/external-fetch.html +1 -1
- data/docs/brut-js/api/global.html +1 -1
- data/docs/brut-js/api/index.html +1 -1
- data/docs/brut-js/api/index.js.html +1 -1
- data/docs/brut-js/api/module-testing.html +1 -1
- data/docs/brut-js/api/testing.AssetMetadata.html +1 -1
- data/docs/brut-js/api/testing.AssetMetadataLoader.html +1 -1
- data/docs/brut-js/api/testing.CustomElementTest.html +1 -1
- data/docs/brut-js/api/testing.DOMCreator.html +1 -1
- data/docs/brut-js/api/testing_AssetMetadata.js.html +1 -1
- data/docs/brut-js/api/testing_AssetMetadataLoader.js.html +1 -1
- data/docs/brut-js/api/testing_CustomElementTest.js.html +1 -1
- data/docs/brut-js/api/testing_DOMCreator.js.html +1 -1
- data/docs/brut-js/api/testing_index.js.html +1 -1
- data/docs/brut-js.html +3 -3
- data/docs/business-logic.html +3 -3
- data/docs/cli.html +3 -3
- data/docs/components.html +7 -7
- data/docs/configuration.html +5 -5
- data/docs/css.html +3 -3
- data/docs/custom-element-tests.html +3 -3
- data/docs/database-access.html +3 -3
- data/docs/database-schema.html +3 -3
- data/docs/deployment.html +3 -3
- data/docs/dev-environment.html +3 -3
- data/docs/dir-structure.html +3 -3
- data/docs/doc-conventions.html +3 -3
- data/docs/end-to-end-tests.html +3 -3
- data/docs/features.html +3 -3
- data/docs/flash-and-session.html +3 -3
- data/docs/form-constraints.html +3 -3
- data/docs/forms.html +5 -5
- data/docs/getting-started.html +6 -6
- data/docs/handlers.html +3 -3
- data/docs/hashmap.json +1 -1
- data/docs/hooks.html +3 -3
- data/docs/i18n.html +3 -3
- data/docs/index.html +3 -3
- data/docs/instrumentation.html +3 -3
- data/docs/javascript.html +3 -3
- data/docs/jobs.html +3 -3
- data/docs/keyword-injection.html +3 -3
- data/docs/layouts.html +3 -3
- data/docs/lsp.html +3 -3
- data/docs/markdown-examples.html +3 -3
- data/docs/middleware.html +3 -3
- data/docs/overview.html +3 -3
- data/docs/pages.html +3 -3
- data/docs/recipes/alternate-layouts.html +3 -3
- data/docs/recipes/authentication.html +3 -3
- data/docs/recipes/blank-layouts.html +3 -3
- data/docs/recipes/custom-flash.html +3 -3
- data/docs/recipes/indexed-forms.html +3 -3
- data/docs/recipes/migrations.html +3 -3
- data/docs/recipes/text-field-component.html +3 -3
- data/docs/roadmap.html +3 -3
- data/docs/routes.html +3 -3
- data/docs/security.html +3 -3
- data/docs/seed-data.html +3 -3
- data/docs/space-time-continuum.html +3 -3
- data/docs/tutorial.html +732 -5
- data/docs/unit-tests.html +3 -3
- data/docs/why.html +3 -3
- data/lib/brut/framework/config.rb +9 -14
- data/lib/brut/version.rb +1 -1
- data/mkbrut/Gemfile.lock +1 -1
- data/mkbrut/lib/mkbrut/version.rb +1 -1
- metadata +35 -15
- data/docs/assets/chunks/@localSearchIndexroot.DUha-mf5.js +0 -1
- data/docs/assets/tutorial.md.BIb7XT6j.js +0 -1
- data/docs/assets/tutorial.md.BIb7XT6j.lean.js +0 -1
- /data/docs/assets/{components.md.-22ecBxM.lean.js → components.md.Bu80E2Nr.lean.js} +0 -0
- /data/docs/assets/{configuration.md.CLKL1lFf.lean.js → configuration.md.CuIxVsSf.lean.js} +0 -0
- /data/docs/assets/{forms.md.CBI5--o2.lean.js → forms.md.DnLbzVDa.lean.js} +0 -0
- /data/docs/assets/{getting-started.md.D5nzBZ4J.lean.js → getting-started.md.DdQLmU3C.lean.js} +0 -0
data/brutrb.com/tutorial.md
CHANGED
@@ -1,11 +1,1736 @@
|
|
1
1
|
# Tutorial
|
2
2
|
|
3
|
-
|
3
|
+
Below, you'll build a blog that has a few pages, a form, a database table, and tests. It's the same steps I took in the [15-minute blog video](https://video.hardlimit.com/w/ae7EMhwjDq9kSH5dqQ9swV).
|
4
4
|
|
5
|
-
|
5
|
+
If you'd just like to read source code, there are two apps you can check out:
|
6
6
|
|
7
|
-
* [
|
7
|
+
* [The blog we'll build here](https://github.com/thirdtank/blog-demo)
|
8
8
|
* [ADRs.cloud](https://github.com/thirdtank/adrs.cloud), which is a more realistic app that has
|
9
9
|
mulitple database tables, progressively-enhanced UI, and background jobs.
|
10
10
|
|
11
11
|
You can be running either of these locally in minutes as long as you have Docker installed.
|
12
|
+
|
13
|
+
## Understanding This Tutorial
|
14
|
+
|
15
|
+
This tutorial will show you command line invocations and code. You should be able to follow along and just type what we say and it should work.
|
16
|
+
|
17
|
+
That said, it's not always clera what we are talking about.
|
18
|
+
|
19
|
+
### Understanding Command Line Invocations
|
20
|
+
|
21
|
+
If you aren't comfortable on the command line, it can be hard to understand what parts of this tutorial represent stuff you should type/paste and what is output from those commands. Here is how that works.
|
22
|
+
|
23
|
+
When we want you to run a command, the preceding text will tell you something like "run this command", and then you'll see a codeblock that has the label "bash" in the upper right corner, like so:
|
24
|
+
|
25
|
+
```bash
|
26
|
+
ls -l
|
27
|
+
```
|
28
|
+
|
29
|
+
If you hover over it, an icon with the tooltip "Copy Code" will appear on the right, and you can click that to copy the command-line invocation. Or, you can select it and copy it, or you can type it in manually.
|
30
|
+
|
31
|
+
In any case, you are expected to type/paste/execute the entire thing. Other parts of this documentation site may precede command lines with `>` to indicate it's a shell command. For this tutorial, we aren't doing that.
|
32
|
+
|
33
|
+
Sometimes, commands are long. They can be split up by entering a backslash (`\`) as the last character of a line, hitting return, and continuing the command. For example this command:
|
34
|
+
|
35
|
+
```bash
|
36
|
+
git push origin main
|
37
|
+
```
|
38
|
+
|
39
|
+
Could be executed like so:
|
40
|
+
|
41
|
+
```bash
|
42
|
+
git push \
|
43
|
+
origin \
|
44
|
+
main
|
45
|
+
```
|
46
|
+
|
47
|
+
In both cases, you can copy/type these as written and they will work.
|
48
|
+
|
49
|
+
To show output of a command, a separate code block will be used, and the first line of the output will be the string `# OUTPUT:`, and there should **not** be a "bash" label in the upper right corner:
|
50
|
+
|
51
|
+
```
|
52
|
+
# OUTPUT
|
53
|
+
app dx puma.config.rb
|
54
|
+
bin Gemfile README.md
|
55
|
+
config.ru package.json specs
|
56
|
+
docker-compose.dx.yml Procfile.development
|
57
|
+
Dockerfile.dx Procfile.test
|
58
|
+
```
|
59
|
+
|
60
|
+
Sometimes, output is very long and very irrelevant. In that case, the string `«LOTS OF OUTPUT»` will be used as a placeholder:
|
61
|
+
|
62
|
+
```
|
63
|
+
# OUTPUT
|
64
|
+
app
|
65
|
+
dx
|
66
|
+
puma.config.rb
|
67
|
+
«LOTS OF OUTPUTS»
|
68
|
+
```
|
69
|
+
|
70
|
+
### Understanding Code Changes
|
71
|
+
|
72
|
+
In most cases, we'll show you the entire code for a file/class, and you should make your copy look like it. Suppose you have this:
|
73
|
+
|
74
|
+
```ruby
|
75
|
+
class SomeComponent < AppComponent
|
76
|
+
end
|
77
|
+
```
|
78
|
+
|
79
|
+
We might say "add the `view_template` to your component so it looks like this:"
|
80
|
+
|
81
|
+
```ruby
|
82
|
+
class SomeComponent < AppComponent
|
83
|
+
def view_template
|
84
|
+
h3 { "My component" }
|
85
|
+
a(href:HelpPage.routing) { "Would You Like to Know More?" }
|
86
|
+
end
|
87
|
+
end
|
88
|
+
```
|
89
|
+
|
90
|
+
That means you can replace the file with this code. Other times, we may only focus on one method. We might write "Change `view_template` in `SomeComponent` so it looks like so:"
|
91
|
+
|
92
|
+
```ruby
|
93
|
+
def view_template
|
94
|
+
h3 { "My component" }
|
95
|
+
a(href:HelpPage.routing) { "Would You Like to Know More?" }
|
96
|
+
end
|
97
|
+
```
|
98
|
+
|
99
|
+
In this case, you'd replace the method, but the leave the rest of the class as-is.
|
100
|
+
|
101
|
+
On occasion we'll want to only change a few lines and, in that case, we'll use a diff format like so:
|
102
|
+
|
103
|
+
```diff
|
104
|
+
- a(href: "") { "Write New Blog Post" }
|
105
|
+
+ a(href: BlogPostEditorPage.routing) { "Write New Blog Post" }
|
106
|
+
```
|
107
|
+
|
108
|
+
This says to find the line that looks like the first one (preceded with a `-` and shown in red) and replace it with the second one (preceded with a `+` and shown in green). **Do not use the `+` or `-` in your code**, that is just to indicate which line is which.
|
109
|
+
|
110
|
+
Lastly, we'll try to mention the path to the file either in the preceding text or as a comment in the code.
|
111
|
+
|
112
|
+
OK, back to your regularly-scheduled tutorial
|
113
|
+
|
114
|
+
## Set Up
|
115
|
+
|
116
|
+
The only two pieces of software you need are Docker and a code editor:
|
117
|
+
|
118
|
+
1. [Install Docker](https://docker.com)
|
119
|
+
|
120
|
+
> [!TIP]
|
121
|
+
> If you are on Windows, we *highly* recommend you use the
|
122
|
+
> Windows Subystem for Linux (WSL2), as this makes Brut, web developement,
|
123
|
+
> and, honestly, your entire life as you know it, far easier than trying to
|
124
|
+
> get things working natively in Windows.
|
125
|
+
2. If you are new to programming or new to Ruby and don't know what editor to get, use VSCode. If you are a vim or emacs person, those will be far better, but if you are used to an IDE, VSCode will be the easiest to get set up and learn to use.
|
126
|
+
|
127
|
+
To check that docker is installed, open up a terminal and run:
|
128
|
+
|
129
|
+
```bash
|
130
|
+
docker info
|
131
|
+
```
|
132
|
+
|
133
|
+
This should produce a ton of output:
|
134
|
+
|
135
|
+
```
|
136
|
+
# OUTPUT
|
137
|
+
Client:
|
138
|
+
Version: 28.2.2
|
139
|
+
«LOTS OF OUTPUT»
|
140
|
+
```
|
141
|
+
|
142
|
+
To be extra sure, **right after you ran `docker info`**, check `$?`, the exit code, to make sure it's a 0, which means the command ran successfully:
|
143
|
+
|
144
|
+
```bash
|
145
|
+
echo $?
|
146
|
+
```
|
147
|
+
|
148
|
+
```
|
149
|
+
# OUTPUT
|
150
|
+
0
|
151
|
+
```
|
152
|
+
|
153
|
+
Now, let's create the app by first initializing it.
|
154
|
+
|
155
|
+
|
156
|
+
## Initialize Your App
|
157
|
+
|
158
|
+
`mkbrut` is a command line app that will initialize your new app. It's available as a RubyGem or a Docker image. We'll use the Docker image since that doesn't require installing anything.
|
159
|
+
|
160
|
+
We'll call the blog simply "blog". `mkbrut` will insert some demo features in new apps to show you have to use Brut. Since you're following this tutorial, you don't need that, so we'll use the `--no-demo` flag.
|
161
|
+
|
162
|
+
`cd` to a folder where you'd like to work. `mkbrut` will create a folder called `blog` in there and in *that* folder, your app will be initialized.
|
163
|
+
|
164
|
+
The command to do this is pretty long, because it downloads `mkbrut` and then runs it inside a Docker container, meaning you don't have to install anything new. Here it is:
|
165
|
+
|
166
|
+
```
|
167
|
+
docker run \
|
168
|
+
-v "$PWD":"$PWD" \
|
169
|
+
-w "$PWD" \
|
170
|
+
-u $(id -u):$(id -g) \
|
171
|
+
-it \
|
172
|
+
thirdtank/mkbrut \
|
173
|
+
mkbrut --no-demo blog
|
174
|
+
```
|
175
|
+
|
176
|
+
You should see this output:
|
177
|
+
|
178
|
+
```
|
179
|
+
# OUTPUT
|
180
|
+
[ mkbrut ] Creating app with these options:
|
181
|
+
[ mkbrut ] App name: blog
|
182
|
+
[ mkbrut ] App ID: blog
|
183
|
+
[ mkbrut ] Prefix: bl
|
184
|
+
[ mkbrut ] Organization: blog
|
185
|
+
[ mkbrut ] Include demo? false
|
186
|
+
[ mkbrut ] Creating Base app
|
187
|
+
[ mkbrut ] Creating segment: Bare bones framing
|
188
|
+
[ mkbrut ] blog was created
|
189
|
+
|
190
|
+
[ mkbrut ] Time to get building:
|
191
|
+
[ mkbrut ] 1. cd blog
|
192
|
+
[ mkbrut ] 2. dx/build
|
193
|
+
[ mkbrut ] 3. dx/start
|
194
|
+
[ mkbrut ] 4. [ in another terminal ] dx/exec bash
|
195
|
+
[ mkbrut ] 5. [ inside the Docker container ] bin/setup
|
196
|
+
[ mkbrut ] 6. [ inside the Docker container ] bin/dev
|
197
|
+
[ mkbrut ] 7. Visit http://localhost:6502 in your browser
|
198
|
+
[ mkbrut ] 8. [ inside the Docker container ] bin/setup help # to see more commands
|
199
|
+
```
|
200
|
+
|
201
|
+
Before we follow the instructions in the output, `cd` to `blog` and check it out.
|
202
|
+
|
203
|
+
```bash
|
204
|
+
cd blog
|
205
|
+
ls
|
206
|
+
```
|
207
|
+
|
208
|
+
```
|
209
|
+
#OUTPUT
|
210
|
+
app Dockerfile.dx Procfile.development specs
|
211
|
+
bin dx Procfile.test
|
212
|
+
config.ru Gemfile puma.config.rb
|
213
|
+
docker-compose.dx.yml package.json README.md
|
214
|
+
```
|
215
|
+
|
216
|
+
* `app` is where all the code your app will be
|
217
|
+
* `bin` has command line tools to manage your app
|
218
|
+
* `dx` has command line tools to manage your development environment
|
219
|
+
* `specs` is where your tests will go
|
220
|
+
|
221
|
+
OK, let's start up the dev environment:
|
222
|
+
|
223
|
+
```bash
|
224
|
+
dx/build
|
225
|
+
```
|
226
|
+
|
227
|
+
```
|
228
|
+
# OUTPUT
|
229
|
+
[ dx/build ] Could not find Gemfile.lock, which is needed to determine the playwright-ruby-client version
|
230
|
+
[ dx/build ] Assuming your app is brand-new, this should be OK
|
231
|
+
[+] Building 0.2s
|
232
|
+
«LOTS OF OUTPUT»
|
233
|
+
```
|
234
|
+
|
235
|
+
This may take a while, but it's building a Docker image where all your work will happen, although you'll be able to edit your code on your computer with the editor of your choice.
|
236
|
+
|
237
|
+
When this is done, you should see a message like so:
|
238
|
+
|
239
|
+
```
|
240
|
+
# OUTPUT
|
241
|
+
[ dx/build ] 🌈 Your Docker image has been built tagged 'blog/blog:ruby-3.4'
|
242
|
+
[ dx/build ] 🔄 You can now run dx/start to start it up, though you may need to stop it first with Ctrl-C
|
243
|
+
```
|
244
|
+
|
245
|
+
Now, start up the environment:
|
246
|
+
|
247
|
+
```bash
|
248
|
+
dx/start
|
249
|
+
```
|
250
|
+
|
251
|
+
```
|
252
|
+
#OUTPUT
|
253
|
+
[ dx/start ] 🚀 Starting docker-compose.dx.yml
|
254
|
+
[+] Running 1/5
|
255
|
+
⠙ Container blog-postgres-1
|
256
|
+
⠙ Container blog-app-1
|
257
|
+
⠙ Container blog-otel-desktop-viewer-1
|
258
|
+
«LOTS OF OUTPUT»
|
259
|
+
app-1 | 2025-08-11T16:39:11.568390000-04:00
|
260
|
+
app-1 | 2025-08-11T16:39:11.568978000-04:00
|
261
|
+
app-1 | 2025-08-11T16:39:11.569430000-04:00
|
262
|
+
app-1 | 2025-08-11T16:39:11.569825000-04:00 🎉 Dev Environment Initialized! 🎉
|
263
|
+
app-1 | 2025-08-11T16:39:11.570214000-04:00
|
264
|
+
app-1 | 2025-08-11T16:39:11.570599000-04:00 ℹ️ To use this environment, open a new terminal and run
|
265
|
+
app-1 | 2025-08-11T16:39:11.570980000-04:00
|
266
|
+
app-1 | 2025-08-11T16:39:11.571250000-04:00 dx/exec bash
|
267
|
+
app-1 | 2025-08-11T16:39:11.571521000-04:00
|
268
|
+
app-1 | 2025-08-11T16:39:11.571795000-04:00 🕹 Use `ctrl-c` to exit.
|
269
|
+
app-1 | 2025-08-11T16:39:11.572064000-04:00
|
270
|
+
app-1 | 2025-08-11T16:39:11.572327000-04:00
|
271
|
+
app-1 | 2025-08-11T16:39:11.572596000-04:00
|
272
|
+
```
|
273
|
+
|
274
|
+
`dx/start` will keep running. If you stop it, your dev environment will stop. It's running three containers:
|
275
|
+
|
276
|
+
* `app`, which is where the app and its test will run
|
277
|
+
* `postgres`, which is running PostgreSQL, a SQL database you'll use
|
278
|
+
* `otel-desktop-viewer` which can view telemetry of your app. We'll see that later.
|
279
|
+
|
280
|
+
Now, let's access the container we started.
|
281
|
+
|
282
|
+
Open a new terminal window, `cd` to where `blog` is, and use `dx/exec` to run `bash`, effectively "logging in" to the container:
|
283
|
+
|
284
|
+
```bash
|
285
|
+
dx/exec bash
|
286
|
+
```
|
287
|
+
|
288
|
+
```
|
289
|
+
# OUTPUT
|
290
|
+
[ dx/exec ] 🚂 Running 'ssh-agent bash' inside container with service name 'app'
|
291
|
+
Now using node v22.18.0 (npm v10.9.3)
|
292
|
+
docker-container - Projects/blog
|
293
|
+
>
|
294
|
+
```
|
295
|
+
|
296
|
+
At that prompt, you can now type commands. If you type `ls`, you'll see the same files you saw when we ran it above:
|
297
|
+
|
298
|
+
```bash
|
299
|
+
ls
|
300
|
+
```
|
301
|
+
|
302
|
+
```
|
303
|
+
#OUTPUT
|
304
|
+
app Dockerfile.dx Procfile.development specs
|
305
|
+
bin dx Procfile.test
|
306
|
+
config.ru Gemfile puma.config.rb
|
307
|
+
docker-compose.dx.yml package.json README.md
|
308
|
+
```
|
309
|
+
|
310
|
+
This is because the folder on your computer is synced to the one inside the container. Changes in one are immediately reflected in the other.
|
311
|
+
|
312
|
+
**From here on out, all command line invocations are run inside this container**, unless stated otherwise.
|
313
|
+
|
314
|
+
## Set Up the App Itself
|
315
|
+
|
316
|
+
`mkbrut` created a lot of files for you, as well as command line apps to manage your app. We're going to perform app setup via `bin/setup`. This completely automates the following tasks:
|
317
|
+
|
318
|
+
* Installing RubyGems
|
319
|
+
* Installing Node Modules
|
320
|
+
* Installing Shopfiy's Ruby LSP, and Microsoft's JS and CSS LSPs
|
321
|
+
* Creating your dev and test databases
|
322
|
+
* Setting up Chromium, which we'll use to run end-to-end tests
|
323
|
+
|
324
|
+
Run it now (remember, this is inside the container, so you should've run `dx/exec bash` on your computer first)
|
325
|
+
|
326
|
+
```bash
|
327
|
+
bin/setup
|
328
|
+
```
|
329
|
+
|
330
|
+
```
|
331
|
+
# OUTPUT
|
332
|
+
[ bin/setup ] Installing gems
|
333
|
+
[ bin/setup ] Executing ["bundle check --no-color || bundle install --no-color --quiet"]
|
334
|
+
«LOTS OF OUTPUT»
|
335
|
+
[ bin/setup ] All set up.
|
336
|
+
|
337
|
+
USEFUL COMMANDS
|
338
|
+
|
339
|
+
bin/dev
|
340
|
+
# run app locally, rebuilding and reloading as needed
|
341
|
+
|
342
|
+
bin/ci
|
343
|
+
# runs all tests and checks as CI would
|
344
|
+
|
345
|
+
bin/console
|
346
|
+
# get an IRB console with the app loaded
|
347
|
+
|
348
|
+
bin/db
|
349
|
+
# interact with the DB for migrations, information, etc
|
350
|
+
|
351
|
+
bin/dbconsole
|
352
|
+
# get a PSQL session to the database
|
353
|
+
|
354
|
+
bin/scaffold
|
355
|
+
# Create various structures in your app, like pages or forms
|
356
|
+
|
357
|
+
bin/setup help
|
358
|
+
# show this help
|
359
|
+
```
|
360
|
+
|
361
|
+
When this is done, we can check that everything's working by running `bin/ci`. `bin/ci` runs all tests and quality checks. Even though you haven't written any code, there's just a bit included to ensure that what little is there is working properly. It's a good check before you start to make sure install and setup worked.
|
362
|
+
|
363
|
+
```bash
|
364
|
+
bin/ci
|
365
|
+
```
|
366
|
+
|
367
|
+
```
|
368
|
+
# OUTPUT
|
369
|
+
[ bin/ci ] Building Assets
|
370
|
+
«LOTS OF OUTPUT»
|
371
|
+
[ bin/ci ] Running non E2E tests
|
372
|
+
«LOTS OF OUTPUT»
|
373
|
+
[ bin/ci ] Running JS tests
|
374
|
+
«LOTS OF OUTPUT»
|
375
|
+
[ bin/ci ] Running E2E tests
|
376
|
+
«LOTS OF OUTPUT»
|
377
|
+
[ bin/ci ] Analyzing Ruby gems for
|
378
|
+
«LOTS OF OUTPUT»
|
379
|
+
[ bin/ci ] security vulnerabilities
|
380
|
+
«LOTS OF OUTPUT»
|
381
|
+
[ bin/ci ] Checking to see that all classes have tests
|
382
|
+
«LOTS OF OUTPUT»
|
383
|
+
[ bin/ci ] Done
|
384
|
+
```
|
385
|
+
|
386
|
+
Finally, we'll run the app itself via `bin/dev`
|
387
|
+
|
388
|
+
```bash
|
389
|
+
bin/dev
|
390
|
+
```
|
391
|
+
|
392
|
+
`bin/dev` won't exit, it'll sit there running your app until you hit `Ctrl-C`. Amongst the output you should see a message like:
|
393
|
+
|
394
|
+
```
|
395
|
+
# OUTPUT
|
396
|
+
« LOTS OF OUTPUT »
|
397
|
+
20:43:41 startup_message.1 | Your app is now running at
|
398
|
+
20:43:41 startup_message.1 |
|
399
|
+
20:43:41 startup_message.1 | http://localhost:6502
|
400
|
+
20:43:41 startup_message.1 |
|
401
|
+
```
|
402
|
+
|
403
|
+
Go to http://localhost:6502 in your web browser. You should see a welcome screen like so:
|
404
|
+
|
405
|
+

|
406
|
+
|
407
|
+
## The Blog We'll Build
|
408
|
+
|
409
|
+
We're ready to write some code! Here's how the blog is going to work:
|
410
|
+
|
411
|
+
* A blog post has a title and content, with each paragraph of the content separated with `\n\r`, which
|
412
|
+
is what the browser inserts when you hit return.
|
413
|
+
* The home page will show all the blog posts in reverse chronological order.
|
414
|
+
* The home page will link to the edit blog post page where a blog post can be created.
|
415
|
+
* Blog posts will be submitted to the backend to be saved, with the following constraints:
|
416
|
+
- title and content are required
|
417
|
+
- title must be at least three characters
|
418
|
+
- content must be at least 5 words (i.e. space-separated tokens)
|
419
|
+
|
420
|
+
We'll discuss tests later. To make it easier to follow Brut, we'll get things working first and then test them. You can absolutely do TDD with Brut, but we find it's hard to learn new things this way.
|
421
|
+
|
422
|
+
Let's start not from the database, but from the user experience.
|
423
|
+
|
424
|
+
## Building and Styling Pages
|
425
|
+
|
426
|
+
The home page of a Brut app is served, naturally, on `/` and is implemented by the class `HomePage`, located in `app/src/front_end/pages/home_page.rb`.
|
427
|
+
|
428
|
+
A *page* in Brut is a Phlex component that is rendered inside a layout. A layout is common markup that all pages should have, such as the `<head>` section and perhaps a `<body>` or other tags. `mkbrut` provided a default layout that's good for now, so we just have to worry about the HTML that is specific to a page.
|
429
|
+
|
430
|
+
Open up `app/src/front_end/pages/home_page.rb` in your editor. You should see something like this:
|
431
|
+
|
432
|
+
```ruby
|
433
|
+
class HomePage < AppPage
|
434
|
+
def page_template
|
435
|
+
# The duplication and excessive class sizes here are to
|
436
|
+
# make it easier for you to remove this when you start working
|
437
|
+
# on your app. There are pros and cons to how this code
|
438
|
+
# is written, so don't take this is as a directive on how to
|
439
|
+
# build your app. You do you!
|
440
|
+
img(src: "/static/images/LogoPylon.png",
|
441
|
+
class: "dn db-ns pos-fixed top-0 left-0 h-100vh w-auto z-2")
|
442
|
+
|
443
|
+
header(class: "flex flex-column items-center justify-center h-100vh") do
|
444
|
+
|
445
|
+
# A lot more code
|
446
|
+
|
447
|
+
end
|
448
|
+
end
|
449
|
+
end
|
450
|
+
```
|
451
|
+
|
452
|
+
`page_template` is where you can call Phlex to generate HTML.
|
453
|
+
|
454
|
+
> [!NOTE]
|
455
|
+
> Phlex components use `view_template`, and that's what
|
456
|
+
> components in Brut use, too. Pages, however, use
|
457
|
+
> `page_template` so that the HTML can be placed inside
|
458
|
+
> a layout. `page_template` is a Brut concept, not a Phlex one.
|
459
|
+
|
460
|
+
### Creating the HomePage
|
461
|
+
|
462
|
+
Delete all the code in `page_template` and replace it with this:
|
463
|
+
|
464
|
+
```ruby
|
465
|
+
def page_template
|
466
|
+
header do
|
467
|
+
h1 { "My Amazing Blog" }
|
468
|
+
a(href: "") { "Write New Blog Post" }
|
469
|
+
end
|
470
|
+
main do
|
471
|
+
p { "Posts go here" }
|
472
|
+
end
|
473
|
+
end
|
474
|
+
```
|
475
|
+
|
476
|
+
If you've never used Phlex before, it's a Ruby API that defines one method for each HTML element (along with any custom elements you tell it about). You then call these methods to build up markup. As you can see, it's structurally identical to HTML, but it's Ruby.
|
477
|
+
|
478
|
+
If your server is still running, refresh the page and you'll see this wonderful UI (otherwise, start your server with `bin/dev`):
|
479
|
+
|
480
|
+

|
481
|
+
|
482
|
+
Let's make it a bit nicer.
|
483
|
+
|
484
|
+
### Using CSS
|
485
|
+
|
486
|
+
Open up `app/src/front_end/css/index.css` in your editor. You should see this:
|
487
|
+
|
488
|
+
```css
|
489
|
+
@import "brut-css/dist/brut.css";
|
490
|
+
@import "svgs.css";
|
491
|
+
```
|
492
|
+
|
493
|
+
Brut uses esbuild to bundle CSS. esbuild makes use of the standard `@import` directive. All `@imports` are relative to the current file or to `node_modules`. `brut-css/dist/brut.css` is the BrutCSS library that comes with Brut. We aren't going to use it, just to keep things focused. `svgs.css` is located in `app/src/front_end/css/svgs.css` and sets up a few classes for inline SVGs.
|
494
|
+
|
495
|
+
We'll add some CSS for the home page right here. We'll use vanilla CSS to avoid going on a deep dive on CSS frameworks.
|
496
|
+
|
497
|
+
Add this below `@import "svgs.css";`
|
498
|
+
|
499
|
+
```css
|
500
|
+
body {
|
501
|
+
width: 50%;
|
502
|
+
margin-left: auto;
|
503
|
+
margin-right: auto;
|
504
|
+
}
|
505
|
+
|
506
|
+
header {
|
507
|
+
border-bottom: solid thin gray;
|
508
|
+
display: flex;
|
509
|
+
align-items: baseline;
|
510
|
+
justify-content: space-between;
|
511
|
+
width: 100%;
|
512
|
+
gap: 0.5rem;
|
513
|
+
}
|
514
|
+
```
|
515
|
+
|
516
|
+
If you reload the home page in your browser, it now looks at least a little bit respectible:
|
517
|
+
|
518
|
+

|
519
|
+
|
520
|
+
Now, let's build the blog post editor.
|
521
|
+
|
522
|
+
## Creating Forms
|
523
|
+
|
524
|
+
To create blog posts, we need three things:
|
525
|
+
|
526
|
+
* A page where the creation happens, which will host an HTML `<form>`
|
527
|
+
* A URL where that `<form>` will be submitted
|
528
|
+
* Some code to handle the submissions
|
529
|
+
|
530
|
+
### Creating a New Page
|
531
|
+
|
532
|
+
To make a new page in Brut, we'll need to declare a route, and Brut will choose the class name. We'll use `/blog_post_editor`, meaning Brut will expect `BlogPostEditorPage` to exist. We can do all this at once with `bin/scaffold page`. `bin/scaffold page` accepts the URL of the page we want to build. Brut will use that URL to figure out the page class' name and generate it, along with a failing test. It will also insert the route into `app.rb`. Run it now, like so:
|
533
|
+
|
534
|
+
```bash
|
535
|
+
bin/scaffold page /blog_post_editor
|
536
|
+
```
|
537
|
+
|
538
|
+
Your output should look like so:
|
539
|
+
|
540
|
+
```
|
541
|
+
# OUTPUT
|
542
|
+
[ bin/scaffold ] Inserted route into app/src/app.rb
|
543
|
+
[ bin/scaffold ] Page source is in app/src/front_end/pages/blog_post_editor_page.rb
|
544
|
+
[ bin/scaffold ] Page test is in specs/front_end/pages/blog_post_editor_page.spec.rb
|
545
|
+
[ bin/scaffold ] Added title to app/config/i18n/en/2_app.rb
|
546
|
+
[ bin/scaffold ] Added route to app/src/app.rb
|
547
|
+
```
|
548
|
+
|
549
|
+
Restart your server (Brut currently cannot auto-reload new routes).
|
550
|
+
|
551
|
+
If you manually navigate to `http://localhost:6502/blog_post_editor`, you can see a very basic page has been created. Before we build the actual page, let's change the home page so it links here.
|
552
|
+
|
553
|
+
If you'll recall, we had a `a(href:"") { ... }` in our template. We now know the URL for that `href`. We *could* use the actual url, `/blog_post_editor`, but it's going to be easier to manage our app over time if we don't hard-code paths and instead use our new page class to generate the URL.
|
554
|
+
|
555
|
+
Open up `app/src/front_end/pages/home_page.rb` and make this change:
|
556
|
+
|
557
|
+
```diff
|
558
|
+
- a(href: "") { "Write New Blog Post" }
|
559
|
+
+ a(href: BlogPostEditorPage.routing) { "Write New Blog Post" }
|
560
|
+
```
|
561
|
+
|
562
|
+
All page classes have a `.routing` method. By using this instead of building a URL ourselves, we get some advantages:
|
563
|
+
|
564
|
+
* If we rename or remove `BlogPostEditorPage`, any page referencing it will generate a nice, easy-to-understand error.
|
565
|
+
* `routing` can manage query strings and anchors in a safe way, plus it can check that if a page has
|
566
|
+
required routing parameters (e.g. the `123` in `/posts/123`), that they are provided.
|
567
|
+
|
568
|
+
With this change, you can now click the link and see the `BlogPostEditorPage`'s template we saw earlier.
|
569
|
+
|
570
|
+
Most of the `BlogPostEditorPage`'s HTML will be a form to submit a new blog post. While we could write that using HTML, Brut makes life easier by allowing the use of a *form class* to do it, which also will describe the data to be submitted to the server. This data is handled by a *handler*.
|
571
|
+
|
572
|
+
### Create a Form and Handler
|
573
|
+
|
574
|
+
A form gets submitted to a URL, and Brut routes that submission to a handler. To create both a form class and a handler, we'll use `bin/scaffold form`, giving it the URL to respond on.
|
575
|
+
|
576
|
+
In this case, we'll use the URL `/new_blog_post`. Stop your server and run this command:
|
577
|
+
|
578
|
+
```bash
|
579
|
+
bin/scaffold form /new_blog_post
|
580
|
+
```
|
581
|
+
|
582
|
+
```
|
583
|
+
# OUTPUT
|
584
|
+
[ bin/scaffold ] NewBlogPostForm in app/src/front_end/forms/new_blog_post_form.rb
|
585
|
+
[ bin/scaffold ] NewBlogPostHandler in app/src/front_end/handlers/new_blog_post_handler.rb
|
586
|
+
[ bin/scaffold ] Spec in specs/front_end/handlers/new_blog_post_handler.spec.rb
|
587
|
+
[ bin/scaffold ] Inserted route into app/src/app.rb
|
588
|
+
```
|
589
|
+
|
590
|
+
When creating a new form, the first thing we have to do is edit the form class (in this case,
|
591
|
+
`NewBlogPostForm`, located in `app/src/front_end/forms/new_blog_post_form.rb`) to describe the values being accepted by the server.
|
592
|
+
|
593
|
+
This can be done by calling static/class methods provided by `Brut::FrontEnd::Form`, the superclass of `AppForm`, which is the superclass of our app's forms.
|
594
|
+
|
595
|
+
Open up `app/src/front_end/forms/new_blog_post_form.rb`. We'll call `input` twice, once for the title and once for the content. `input` takes keyword arguments that mirror those of the web platform's constraint validation system. Since our title must be at least 3 characters, that means we'll use `minlength` to specify this.
|
596
|
+
|
597
|
+
Here's the code:
|
598
|
+
|
599
|
+
```ruby
|
600
|
+
class NewBlogPostForm < AppForm
|
601
|
+
input :title, minlength: 3
|
602
|
+
input :content
|
603
|
+
end
|
604
|
+
```
|
605
|
+
|
606
|
+
Each field is required by default (you can set `required: false` on fields that aren't required).
|
607
|
+
|
608
|
+
With these declarations, we can use an instance of this class to generate HTML.
|
609
|
+
|
610
|
+
### Generating an HTML Form
|
611
|
+
|
612
|
+
The `BlogPostEditorPage` will contain the form used to write a blog post. This page must make sure two things happen:
|
613
|
+
|
614
|
+
* When someone navigates to it, it should show the form with nothing in the fields.
|
615
|
+
* When there is an error in what the blog post author has provided, it should show those errors, but
|
616
|
+
also maintain the inputs the author provided.
|
617
|
+
|
618
|
+
To do this, the `BlogPostEditorPage` will need an instance of `NewBlogPostForm`. We can create this in its constructor. Open up `app/src/front_end/pages/blog_post_editor_page.rb` and start it off like so:
|
619
|
+
|
620
|
+
```ruby
|
621
|
+
class BlogPostEditorPage < AppPage
|
622
|
+
|
623
|
+
def initialize
|
624
|
+
@form = NewBlogPostForm.new
|
625
|
+
end
|
626
|
+
|
627
|
+
# ...
|
628
|
+
|
629
|
+
end
|
630
|
+
```
|
631
|
+
|
632
|
+
Next, we'll implement `page_template` and we'll use `@form` to create HTML for the form's inputs, including client-side constraints and, as we'll see later, pre-existing values from a previous submission.
|
633
|
+
|
634
|
+
This will require four parts of Brut's API and use one optional one:
|
635
|
+
|
636
|
+
* `brut_form`, a custom element (`<brut-form>`) that will progressively enhance the form to provide
|
637
|
+
constraint violation visitor experience if JavaScript is enabled.
|
638
|
+
* `FormTag`, a Phlex component provided by Brut that generates the correct `<form>` element, as well as
|
639
|
+
CSRF protection.
|
640
|
+
* `Inputs::` components, namely `Brut::FrontEnd::Components::Inputs::InputTag` and `Brut::FrontEnd::Components::Inputs::TextareaTag`, which generate `<input>` and `<textarea>`, respectively. These Phlex components (provided by Brut) will add the correct attributes for validation, and set the values if the form they are given has values set.
|
641
|
+
* `Brut::FrontEnd::Components::ConstraintViolations`, a Phlex component provided by Brut that generates custom elements that, when JavaScript is enabled, allow for control over the visitor experience when there are constraint violations.
|
642
|
+
* *(optional)* `t` provides access to localized strings, instead of hard-coding English.
|
643
|
+
|
644
|
+
Create `page_template` to look like so:
|
645
|
+
|
646
|
+
```ruby
|
647
|
+
def page_template
|
648
|
+
h1 { t(:write_new_post) }
|
649
|
+
brut_form do
|
650
|
+
FormTag(for: @form) do
|
651
|
+
label do
|
652
|
+
Inputs::InputTag(form: @form,input_name: :title, autofocus: true)
|
653
|
+
div { t([:form, :title]) }
|
654
|
+
ConstraintViolations(form: @form, input_name: :title)
|
655
|
+
end
|
656
|
+
label do
|
657
|
+
Inputs::TextareaTag(form: @form,input_name: :content, rows: 10)
|
658
|
+
div { t([:form, :content] ) }
|
659
|
+
ConstraintViolations(form: @form, input_name: :content)
|
660
|
+
end
|
661
|
+
|
662
|
+
button { t([:form, :post]) }
|
663
|
+
end
|
664
|
+
end
|
665
|
+
end
|
666
|
+
```
|
667
|
+
|
668
|
+
> [!TIP]
|
669
|
+
> You'll notice that we mentioned classes like `Brut::FrontEnd::Components::Inputs::InputTag`, but the
|
670
|
+
> code above is only using `Input::InputTag`. This is due to [*Phlex
|
671
|
+
> Kits*](https://www.phlex.fun/components/kits.html), which allow you to use relative class names
|
672
|
+
> in certain circumstances.
|
673
|
+
>
|
674
|
+
> Brut makes use of this so there is a clear and organized name for a component, but you almost never
|
675
|
+
> have to type or read the entire thing.
|
676
|
+
|
677
|
+
Make sure your server is running, then reload the blog post editor page. You should see an error like so:
|
678
|
+
|
679
|
+
> `Translation missing. Options considered were: - en.pages.BlogPostEditorPage.write_new_post - en.write_new_post`
|
680
|
+
|
681
|
+
Let's add those keys.
|
682
|
+
|
683
|
+
### Adding Translation Keys
|
684
|
+
|
685
|
+
In Brut, translations aren't stored in YAML 🥳🎉, but in a Ruby hash. Brut provides standard translations in `app/config/i18n/en/1_defaults.rb`, but your app will set its own in `app/config/i18n/en/2_app.rb`:
|
686
|
+
|
687
|
+
```ruby
|
688
|
+
# All app-specific translations, or overrides of Brut's defaults, go here.
|
689
|
+
{
|
690
|
+
# en: must be the first entry, thus indicating this is the EN translations
|
691
|
+
en: {
|
692
|
+
cv: {
|
693
|
+
cs: {
|
694
|
+
},
|
695
|
+
ss: {
|
696
|
+
email_taken: "This email has been taken",
|
697
|
+
},
|
698
|
+
},
|
699
|
+
pages: { # Page-specific messages - this key is relied-upon by Brut to exist
|
700
|
+
HomePage: {
|
701
|
+
title: "Welcome!",
|
702
|
+
},
|
703
|
+
BlogPostEditorPage: {
|
704
|
+
title: "BlogPostEditorPage"
|
705
|
+
},
|
706
|
+
},
|
707
|
+
# ... more code
|
708
|
+
```
|
709
|
+
|
710
|
+
When you use `t` on a page in Brut, it looks for `pages.«page name».«key»`, so Brut needs from our form:
|
711
|
+
|
712
|
+
* `pages.BlogPostEditorPage.write_new_post`
|
713
|
+
* `pages.BlogPostEditorPage.form.title`
|
714
|
+
* `pages.BlogPostEditorPage.form.content`
|
715
|
+
* `pages.BlogPostEditorPage.form.post`
|
716
|
+
|
717
|
+
Give them values like so:
|
718
|
+
|
719
|
+
```ruby
|
720
|
+
# All app-specific translations, or overrides of Brut's defaults, go here.
|
721
|
+
{
|
722
|
+
# en: must be the first entry, thus indicating this is the EN translations
|
723
|
+
en: {
|
724
|
+
cv: {
|
725
|
+
cs: {
|
726
|
+
},
|
727
|
+
ss: {
|
728
|
+
email_taken: "This email has been taken",
|
729
|
+
},
|
730
|
+
},
|
731
|
+
pages: { # Page-specific messages - this key is relied-upon by Brut to exist
|
732
|
+
HomePage: {
|
733
|
+
title: "Welcome!",
|
734
|
+
},
|
735
|
+
BlogPostEditorPage: {
|
736
|
+
title: "BlogPostEditorPage",
|
737
|
+
write_new_post: "Write a new post!",
|
738
|
+
form: {
|
739
|
+
title: "Title",
|
740
|
+
content: "Post Content",
|
741
|
+
post: "Post It!",
|
742
|
+
}
|
743
|
+
},
|
744
|
+
},
|
745
|
+
# ... more code
|
746
|
+
```
|
747
|
+
|
748
|
+
Now, when you reload the page, it should work:
|
749
|
+
|
750
|
+

|
751
|
+
|
752
|
+
Without filling anything in, click the submit button. The form should show you some error messages that are unstyled:
|
753
|
+
|
754
|
+

|
755
|
+
|
756
|
+
Let's style them and learn about how the `<brut-cv>` tags created by `ConstraintViolations` work.
|
757
|
+
|
758
|
+
### Styling Constraint Violations
|
759
|
+
|
760
|
+
If you view source, you should see HTML like so:
|
761
|
+
|
762
|
+
```html
|
763
|
+
<brut-cv-messages input-name='title'>
|
764
|
+
</brut-cv-messages>
|
765
|
+
```
|
766
|
+
|
767
|
+
If you click submit and view source, you'll see something like this:
|
768
|
+
|
769
|
+
```html
|
770
|
+
<brut-cv-messages input-name='title'>
|
771
|
+
<brut-cv>This field is required</brut-cv>
|
772
|
+
</brut-cv-messages>
|
773
|
+
```
|
774
|
+
|
775
|
+
This was inserted by `<brut-form>` whenever an element of the form is invalid. This could happen before the visitor clicks "submit", however. To allow you to style these elements only when a submit has been attempted, `<brut-form>` will set the attribute `submitted-invalid` on itself when this happens.
|
776
|
+
|
777
|
+
Open `app/src/front_end/css/index.css` in your editor. We want `<brut-cv>` messages to be red, bold, and in the body font size. We also want them hidden by default.
|
778
|
+
|
779
|
+
```css
|
780
|
+
brut-cv {
|
781
|
+
display: none;
|
782
|
+
color: #A60053;
|
783
|
+
font-weight: bold;
|
784
|
+
font-size: 1rem;
|
785
|
+
}
|
786
|
+
```
|
787
|
+
|
788
|
+
When `submitted-invalid` is set on `brut-form`, *then* we show them. We *also* want to show them if they were generated from the server, which `ConstraintViolations` will do:
|
789
|
+
|
790
|
+
```css
|
791
|
+
brut-form[submitted-invalid] brut-cv,
|
792
|
+
brut-cv[server-side] {
|
793
|
+
display: block;
|
794
|
+
}
|
795
|
+
```
|
796
|
+
|
797
|
+
Let's also do some styling for the form and its elements. Add this below the CSS you just wrote:
|
798
|
+
|
799
|
+
```css
|
800
|
+
.BlogPostEditorPage {
|
801
|
+
brut-form {
|
802
|
+
display: block;
|
803
|
+
padding: 1rem;
|
804
|
+
border: solid thin gray;
|
805
|
+
border-radius: 0.25rem;
|
806
|
+
background-color: #eeeeee;
|
807
|
+
|
808
|
+
form {
|
809
|
+
display: flex;
|
810
|
+
flex-direction: column;
|
811
|
+
gap: 1rem;
|
812
|
+
align-items: start;
|
813
|
+
}
|
814
|
+
|
815
|
+
input, textarea {
|
816
|
+
width: 100%;
|
817
|
+
padding: 0.5rem;
|
818
|
+
font-size: 130%;
|
819
|
+
}
|
820
|
+
label {
|
821
|
+
width: 100%;
|
822
|
+
font-size: 120%;
|
823
|
+
display: block;
|
824
|
+
div {
|
825
|
+
font-weight: bold;
|
826
|
+
font-style: italic;
|
827
|
+
}
|
828
|
+
}
|
829
|
+
button {
|
830
|
+
padding-left: 2rem;
|
831
|
+
padding-right: 2rem;
|
832
|
+
padding-top: 1rem;
|
833
|
+
padding-bottom: 1rem;
|
834
|
+
background-color: #E5FFE5;
|
835
|
+
border: solid thin #006300;
|
836
|
+
color: #006300;
|
837
|
+
border-radius: 1rem;
|
838
|
+
font-size: 150%;
|
839
|
+
align-self: end;
|
840
|
+
cursor: pointer;
|
841
|
+
&:hover {
|
842
|
+
background-color: #ACFFAC;
|
843
|
+
}
|
844
|
+
}
|
845
|
+
}
|
846
|
+
}
|
847
|
+
```
|
848
|
+
|
849
|
+
Two notes about this CSS:
|
850
|
+
|
851
|
+
* It's using nesting, which is part of
|
852
|
+
[Baseline](https://developer.mozilla.org/en-US/docs/Glossary/Baseline/Compatibility)
|
853
|
+
* We've nested all the CSS inside the `.BlogPostEditorPage` class. The default layout Brut provides
|
854
|
+
includes this:
|
855
|
+
|
856
|
+
```ruby
|
857
|
+
body(class: @page_name) do
|
858
|
+
yield
|
859
|
+
end
|
860
|
+
```
|
861
|
+
|
862
|
+
This means all pages have their page name set on the `<body>` tag, allowing nested CSS, if you like.
|
863
|
+
|
864
|
+
*Now*, if you submit the form without providing any values, you'll see a decent-looking experience:
|
865
|
+
|
866
|
+

|
867
|
+
|
868
|
+
If you fill out the fields correctly, you should see an error that you need to implement your handler. Let's do that next.
|
869
|
+
|
870
|
+
## Handling Form Submissions
|
871
|
+
|
872
|
+
When you ran `bin/scaffold form` earlier, it created `NewBlogPostHandler`. It's located in `app/src/front_end/handlers/new_blog_post_handler.rb`, which should look like so:
|
873
|
+
|
874
|
+
```ruby
|
875
|
+
class NewBlogPostHandler < AppHandler
|
876
|
+
def initialize(form:)
|
877
|
+
@form = form
|
878
|
+
end
|
879
|
+
|
880
|
+
def handle
|
881
|
+
raise "You need to implement your handler"
|
882
|
+
end
|
883
|
+
end
|
884
|
+
```
|
885
|
+
|
886
|
+
The `handle` method is expected to return a value that tells Brut how to respond to a form submission. In our case, we'll either want it to re-generate `BlogPostEditorPage`'s HTML with error messages and the visitor-supplied form fields pre-filled in, or save the blog post and redirect back to `HomePage`.
|
887
|
+
|
888
|
+
To do that, we'll either return an instance of `BlogPostEditorPage`, or return a `URI` to `HomePage` (which we can do with the helper method `redirect_to`).
|
889
|
+
|
890
|
+
Before `handle` is called, `NewBlogPostHandler` will be initialized and give an instance of `NewBlogPostForm` containing whatever data was submitted by the browser. `handle` can then use `@form` to determine what to do.
|
891
|
+
|
892
|
+
First, we'll re-check client-side validations by calling `.valid?`. If that's true, we can perform server-side validations, calling `server_side_constraint_violation` for any violations we find. Then, we'll check `.valid?` again and return a `BlogPostEditorPage` or redirect.
|
893
|
+
|
894
|
+
```ruby
|
895
|
+
class NewBlogPostHandler < AppHandler
|
896
|
+
def initialize(form:)
|
897
|
+
@form = form
|
898
|
+
end
|
899
|
+
|
900
|
+
def handle
|
901
|
+
if @form.valid?
|
902
|
+
if @form.content.split(/\s/).length < 5
|
903
|
+
@form.server_side_constraint_violation(
|
904
|
+
input_name: :content,
|
905
|
+
key: :not_enough_words,
|
906
|
+
context: { num_words: 5 }
|
907
|
+
)
|
908
|
+
end
|
909
|
+
end
|
910
|
+
if @form.valid?
|
911
|
+
# TODO: Actually save the post
|
912
|
+
redirect_to(HomePage)
|
913
|
+
else
|
914
|
+
BlogPostEditorPage.new(form: @form)
|
915
|
+
end
|
916
|
+
end
|
917
|
+
end
|
918
|
+
```
|
919
|
+
|
920
|
+
Of course, `BlogPostEditorPage` does not accept the form as a parameter. We'll need to change that. Since we are using the `@form` instance to help generate HTML, if we pass the instance from our handler to the `BlogPostEditorPage`, when *that* instance generates HTML, it will have errors indicated and show the visitor's provided values instead of defaults.
|
921
|
+
|
922
|
+
Of course, we still need to create a blank form when the page is accessed for the first time, so we'll make `form:` default to `nil` and create it if we aren't given a value:
|
923
|
+
|
924
|
+
```ruby{2,3}
|
925
|
+
class BlogPostEditorPage < AppPage
|
926
|
+
|
927
|
+
def initialize(form: nil)
|
928
|
+
@form = form || NewBlogPostForm.new
|
929
|
+
end
|
930
|
+
```
|
931
|
+
|
932
|
+
With this in place, create a new blog post but with only four words in the content. This will pass client-side checks, but fail server-side checks. When you submit, you'll see an error related to `cv.ss.not_enough_words`, which is the key Brut is trying to use to find the actual error message.
|
933
|
+
|
934
|
+
> `Translation missing. Options considered were: -
|
935
|
+
> en.components.Brut::FrontEnd::Components::ConstraintViolations.cv.ss.not_enough_words -
|
936
|
+
> en.cv.ss.not_enough_words`
|
937
|
+
|
938
|
+
Add it to `app/config/i18n/en/2_app.rb`, under `en`, `cv` (for constraint violations), `ss` (for server-side):
|
939
|
+
|
940
|
+
```ruby {10}
|
941
|
+
# All app-specific translations, or overrides of Brut's defaults, go here.
|
942
|
+
{
|
943
|
+
# en: must be the first entry, thus indicating this is the EN translations
|
944
|
+
en: {
|
945
|
+
cv: {
|
946
|
+
cs: {
|
947
|
+
},
|
948
|
+
ss: {
|
949
|
+
email_taken: "This email has been taken",
|
950
|
+
not_enough_words: "%{field} does not have enough words. Must have %{num_words}",
|
951
|
+
},
|
952
|
+
},
|
953
|
+
```
|
954
|
+
|
955
|
+
*Now*, try again, and you'll see this message, rendered exactly like client-side errors:
|
956
|
+
|
957
|
+

|
958
|
+
|
959
|
+
Now that we have the user experience in place, let's actually store the blog post in the database.
|
960
|
+
|
961
|
+
## Using a Database
|
962
|
+
|
963
|
+
Brut uses Postgres, and includes and configures the [Sequel](https://sequel.jeremyevans.net/) library to access your data. Sequel has some similarity to Rails' Active Record, but it's not quite the same.
|
964
|
+
|
965
|
+
The main way to access data is to create a *database model* class (which is similar to an Active Record). Sequel also provides a way to manage your database schema via *migrations*.
|
966
|
+
|
967
|
+
The steps to take when creating a new table you want to access are:
|
968
|
+
|
969
|
+
1. Create a migration that creates the schema for the new table.
|
970
|
+
2. Create the database model class itself.
|
971
|
+
3. Create a FactoryBot factory that can create sample instances of rows in the table, useful for testing and development
|
972
|
+
4. Modify seed data to create sample data for development.
|
973
|
+
|
974
|
+
Most of this can be done via `bin/scaffold db_model`.
|
975
|
+
|
976
|
+
### Creating a New Database Table
|
977
|
+
|
978
|
+
Stop your server and run `bin/scaffold` like so:
|
979
|
+
|
980
|
+
```bash
|
981
|
+
bin/scaffold db_model blog_post
|
982
|
+
```
|
983
|
+
|
984
|
+
```
|
985
|
+
# OUTPUT
|
986
|
+
[ bin/scaffold ] Executing ["bin/db new_migration create_blog_post"]
|
987
|
+
[ bin/db ] Migration created:
|
988
|
+
app/src/back_end/data_models/migrations/20250811213758_create_blog_post.rb
|
989
|
+
[ bin/scaffold ] ["bin/db new_migration create_blog_post"] succeeded
|
990
|
+
[ bin/scaffold ] Creating DB::BlogPost in app/src/back_end/data_models/db/blog_post.rb
|
991
|
+
[ bin/scaffold ] Creating spec for DB::BlogPost in specs/back_end/data_models/db/blog_post.spec.rb
|
992
|
+
[ bin/scaffold ] Creating factory for DB::BlogPost in specs/factories/db/blog_post.factory.rb
|
993
|
+
```
|
994
|
+
|
995
|
+
Your migration file name will be different than ours, since it has a timestamp embedded into it.
|
996
|
+
|
997
|
+
Open that file in your editor and use `create_table`, as provided by Sequel, to describe the table.
|
998
|
+
|
999
|
+
Brut enhances Sequel's `create_table` in the following ways:
|
1000
|
+
|
1001
|
+
* A numeric primary key called `id` is created.
|
1002
|
+
* `comment:` is required.
|
1003
|
+
* `external_id` can be given, and will create a managed unique key called `external_id` that is safe to externalize and is not used in foreign key or referential integrity.
|
1004
|
+
* A timestamped field, `created_at` is created and will be set when new rows are created from your app.
|
1005
|
+
|
1006
|
+
Inside `create_table`, we'll call `column` to define columns. Brut defaults all columns to `NOT NULL`, so you don't need to specify `null: false`.
|
1007
|
+
|
1008
|
+
All of this goes inside a block given to the method `up`, like so:
|
1009
|
+
|
1010
|
+
```ruby
|
1011
|
+
Sequel.migration do
|
1012
|
+
up do
|
1013
|
+
create_table :blog_posts,
|
1014
|
+
comment: "All the posts fit to post",
|
1015
|
+
external_id: true do
|
1016
|
+
column :title, :text
|
1017
|
+
column :content, :text
|
1018
|
+
end
|
1019
|
+
end
|
1020
|
+
end
|
1021
|
+
```
|
1022
|
+
|
1023
|
+
If you've used migrations before, you may know that `down` can be used to specify a way to undo the migration, or that a method like `change` can be used to automatically do both. Brut recommends only using forward migrations inside `up`. If you need to undo and redo your changes, you can use `bin/db rebuild` to rebuild your database from scratch.
|
1024
|
+
|
1025
|
+
Save this file, then apply this migration to your development database:
|
1026
|
+
|
1027
|
+
```bash
|
1028
|
+
bin/db migrate
|
1029
|
+
```
|
1030
|
+
|
1031
|
+
```
|
1032
|
+
# OUTPUT
|
1033
|
+
[ bin/db ] Migrations applied
|
1034
|
+
```
|
1035
|
+
|
1036
|
+
Now, apply it to your test database:
|
1037
|
+
|
1038
|
+
```bash
|
1039
|
+
bin/db migrate -e test
|
1040
|
+
```
|
1041
|
+
|
1042
|
+
```
|
1043
|
+
# OUTPUT
|
1044
|
+
[ bin/db ] Migrations applied
|
1045
|
+
```
|
1046
|
+
|
1047
|
+
You can examine the table that was created by running `bin/dbconsole`:
|
1048
|
+
|
1049
|
+
```bash
|
1050
|
+
bin/dbconsole
|
1051
|
+
```
|
1052
|
+
|
1053
|
+
```
|
1054
|
+
# OUTPUT
|
1055
|
+
psql (16.9 (Debian 16.9-1.pgdg120+1), server 16.4 (Debian 16.4-1.pgdg120+2))
|
1056
|
+
Type "help" for help.
|
1057
|
+
|
1058
|
+
blog_development=#
|
1059
|
+
```
|
1060
|
+
|
1061
|
+
This will give you a new prompt where you can type commands to `psql`, the Postgres command-line client. Try describing the table:
|
1062
|
+
|
1063
|
+
```bash
|
1064
|
+
\d blog_posts
|
1065
|
+
```
|
1066
|
+
|
1067
|
+
```
|
1068
|
+
Table "public.blog_posts"
|
1069
|
+
Column | Type | Collation | Nullable | Default
|
1070
|
+
-------------+--------------------------+-----------+----------+----------------------------------
|
1071
|
+
id | integer | | not null | generated by default as identity
|
1072
|
+
title | text | | not null |
|
1073
|
+
content | text | | not null |
|
1074
|
+
created_at | timestamp with time zone | | not null |
|
1075
|
+
external_id | citext | | not null |
|
1076
|
+
Indexes:
|
1077
|
+
"blog_posts_pkey" PRIMARY KEY, btree (id)
|
1078
|
+
"blog_posts_external_id_key" UNIQUE CONSTRAINT, btree (external_id)
|
1079
|
+
```
|
1080
|
+
|
1081
|
+
`bin/scaffold` created the database model for you in `app/src/back_end/data_models/db/blog_post.rb`:
|
1082
|
+
|
1083
|
+
```ruby
|
1084
|
+
class DB::BlogPost < AppDataModel
|
1085
|
+
has_external_id :bl
|
1086
|
+
end
|
1087
|
+
```
|
1088
|
+
|
1089
|
+
In Brut, database models are in the `DB::` namespace, so you do not conflate them with a *domain* model.
|
1090
|
+
|
1091
|
+
> [!TIP]
|
1092
|
+
> Note `has_external_id`. This tells Brut and Sequel that the underlying table is expected
|
1093
|
+
> to have the field `external_id` and that it is expected to be unique. You can see this in
|
1094
|
+
> the output of `\d blog_posts`, where it says `UNIQUE CONSTRAINT, btree (external_id)`.
|
1095
|
+
>
|
1096
|
+
> `has_external_id` configures the database model to provide a value for this key when saving or
|
1097
|
+
> creating a row. To aid in understanding the values out of context, external ids are prefixed
|
1098
|
+
> with two values: one is an app-wide value, in our case `bl`. The other is a model-specific
|
1099
|
+
> value, also `bl`. Thus, external ids might look like `blbl_9783245789345789345789345`.
|
1100
|
+
>
|
1101
|
+
Before we use this database model, we want to be able to create instances in tests, as well as for local development. The way to do that in Brut is to create a factory.
|
1102
|
+
|
1103
|
+
### Creating Test and Development Data
|
1104
|
+
|
1105
|
+
Brut uses [FactoryBot](https://github.com/thoughtbot/factory_bot) to create sample instance of your data. Open up `specs/factories/db/blog_post.factory.rb` in your editor.
|
1106
|
+
|
1107
|
+
If you are new to FactoryBot, it is a lightweight (ish) DSL that allows creating test data. You'll call methods based on the column names in order to specify values. Brut also includes [Faker](https://github.com/faker-ruby/faker), which creates randomized but realistic test data.
|
1108
|
+
|
1109
|
+
For the title, we'll use Faker's "hipster ipsum". For the content, we want several paragraphs delineated by `\n\r`, so we'll create between 2 and 6 paragraphs and join them.
|
1110
|
+
|
1111
|
+
Make `specs/factories/db/blog_post.factory.rb` look like so:
|
1112
|
+
|
1113
|
+
```ruby
|
1114
|
+
FactoryBot.define do
|
1115
|
+
factory :blog_post, class: "DB::BlogPost" do
|
1116
|
+
title { Faker::Hipster.sentence }
|
1117
|
+
content {
|
1118
|
+
(rand(4) + 2).times.map {
|
1119
|
+
Faker::Hipster.paragraph_by_chars(characters: 200)
|
1120
|
+
}.join("\n\r")
|
1121
|
+
}
|
1122
|
+
end
|
1123
|
+
end
|
1124
|
+
```
|
1125
|
+
|
1126
|
+
Brut includes a test to make sure your factories are valid and will work. It's in `specs/lint_factories.spec.rb`. Run it now to make sure this factory works:
|
1127
|
+
|
1128
|
+
```bash
|
1129
|
+
bin/test run specs/lint_factories.spec.rb
|
1130
|
+
```
|
1131
|
+
|
1132
|
+
```
|
1133
|
+
# OUTPUT
|
1134
|
+
[ bin/test ] Executing ["bin/rspec -I /Users/davec/Projects/ThirdTank/brutcasts/01-make-a-blog/blog/specs -I /Users/davec/Projects/ThirdTank/brutcasts/01-make-a-blog/blog/app/src -I lib/ --tag ~e2e -P \"**/*.spec.rb\" \"specs/lint_factories.spec.rb\""]
|
1135
|
+
Run options: exclude {e2e: true}
|
1136
|
+
|
1137
|
+
Randomized with seed 29315
|
1138
|
+
|
1139
|
+
factories
|
1140
|
+
should be possible to create them all
|
1141
|
+
|
1142
|
+
Finished in 0.59465 seconds (files took 0.7718 seconds to load)
|
1143
|
+
1 example, 0 failures
|
1144
|
+
|
1145
|
+
Randomized with seed 29315
|
1146
|
+
|
1147
|
+
[ bin/test ] ["bin/rspec -I /Users/davec/Projects/ThirdTank/brutcasts/01-make-a-blog/blog/specs -I /Users/davec/Projects/ThirdTank/brutcasts/01-make-a-blog/blog/app/src -I lib/ --tag ~e2e -P \"**/*.spec.rb\" \"specs/lint_factories.spec.rb\""] succeeded
|
1148
|
+
```
|
1149
|
+
|
1150
|
+
We can use this factory for seed data to provide realistic data for development. Edit `app/src/back_end/data_models/seed/seed_data.rb`, and make it look like so, which will create 10 blog posts:
|
1151
|
+
|
1152
|
+
```ruby
|
1153
|
+
require "brut/back_end/seed_data"
|
1154
|
+
class SeedData < Brut::BackEnd::SeedData
|
1155
|
+
include FactoryBot::Syntax::Methods
|
1156
|
+
def seed!
|
1157
|
+
10.times do |i|
|
1158
|
+
create(:blog_post, created_at: Date.today - i)
|
1159
|
+
end
|
1160
|
+
end
|
1161
|
+
end
|
1162
|
+
```
|
1163
|
+
|
1164
|
+
`create` is a method provided by Factory Bot that is brought in via `FactoryBot::Syntax::Methods`.
|
1165
|
+
|
1166
|
+
Now, load the seed data into the development database with `bin/db seed`:
|
1167
|
+
|
1168
|
+
```bash
|
1169
|
+
bin/db seed
|
1170
|
+
```
|
1171
|
+
|
1172
|
+
We can now show this data on the home page.
|
1173
|
+
|
1174
|
+
## Accessing the Database
|
1175
|
+
|
1176
|
+
On `HomePage`, we put in a `<p>` as a placeholder for blog posts. Let's replace that with code to fetch and display the blog posts.
|
1177
|
+
|
1178
|
+
In Brut, since HTML is generated by Phlex and thus by Ruby code, we can structure our HTML generation however we like, including by accessing the database directly. This may not scale as our app gets large, but for now, it's the simplest thing to do.
|
1179
|
+
|
1180
|
+
Sequel's database models are similar to Rails' Active Record's in that we can call class methods to access data. In this case, `DB::BlogPost` has a method `order` that will fetch all records sorted by the field we give it in the order we decide. The sort field and order is specified via `Sequel.desc` for descending or `Sequel.asc` for ascending. We want posts in reverse-chronological order, so `Sequel.desc(:created_at)` will achieve this.
|
1181
|
+
|
1182
|
+
We can call `.each` on the result and iterate over each blog post. For the content, we'll split by `\n\r` to create paragraphs.
|
1183
|
+
|
1184
|
+
Here's what `HomePage`'s `page_template` should look like now:
|
1185
|
+
|
1186
|
+
```ruby
|
1187
|
+
def page_template
|
1188
|
+
header do
|
1189
|
+
h1 { "My Amazing Blog" }
|
1190
|
+
a(href: "") { "Write New Blog Post" }
|
1191
|
+
end
|
1192
|
+
main do
|
1193
|
+
DB::BlogPost.order(Sequel.desc(:created_at)).each do |blog_post|
|
1194
|
+
article do
|
1195
|
+
h2 { blog_post.title }
|
1196
|
+
blog_post.content.split(/\n\r/).each do |paragraph|
|
1197
|
+
p { paragraph }
|
1198
|
+
end
|
1199
|
+
end
|
1200
|
+
hr
|
1201
|
+
end
|
1202
|
+
end
|
1203
|
+
end
|
1204
|
+
```
|
1205
|
+
|
1206
|
+
Start your server if you stopped it before. Go to the home page, and you should see our fake blog posts:
|
1207
|
+
|
1208
|
+

|
1209
|
+
|
1210
|
+
If we modify our handler to save new posts to the database, they'll show up here.
|
1211
|
+
|
1212
|
+
## Saving to the Database
|
1213
|
+
|
1214
|
+
To create rows in the database, the class method `create` can be called on `DB::BlogPost`. Let's change the handler to use that. Open up `app/src/front_end/handlers/new_blog_post_handler.rb` and make `handle` look like so (the changed lines are highlighted):
|
1215
|
+
|
1216
|
+
```ruby {12-15}
|
1217
|
+
def handle
|
1218
|
+
if !@form.constraint_violations?
|
1219
|
+
if @form.content.split(/\s/).length < 5
|
1220
|
+
@form.server_side_constraint_violation(
|
1221
|
+
input_name: :content,
|
1222
|
+
key: :not_enough_words,
|
1223
|
+
context: { num_words: 5 }
|
1224
|
+
)
|
1225
|
+
end
|
1226
|
+
end
|
1227
|
+
if @form.valid?
|
1228
|
+
DB::BlogPost.create(
|
1229
|
+
title: @form.title,
|
1230
|
+
content: @form.content
|
1231
|
+
)
|
1232
|
+
redirect_to(HomePage)
|
1233
|
+
else
|
1234
|
+
NewBlogPostPage.new(form: @form)
|
1235
|
+
end
|
1236
|
+
end
|
1237
|
+
```
|
1238
|
+
|
1239
|
+
The form object provides access to the values of any field we've declared via a method call.
|
1240
|
+
|
1241
|
+
Now, create a new blog post, provide valid data, and submit it.
|
1242
|
+
|
1243
|
+

|
1244
|
+
|
1245
|
+
Once you submit it, you should see the homage page with your new post at the top:
|
1246
|
+
|
1247
|
+

|
1248
|
+
|
1249
|
+
Our work isn't quite done. We need tests.
|
1250
|
+
|
1251
|
+
## Testing Brut Apps
|
1252
|
+
|
1253
|
+
We'll create the following tests:
|
1254
|
+
|
1255
|
+
* Check that the logic in the handler is sound
|
1256
|
+
* Check that blog posts show up on the home page
|
1257
|
+
* Check that the entire workflow of create a blog post and seeing it show up on the home page works in a
|
1258
|
+
real web browser
|
1259
|
+
|
1260
|
+
Let's test our handler first, as that is where the main logic is.
|
1261
|
+
|
1262
|
+
### Testing Handlers
|
1263
|
+
|
1264
|
+
Our handler will need three tests:
|
1265
|
+
|
1266
|
+
* If the form was submitted without client-side validations happening, we should not create a new blog
|
1267
|
+
post and re-generate the blog post editor page, showing the errors.
|
1268
|
+
* If client-side validations pass, but the blog post isn't five words or more, we should not create a
|
1269
|
+
new blog post and re-generate the blog post editor page, showing the errors.
|
1270
|
+
* If everything looks good, we save the new blog post and redirect to the home page.
|
1271
|
+
|
1272
|
+
Brut apps are tested with RSpec, and Brut provides several convenience methods and matchers to make testing as painless as possible.
|
1273
|
+
|
1274
|
+
When testing a handler, the public method is `handle!`, not `handle`, so we want to call that (Brut implements `handle!` to call `handle`).
|
1275
|
+
|
1276
|
+
First, we'll test client-side validations. Open up `specs/front_end/handlers/new_blog_post_handler.spec.rb` and replace the code there with this:
|
1277
|
+
|
1278
|
+
```ruby
|
1279
|
+
require "spec_helper"
|
1280
|
+
|
1281
|
+
RSpec.describe NewBlogPostHandler do
|
1282
|
+
describe "#handle!" do
|
1283
|
+
context "client-side violations got to the server" do
|
1284
|
+
it "re-generates the HTML for the BlogPostEditorPage" do
|
1285
|
+
form = NewBlogPostForm.new(params: {})
|
1286
|
+
|
1287
|
+
result = described_class.new(form:).handle!
|
1288
|
+
|
1289
|
+
expect(result).to have_generated(BlogPostEditorPage)
|
1290
|
+
expect(form).to have_constraint_violation(:title, key: :valueMissing)
|
1291
|
+
expect(form).to have_constraint_violation(:content, key: :valueMissing)
|
1292
|
+
end
|
1293
|
+
end
|
1294
|
+
end
|
1295
|
+
end
|
1296
|
+
```
|
1297
|
+
|
1298
|
+
`have_generated` asserts that the value returned from `handle!` is an instance of the page given, `BlogPostEditorPage` in this case. You could just as easily write `expect(result.kind_of?(BlogPostEditorPage)).to eq(true)`, but `have_generated` expressed the intent of what's happening.
|
1299
|
+
|
1300
|
+
`have_constraint_violation` checks that the form's `constraint_violations` contains one for the given field and the given key. In this case, we check both `:title` and `:content` for `:valueMissing`.
|
1301
|
+
|
1302
|
+
> [!TIP]
|
1303
|
+
> Client-side constraint violations use the same keys on the server as they do in the browser.
|
1304
|
+
> In the case of a required field, the browser's
|
1305
|
+
> [`ValidityState`](https://developer.mozilla.org/en-US/docs/Web/API/ValidityState) would set
|
1306
|
+
> `valueMissing` to true. So, that's what Brut would do on the server-side, when reflecting
|
1307
|
+
> client-side constraints.
|
1308
|
+
|
1309
|
+
Next, we'll check that the server-side constraint violations are being checked. Add this just below the `context` you just added:
|
1310
|
+
|
1311
|
+
```ruby
|
1312
|
+
context "post is not enough words" do
|
1313
|
+
it "re-generates the HTML for the BlogPostEditorPage, with server-side errors indicated" do
|
1314
|
+
form = NewBlogPostForm.new(params: {
|
1315
|
+
title: "What a great post",
|
1316
|
+
content: "Not enough words",
|
1317
|
+
})
|
1318
|
+
|
1319
|
+
confidence_check { expect(form.constraint_violations?).to eq(false) }
|
1320
|
+
|
1321
|
+
result = described_class.new(form:).handle!
|
1322
|
+
|
1323
|
+
expect(result).to have_generated(BlogPostEditorPage)
|
1324
|
+
expect(form).to have_constraint_violation(:content, key: :not_enough_words)
|
1325
|
+
end
|
1326
|
+
end
|
1327
|
+
```
|
1328
|
+
|
1329
|
+
This test introduces two new concepts:
|
1330
|
+
|
1331
|
+
* To initialize a form with data, you must pass a `Hash` to the keyword argument `params:`. If the
|
1332
|
+
`Hash` contains parameters that the form doesn't recognize, they are ignored and discarded.
|
1333
|
+
* Although we aren't expecting the form to have client-side constraint violations, if there are any, the
|
1334
|
+
test would fail in a confusing way. To manage this, Brut includes the [confidence-check](https://github.com/sustainable-rails/confidence-check) gem that allows you to make assertions that are not part of the test. If the confidence check fails, the test output will be obvious that the test could not run due to an assumption being violated.
|
1335
|
+
|
1336
|
+
|
1337
|
+
Lastly, we'll check that everything worked when there aren't any constraint violations. Add this below the `context` you just added:
|
1338
|
+
|
1339
|
+
```ruby
|
1340
|
+
context "post is good!" do
|
1341
|
+
it "saves the post and redirects to the HomePage" do
|
1342
|
+
form = NewBlogPostForm.new(params: {
|
1343
|
+
title: "What a great post",
|
1344
|
+
content: "This post is the best post that has been written in the history of posts",
|
1345
|
+
})
|
1346
|
+
|
1347
|
+
confidence_check { expect(form.constraint_violations?).to eq(false) }
|
1348
|
+
|
1349
|
+
result = nil
|
1350
|
+
expect {
|
1351
|
+
result = described_class.new(form:).handle!
|
1352
|
+
}.to change { DB::BlogPost.count }.by(1)
|
1353
|
+
|
1354
|
+
expect(result).to have_redirected_to(HomePage)
|
1355
|
+
|
1356
|
+
blog_post = DB::BlogPost.last
|
1357
|
+
expect(blog_post.title).to eq("What a great post")
|
1358
|
+
expect(blog_post.content).to eq("This post is the best post that has been written in the history of posts")
|
1359
|
+
|
1360
|
+
end
|
1361
|
+
end
|
1362
|
+
```
|
1363
|
+
|
1364
|
+
This is using RSpec's `expect { ... }.to change { ... }.by(N)` to make sure that our handler created a row in the database. We then use the matcher `have_redirected_to` to assert that `result` is a URI to `HomePage`. We also check that the blog post we created in the database is correct.
|
1365
|
+
|
1366
|
+
Let's run the test with `bin/test run`
|
1367
|
+
|
1368
|
+
```bash
|
1369
|
+
bin/test run specs/front_end/handlers/new_blog_post_handler.spec.rb
|
1370
|
+
```
|
1371
|
+
|
1372
|
+
```
|
1373
|
+
# OUTPUT
|
1374
|
+
[ bin/test ] Executing ["bin/rspec -I /Users/davec/Projects/ThirdTank/brutcasts/01-make-a-blog/blog/specs -I /Users/davec/Projects/ThirdTank/brutcasts/01-make-a-blog/blog/app/src -I lib/ --tag ~e2e -P \"**/*.spec.rb\" \"specs/front_end/handlers/new_blog_post_handler.spec.rb\""]
|
1375
|
+
Run options: exclude {e2e: true}
|
1376
|
+
|
1377
|
+
Randomized with seed 61034
|
1378
|
+
|
1379
|
+
NewBlogPostHandler
|
1380
|
+
post is not enough words
|
1381
|
+
re-generates the HTML for the BlogPostEditorPage, with server-side errors indicated
|
1382
|
+
post is good!
|
1383
|
+
saves the post and redirects to the HomePage
|
1384
|
+
#handle!
|
1385
|
+
client-side violations got to the server
|
1386
|
+
re-generates the HTML for the BlogPostEditorPage
|
1387
|
+
|
1388
|
+
Finished in 0.0138 seconds (files took 0.73976 seconds to load)
|
1389
|
+
3 examples, 0 failures
|
1390
|
+
|
1391
|
+
Randomized with seed 61034
|
1392
|
+
|
1393
|
+
[ bin/test ] ["bin/rspec -I /Users/davec/Projects/ThirdTank/brutcasts/01-make-a-blog/blog/specs -I /Users/davec/Projects/ThirdTank/brutcasts/01-make-a-blog/blog/app/src -I lib/ --tag ~e2e -P \"**/*.spec.rb\" \"specs/front_end/handlers/new_blog_post_handler.spec.rb\""] succeeded
|
1394
|
+
```
|
1395
|
+
|
1396
|
+
It passes!
|
1397
|
+
|
1398
|
+
Next, let's test `HomePage`.
|
1399
|
+
|
1400
|
+
### Testing Pages
|
1401
|
+
|
1402
|
+
Unlike our handler, which accepts arguments and returns a result, pages generate HTML. We are better off testing pages by asking them to generate HTML and then examining the HTML directly.
|
1403
|
+
|
1404
|
+
Brut provides the method `generate_and_parse` to generate a page's HTML, then use [Nokogiri](https://nokogiri.org/) to parse it. We can use CSS selectors on the result to assert things about the HTML.
|
1405
|
+
|
1406
|
+
`mkbrut` created `specs/front_end/pages/home_page.spec.rb`, so let's open that up on your editor.
|
1407
|
+
|
1408
|
+
The way we'll write this test is to generate four random blog posts using our factory, request the page, then assert that each blog post is on the page.
|
1409
|
+
|
1410
|
+
Rather than assert that each blog post's text is just somewhere on the page, we'll make use of the `external_id` concept. We'll use it as the `id` attribute of the `<article>`.
|
1411
|
+
|
1412
|
+
Brut intends for you to use Nokogiri's API to access information about the parsed document, however it provides a few convenience methods. In the test below, you'll see `e!`, which is added to Nokogiri nodes.
|
1413
|
+
|
1414
|
+
`e!` asserts that exactly one node matches the given CSS selector and returns that node. This makes it more expedient to access something that should be there, but fail with a useful error message when it's not.
|
1415
|
+
|
1416
|
+
Here's the test:
|
1417
|
+
|
1418
|
+
```ruby
|
1419
|
+
require "spec_helper"
|
1420
|
+
|
1421
|
+
RSpec.describe HomePage do
|
1422
|
+
it "should show the blog posts" do
|
1423
|
+
|
1424
|
+
blog_posts = 4.times.map { create(:blog_post) }
|
1425
|
+
|
1426
|
+
result = generate_and_parse(described_class.new)
|
1427
|
+
|
1428
|
+
expect(result.e!("h1").text).to eq("My Amazing Blog")
|
1429
|
+
|
1430
|
+
blog_posts.each do |blog_post|
|
1431
|
+
post_article = result.e!("article##{blog_post.external_id}")
|
1432
|
+
expect(post_article.e!("h2").text).to eq(blog_post.title)
|
1433
|
+
blog_post.content.split(/\n\r/).each do |paragraph|
|
1434
|
+
expect(post_article.text).to include(paragraph)
|
1435
|
+
end
|
1436
|
+
end
|
1437
|
+
end
|
1438
|
+
end
|
1439
|
+
```
|
1440
|
+
|
1441
|
+
Let's run the test, which should fail:
|
1442
|
+
|
1443
|
+
```bash
|
1444
|
+
bin/test run specs/front_end/pages/home_page.spec.rb
|
1445
|
+
```
|
1446
|
+
|
1447
|
+
```
|
1448
|
+
# OUTPUT
|
1449
|
+
[ bin/test ] Executing ["bin/rspec -I /Users/davec/Projects/ThirdTank/brutcasts/01-make-a-blog/blog/specs -I /Users/davec/Projects/ThirdTank/brutcasts/01-make-a-blog/blog/app/src -I lib/ --tag ~e2e -P \"**/*.spec.rb\" \"specs/front_end/pages/home_page.spec.rb\""]
|
1450
|
+
Run options: exclude {e2e: true}
|
1451
|
+
|
1452
|
+
Randomized with seed 44491
|
1453
|
+
|
1454
|
+
HomePage
|
1455
|
+
should show the blog posts (FAILED - 1)
|
1456
|
+
|
1457
|
+
Failures:
|
1458
|
+
|
1459
|
+
1) HomePage should show the blog posts
|
1460
|
+
Failure/Error: post_article = result.e!("article##{blog_post.external_id}")
|
1461
|
+
|
1462
|
+
article#blbl_6f04feaefb9520d86b19c3ac4ad22c4f matched 0 elements, not exactly 1:
|
1463
|
+
|
1464
|
+
«HUGE HTML DOCUMENT»
|
1465
|
+
|
1466
|
+
# ./local-gems/gem-home/gems/brut-0.5.0/lib/brut/spec_support/enhanced_node.rb:32:in 'Brut::SpecSupport::EnhancedNode#e!'
|
1467
|
+
# ./specs/front_end/pages/home_page.spec.rb:13:in 'block (3 levels) in <top (required)>'
|
1468
|
+
# ./specs/front_end/pages/home_page.spec.rb:12:in 'Array#each'
|
1469
|
+
# ./specs/front_end/pages/home_page.spec.rb:12:in 'block (2 levels) in <top (required)>'
|
1470
|
+
# ./local-gems/gem-home/gems/brut-0.5.0/lib/brut/spec_support/rspec_setup.rb:158:in 'block (2 levels) in Brut::SpecSupport::RSpecSetup#setup!'
|
1471
|
+
# ./local-gems/gem-home/gems/sequel-5.95.1/lib/sequel/database/transactions.rb:264:in 'Sequel::Database#_transaction'
|
1472
|
+
# ./local-gems/gem-home/gems/sequel-5.95.1/lib/sequel/database/transactions.rb:239:in 'block in Sequel::Database#transaction'
|
1473
|
+
# ./local-gems/gem-home/gems/sequel-5.95.1/lib/sequel/connection_pool/timed_queue.rb:90:in 'Sequel::TimedQueueConnectionPool#hold'
|
1474
|
+
# ./local-gems/gem-home/gems/sequel-5.95.1/lib/sequel/database/connecting.rb:283:in 'Sequel::Database#synchronize'
|
1475
|
+
# ./local-gems/gem-home/gems/sequel-5.95.1/lib/sequel/database/transactions.rb:197:in 'Sequel::Database#transaction'
|
1476
|
+
# ./local-gems/gem-home/gems/brut-0.5.0/lib/brut/spec_support/rspec_setup.rb:156:in 'block in Brut::SpecSupport::RSpecSetup#setup!'
|
1477
|
+
|
1478
|
+
Finished in 0.54876 seconds (files took 0.73025 seconds to load)
|
1479
|
+
1 example, 1 failure
|
1480
|
+
|
1481
|
+
Failed examples:
|
1482
|
+
|
1483
|
+
bin/test run ./specs/front_end/pages/home_page.spec.rb:4 # HomePage should show the blog posts
|
1484
|
+
|
1485
|
+
Randomized with seed 44491
|
1486
|
+
|
1487
|
+
[ bin/test ] error: ["bin/rspec -I /Users/davec/Projects/ThirdTank/brutcasts/01-make-a-blog/blog/specs -I /Users/davec/Projects/ThirdTank/brutcasts/01-make-a-blog/blog/app/src -I lib/ --tag ~e2e -P \"**/*.spec.rb\" \"specs/front_end/pages/home_page.spec.rb\""] failed - exited 1
|
1488
|
+
```
|
1489
|
+
|
1490
|
+
Brut obviously errs on the side of being verbose. But, you can see that the problem is that it cannot find an `<article>` with the `id=` of `blbl_6f04feaefb9520d86b19c3ac4ad22c4f`, the `external_id` of the first blog post.
|
1491
|
+
|
1492
|
+
To make it pass, we'll need to add `id:` to each `<article>`. Make this one-line change in `HomePage`:
|
1493
|
+
|
1494
|
+
```diff
|
1495
|
+
- article do
|
1496
|
+
+ article(id: blog_post.external_id) do
|
1497
|
+
```
|
1498
|
+
|
1499
|
+
> [!TIP]
|
1500
|
+
> This shows a useful feature of the `external_id`: Because it's not only unique
|
1501
|
+
> to the database table, but also across *all* database tables, it makes a pretty
|
1502
|
+
> good `ID` inside an HTML page, since it's highly unlikely any other part of the page
|
1503
|
+
> would use that value for the `id=` of an element.
|
1504
|
+
|
1505
|
+
Now, the test should pass:
|
1506
|
+
|
1507
|
+
```bash
|
1508
|
+
bin/test run specs/front_end/pages/home_page.spec.rb
|
1509
|
+
```
|
1510
|
+
|
1511
|
+
```
|
1512
|
+
# OUTPUT
|
1513
|
+
[ bin/test ] Executing ["bin/rspec -I /Users/davec/Projects/ThirdTank/brutcasts/01-make-a-blog/blog/specs -I /Users/davec/Projects/ThirdTank/brutcasts/01-make-a-blog/blog/app/src -I lib/ --tag ~e2e -P \"**/*.spec.rb\" \"specs/front_end/pages/home_page.spec.rb\""]
|
1514
|
+
Run options: exclude {e2e: true}
|
1515
|
+
|
1516
|
+
Randomized with seed 56951
|
1517
|
+
|
1518
|
+
HomePage
|
1519
|
+
should show the blog posts
|
1520
|
+
|
1521
|
+
Finished in 0.53858 seconds (files took 0.69257 seconds to load)
|
1522
|
+
1 example, 0 failures
|
1523
|
+
|
1524
|
+
Randomized with seed 56951
|
1525
|
+
|
1526
|
+
[ bin/test ] ["bin/rspec -I /Users/davec/Projects/ThirdTank/brutcasts/01-make-a-blog/blog/specs -I /Users/davec/Projects/ThirdTank/brutcasts/01-make-a-blog/blog/app/src -I lib/ --tag ~e2e -P \"**/*.spec.rb\" \"specs/front_end/pages/home_page.spec.rb\""] succeeded
|
1527
|
+
```
|
1528
|
+
|
1529
|
+
For `BlogPostEditorPage`, there really isn't anything to test - it's static HTML at this point. Even still, it's good to record a decision about testing code or not, so it's clear we didn't just forget. Brut provides the method `implementation_is_covered_by_other_tests` to do just that. It accepts a string where we can describe where the coverage for this class is.
|
1530
|
+
|
1531
|
+
In our case, it's going to be covered by an end-to-end test we'll write next.
|
1532
|
+
|
1533
|
+
Open up `specs/front_end/pages/blog_post_editor_page.spec.rb` and make it look like so:
|
1534
|
+
|
1535
|
+
```ruby
|
1536
|
+
require "spec_helper"
|
1537
|
+
|
1538
|
+
RSpec.describe BlogPostEditorPage do
|
1539
|
+
implementation_is_covered_by_other_tests "end-to-end test"
|
1540
|
+
end
|
1541
|
+
```
|
1542
|
+
|
1543
|
+
Now, all unit tests should pass, which we can check via `bin/test run`:
|
1544
|
+
|
1545
|
+
```bash
|
1546
|
+
bin/test run
|
1547
|
+
```
|
1548
|
+
|
1549
|
+
```
|
1550
|
+
# OUTPUT
|
1551
|
+
[ bin/test ] Running all tests
|
1552
|
+
[ bin/test ] Executing ["bin/rspec -I /Users/davec/Projects/ThirdTank/brutcasts/01-make-a-blog/blog/specs -I /Users/davec/Projects/ThirdTank/brutcasts/01-make-a-blog/blog/app/src -I lib/ --tag ~e2e -P \"**/*.spec.rb\" /Users/davec/Projects/ThirdTank/brutcasts/01-make-a-blog/blog/specs/"]
|
1553
|
+
Run options: exclude {e2e: true}
|
1554
|
+
|
1555
|
+
Randomized with seed 63173
|
1556
|
+
...........
|
1557
|
+
|
1558
|
+
Finished in 0.53248 seconds (files took 0.7012 seconds to load)
|
1559
|
+
11 examples, 0 failures
|
1560
|
+
|
1561
|
+
Randomized with seed 63173
|
1562
|
+
|
1563
|
+
[ bin/test ] ["bin/rspec -I /Users/davec/Projects/ThirdTank/brutcasts/01-make-a-blog/blog/specs -I /Users/davec/Projects/ThirdTank/brutcasts/01-make-a-blog/blog/app/src -I lib/ --tag ~e2e -P \"**/*.spec.rb\" /Users/davec/Projects/ThirdTank/brutcasts/01-make-a-blog/blog/specs/"] succeeded
|
1564
|
+
```
|
1565
|
+
|
1566
|
+
As our last test, we'll write an end-to-end that uses a browser.
|
1567
|
+
|
1568
|
+
### Writing Browser End-to-End Tests
|
1569
|
+
|
1570
|
+
Browser tests are expensive and slow, but it's good to test entire workflows that catch issues that a unit test might not. In this case, we want to go through the steps of writing a blog post:
|
1571
|
+
|
1572
|
+
1. Go to the post editor page and make sure client-side validations show us errors.
|
1573
|
+
2. Submit a post that's too short and make sure server-side errors show up.
|
1574
|
+
3. Submit a valid post and check that it makes it back to the home page.
|
1575
|
+
|
1576
|
+
Brut uses [Playwright](https://playwright.dev/) to author end to end tests. Playwright is written in JavaScript, but there is a [Ruby wrapper library](https://playwright-ruby-client.vercel.app/) that alleviates us from having to worry about async/await style coding.
|
1577
|
+
|
1578
|
+
Ideally, we'd use the same API here as we do in our page tests. Or, equally ideally, we'd be able to use the API of the web platform. Playwright went a third way. Such is life.
|
1579
|
+
|
1580
|
+
The way this test will work is:
|
1581
|
+
|
1582
|
+
1. Use `HomePage.routing` to kick everything off
|
1583
|
+
2. Find a link to `BlogPostEditorPage.routing` on the page
|
1584
|
+
3. Use Playwright's `page.locator` to find elements on the page to interact with (which will naturally wait for the page to load before doing so).
|
1585
|
+
4. We'll use `fill` to fill in data for the form fields and `click` to submit the form by clicking the submit button.
|
1586
|
+
5. The matcher `have_text` will be used assert that text appears inside an element.
|
1587
|
+
|
1588
|
+
Brut provides the matcher `be_page_for` to assert that we are viewing the page we think we are. Nothing is more frustrating than watching assertions fail because your test ended up on the wrong page.
|
1589
|
+
|
1590
|
+
Open up `specs/e2e/home_page.spec.rb` and replace it with this:
|
1591
|
+
|
1592
|
+
```ruby
|
1593
|
+
require "spec_helper"
|
1594
|
+
|
1595
|
+
RSpec.describe "Posting blog posts" do
|
1596
|
+
it "allows posting a post" do
|
1597
|
+
|
1598
|
+
# 1. Go to the blog post editor page from the home page
|
1599
|
+
page.goto(HomePage.routing)
|
1600
|
+
new_post_link = page.locator("a[href='#{BlogPostEditorPage.routing}']")
|
1601
|
+
new_post_link.click
|
1602
|
+
|
1603
|
+
expect(page).to be_page_for(BlogPostEditorPage)
|
1604
|
+
|
1605
|
+
# 2. Provide data that violates client-side constraints and check for error messages
|
1606
|
+
title_field = page.locator("brut-form input[name='title']")
|
1607
|
+
content_field = page.locator("brut-form textarea[name='content']")
|
1608
|
+
|
1609
|
+
title_field.fill("XX")
|
1610
|
+
|
1611
|
+
submit_button = page.locator("brut-form button")
|
1612
|
+
submit_button.click
|
1613
|
+
|
1614
|
+
expect(page).to be_page_for(BlogPostEditorPage)
|
1615
|
+
|
1616
|
+
title_error_message = page.locator("brut-cv-messages[input-name='title'] brut-cv")
|
1617
|
+
content_error_message = page.locator("brut-cv-messages[input-name='content'] brut-cv")
|
1618
|
+
|
1619
|
+
expect(title_error_message).to have_text("This field is too short")
|
1620
|
+
expect(content_error_message).to have_text("This field is required")
|
1621
|
+
|
1622
|
+
# 3. Provide data that passes client-side constraints but violates server-side ones
|
1623
|
+
title_field.fill("New blog post")
|
1624
|
+
content_field.fill("Too short")
|
1625
|
+
|
1626
|
+
submit_button.click
|
1627
|
+
|
1628
|
+
expect(page).to be_page_for(BlogPostEditorPage)
|
1629
|
+
|
1630
|
+
expect(content_error_message).to have_text("This field does not have enough words")
|
1631
|
+
|
1632
|
+
# 4. Provide a valid blog post
|
1633
|
+
content_field.fill("This is a longer post, so we should be OK")
|
1634
|
+
|
1635
|
+
submit_button.click
|
1636
|
+
expect(page).to be_page_for(HomePage)
|
1637
|
+
|
1638
|
+
new_post = DB::BlogPost.order(Sequel.desc(:created_at)).first
|
1639
|
+
|
1640
|
+
article = page.locator("article##{new_post.external_id}")
|
1641
|
+
|
1642
|
+
expect(article).to have_text("New blog post")
|
1643
|
+
expect(article).to have_text("This is a longer post, so we should be OK")
|
1644
|
+
|
1645
|
+
end
|
1646
|
+
end
|
1647
|
+
```
|
1648
|
+
|
1649
|
+
Run it now with `bin/test e2e`:
|
1650
|
+
|
1651
|
+
```bash
|
1652
|
+
bin/test e2e
|
1653
|
+
```
|
1654
|
+
|
1655
|
+
It should pass:
|
1656
|
+
|
1657
|
+
```
|
1658
|
+
# OUTPUT
|
1659
|
+
[ bin/test ] Rebuilding test database schema
|
1660
|
+
[ bin/test ] Executing ["bin/db rebuild --env=test"]
|
1661
|
+
[ bin/db ] Database exists. Dropping...
|
1662
|
+
[ bin/db ] blog_test does not exit. Creating...
|
1663
|
+
[ bin/db ] Migrations applied
|
1664
|
+
[ bin/test ] ["bin/db rebuild --env=test"] succeeded
|
1665
|
+
[ bin/test ] Running all tests
|
1666
|
+
[ bin/test ] Executing ["bin/rspec -I /Users/davec/Projects/ThirdTank/brutcasts/01-make-a-blog/blog/specs -I /Users/davec/Projects/ThirdTank/brutcasts/01-make-a-blog/blog/app/src -I lib/ --tag e2e -P \"**/*.spec.rb\" /Users/davec/Projects/ThirdTank/brutcasts/01-make-a-blog/blog/specs/"]
|
1667
|
+
Run options: include {e2e: true}
|
1668
|
+
|
1669
|
+
Randomized with seed 27681
|
1670
|
+
[ bin/test-server ] Building assets
|
1671
|
+
«TONS OF OUTPUT»
|
1672
|
+
[ bin/test-server ] Starting server
|
1673
|
+
[ bin/run ] No pidfile-Starting up
|
1674
|
+
[3352] Configuration:
|
1675
|
+
«TONS OF OUTPUT»
|
1676
|
+
[3352] Use Ctrl-C to stop
|
1677
|
+
[3352] - Worker 0 (PID: 3361) booted in 0.0s, phase: 0
|
1678
|
+
[3352] - Worker 1 (PID: 3364) booted in 0.0s, phase: 0
|
1679
|
+
.[3352] === puma shutdown: 2025-08-11 22:18:16 +0000 ===
|
1680
|
+
[3352] - Goodbye!
|
1681
|
+
[3352] - Gracefully shutting down workers...
|
1682
|
+
|
1683
|
+
|
1684
|
+
Finished in 3.45 seconds (files took 0.69401 seconds to load)
|
1685
|
+
1 example, 0 failures
|
1686
|
+
|
1687
|
+
Randomized with seed 27681
|
1688
|
+
|
1689
|
+
[ bin/test ] ["bin/rspec -I /Users/davec/Projects/ThirdTank/brutcasts/01-make-a-blog/blog/specs -I /Users/davec/Projects/ThirdTank/brutcasts/01-make-a-blog/blog/app/src -I lib/ --tag e2e -P \"**/*.spec.rb\" /Users/davec/Projects/ThirdTank/brutcasts/01-make-a-blog/blog/specs/"] succeeded
|
1690
|
+
[ bin/test ] Re-Rebuilding test database schema
|
1691
|
+
[ bin/test ] Executing ["bin/db rebuild --env=test"]
|
1692
|
+
[ bin/db ] Database exists. Dropping...
|
1693
|
+
[ bin/db ] blog_test does not exit. Creating...
|
1694
|
+
[ bin/db ] Migrations applied
|
1695
|
+
[ bin/test ] ["bin/db rebuild --env=test"] succeeded
|
1696
|
+
```
|
1697
|
+
|
1698
|
+
With that test done, `bin/ci`, which we ran at the start, should run all tests, plus check for CVEs in our installed gems.
|
1699
|
+
|
1700
|
+
```bash
|
1701
|
+
bin/ci
|
1702
|
+
```
|
1703
|
+
|
1704
|
+
It should also pass:
|
1705
|
+
|
1706
|
+
```
|
1707
|
+
# OUTPUT
|
1708
|
+
«TONS OF OUTPUT»
|
1709
|
+
[ bin/ci ] Analyzing Ruby gems for
|
1710
|
+
[ bin/ci ] security vulnerabilities
|
1711
|
+
Updating ruby-advisory-db ...
|
1712
|
+
From https://github.com/rubysec/ruby-advisory-db
|
1713
|
+
* branch master -> FETCH_HEAD
|
1714
|
+
Already up to date.
|
1715
|
+
Updated ruby-advisory-db
|
1716
|
+
ruby-advisory-db:
|
1717
|
+
advisories: 998 advisories
|
1718
|
+
last updated: 2025-08-08 10:26:18 -0700
|
1719
|
+
commit: 43149b540b701c9683e402fcd7fa4e5b6e5b60e9
|
1720
|
+
No vulnerabilities found
|
1721
|
+
[ bin/ci ] Checking to see that all classes have tests
|
1722
|
+
[ bin/test ] All tests exists!
|
1723
|
+
[ bin/ci ] Done
|
1724
|
+
```
|
1725
|
+
|
1726
|
+
That's it!
|
1727
|
+
|
1728
|
+
## Areas for Self-Exploration
|
1729
|
+
|
1730
|
+
Here are a few enhancement you can try to make:
|
1731
|
+
|
1732
|
+
* Create a client-side constraint requiring the title to match a certain regexp.
|
1733
|
+
* Add a server-side constraint requiring at least two paragraphs.
|
1734
|
+
* Allow editing the blog post creation date
|
1735
|
+
* Add an author field to allow entering the author's name
|
1736
|
+
* Add pagination to the home page
|