brut 0.10.0 → 0.11.0

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.
Files changed (376) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +14 -0
  3. data/Gemfile.lock +15 -15
  4. data/assets/YouTubeThumb.pxd +0 -0
  5. data/bin/new-version +3 -3
  6. data/brut-css/package-lock.json +2 -2
  7. data/brut-css/package.json +1 -1
  8. data/brut-js/package-lock.json +2 -2
  9. data/brut-js/package.json +1 -1
  10. data/brutrb.com/getting-started.md +3 -0
  11. data/brutrb.com/images/tutorial/02-confirmation-dialog-browser-element-styled.png +0 -0
  12. data/brutrb.com/images/tutorial/02-confirmation-dialog-browser-element.png +0 -0
  13. data/brutrb.com/images/tutorial/02-confirmation-dialog-browser.png +0 -0
  14. data/brutrb.com/images/tutorial/02-confirmation-flow.graffle +0 -0
  15. data/brutrb.com/images/tutorial/02-confirmation-flow.png +0 -0
  16. data/brutrb.com/instrumentation.md +142 -3
  17. data/brutrb.com/tutorial.md +29 -1627
  18. data/brutrb.com/tutorials/01-intro.md +1630 -0
  19. data/brutrb.com/tutorials/02-dialog.md +569 -0
  20. data/docs/404.html +2 -2
  21. data/docs/adrs.html +4 -4
  22. data/docs/ai.html +4 -4
  23. data/docs/api/Brut/BackEnd/SeedData.html +1 -1
  24. data/docs/api/Brut/BackEnd/Sidekiq/Middlewares/Server/FlushSpans.html +1 -1
  25. data/docs/api/Brut/BackEnd/Sidekiq/Middlewares/Server.html +1 -1
  26. data/docs/api/Brut/BackEnd/Sidekiq/Middlewares.html +1 -1
  27. data/docs/api/Brut/BackEnd/Sidekiq.html +1 -1
  28. data/docs/api/Brut/BackEnd/Validators/FormValidator.html +1 -1
  29. data/docs/api/Brut/BackEnd/Validators.html +1 -1
  30. data/docs/api/Brut/BackEnd.html +1 -1
  31. data/docs/api/Brut/CLI/App.html +1 -1
  32. data/docs/api/Brut/CLI/AppRunner.html +1 -1
  33. data/docs/api/Brut/CLI/Apps/BuildAssets/All.html +1 -1
  34. data/docs/api/Brut/CLI/Apps/BuildAssets/CSS.html +1 -1
  35. data/docs/api/Brut/CLI/Apps/BuildAssets/Images.html +1 -1
  36. data/docs/api/Brut/CLI/Apps/BuildAssets/JS.html +1 -1
  37. data/docs/api/Brut/CLI/Apps/BuildAssets.html +1 -1
  38. data/docs/api/Brut/CLI/Apps/DB/Create.html +1 -1
  39. data/docs/api/Brut/CLI/Apps/DB/Drop.html +1 -1
  40. data/docs/api/Brut/CLI/Apps/DB/Migrate.html +1 -1
  41. data/docs/api/Brut/CLI/Apps/DB/NewMigration.html +1 -1
  42. data/docs/api/Brut/CLI/Apps/DB/Rebuild.html +1 -1
  43. data/docs/api/Brut/CLI/Apps/DB/Seed.html +1 -1
  44. data/docs/api/Brut/CLI/Apps/DB/Status.html +1 -1
  45. data/docs/api/Brut/CLI/Apps/DB.html +1 -1
  46. data/docs/api/Brut/CLI/Apps/DeployBase/GitChecks.html +1 -1
  47. data/docs/api/Brut/CLI/Apps/DeployBase.html +1 -1
  48. data/docs/api/Brut/CLI/Apps/HerokuContainerBasedDeploy/Deploy.html +1 -1
  49. data/docs/api/Brut/CLI/Apps/HerokuContainerBasedDeploy.html +1 -1
  50. data/docs/api/Brut/CLI/Apps/Scaffold/Action/Route.html +1 -1
  51. data/docs/api/Brut/CLI/Apps/Scaffold/Action.html +1 -1
  52. data/docs/api/Brut/CLI/Apps/Scaffold/Component.html +1 -1
  53. data/docs/api/Brut/CLI/Apps/Scaffold/CustomElementTest.html +1 -1
  54. data/docs/api/Brut/CLI/Apps/Scaffold/DbModel.html +1 -1
  55. data/docs/api/Brut/CLI/Apps/Scaffold/E2ETest.html +1 -1
  56. data/docs/api/Brut/CLI/Apps/Scaffold/Form.html +1 -1
  57. data/docs/api/Brut/CLI/Apps/Scaffold/Page/Route.html +1 -1
  58. data/docs/api/Brut/CLI/Apps/Scaffold/Page.html +1 -1
  59. data/docs/api/Brut/CLI/Apps/Scaffold/RoutesEditor.html +1 -1
  60. data/docs/api/Brut/CLI/Apps/Scaffold/Test.html +1 -1
  61. data/docs/api/Brut/CLI/Apps/Scaffold.html +1 -1
  62. data/docs/api/Brut/CLI/Apps/Test/Audit.html +1 -1
  63. data/docs/api/Brut/CLI/Apps/Test/E2e.html +1 -1
  64. data/docs/api/Brut/CLI/Apps/Test/JS.html +1 -1
  65. data/docs/api/Brut/CLI/Apps/Test/Run.html +1 -1
  66. data/docs/api/Brut/CLI/Apps/Test.html +1 -1
  67. data/docs/api/Brut/CLI/Apps.html +1 -1
  68. data/docs/api/Brut/CLI/Command.html +1 -1
  69. data/docs/api/Brut/CLI/Error.html +1 -1
  70. data/docs/api/Brut/CLI/ExecutionResults/Result.html +1 -1
  71. data/docs/api/Brut/CLI/ExecutionResults.html +1 -1
  72. data/docs/api/Brut/CLI/Executor.html +1 -1
  73. data/docs/api/Brut/CLI/InvalidOption.html +1 -1
  74. data/docs/api/Brut/CLI/Options.html +1 -1
  75. data/docs/api/Brut/CLI/Output.html +1 -1
  76. data/docs/api/Brut/CLI/SystemExecError.html +1 -1
  77. data/docs/api/Brut/CLI.html +1 -1
  78. data/docs/api/Brut/FactoryBot.html +1 -1
  79. data/docs/api/Brut/Framework/App.html +1 -1
  80. data/docs/api/Brut/Framework/Config.html +1 -1
  81. data/docs/api/Brut/Framework/Container.html +1 -1
  82. data/docs/api/Brut/Framework/Error.html +1 -1
  83. data/docs/api/Brut/Framework/Errors/AbstractMethod.html +1 -1
  84. data/docs/api/Brut/Framework/Errors/Bug.html +1 -1
  85. data/docs/api/Brut/Framework/Errors/MissingConfiguration.html +1 -1
  86. data/docs/api/Brut/Framework/Errors/MissingParameter.html +1 -1
  87. data/docs/api/Brut/Framework/Errors/NoClassForPath.html +1 -1
  88. data/docs/api/Brut/Framework/Errors/NotFound.html +1 -1
  89. data/docs/api/Brut/Framework/Errors/NotImplemented.html +1 -1
  90. data/docs/api/Brut/Framework/Errors.html +1 -1
  91. data/docs/api/Brut/Framework/FussyTypeEnforcement.html +1 -1
  92. data/docs/api/Brut/Framework/MCP.html +1 -1
  93. data/docs/api/Brut/Framework/ProjectEnvironment.html +1 -1
  94. data/docs/api/Brut/Framework.html +1 -1
  95. data/docs/api/Brut/FrontEnd/AssetPathResolver.html +1 -1
  96. data/docs/api/Brut/FrontEnd/Component/Helpers.html +1 -1
  97. data/docs/api/Brut/FrontEnd/Component.html +1 -1
  98. data/docs/api/Brut/FrontEnd/Components/ConstraintViolations.html +48 -27
  99. data/docs/api/Brut/FrontEnd/Components/FormTag.html +1 -1
  100. data/docs/api/Brut/FrontEnd/Components/I18nTranslations.html +1 -1
  101. data/docs/api/Brut/FrontEnd/Components/Input.html +1 -1
  102. data/docs/api/Brut/FrontEnd/Components/Inputs/CsrfToken.html +1 -1
  103. data/docs/api/Brut/FrontEnd/Components/Inputs/InputTag.html +1 -1
  104. data/docs/api/Brut/FrontEnd/Components/Inputs/RadioButton.html +1 -1
  105. data/docs/api/Brut/FrontEnd/Components/Inputs/SelectTagWithOptions.html +1 -1
  106. data/docs/api/Brut/FrontEnd/Components/Inputs/TextareaTag.html +1 -1
  107. data/docs/api/Brut/FrontEnd/Components/Inputs.html +1 -1
  108. data/docs/api/Brut/FrontEnd/Components/LocaleDetection.html +1 -1
  109. data/docs/api/Brut/FrontEnd/Components/PageIdentifier.html +1 -1
  110. data/docs/api/Brut/FrontEnd/Components/TimeTag.html +1 -1
  111. data/docs/api/Brut/FrontEnd/Components/Traceparent.html +1 -1
  112. data/docs/api/Brut/FrontEnd/Components.html +1 -1
  113. data/docs/api/Brut/FrontEnd/CsrfProtector.html +1 -1
  114. data/docs/api/Brut/FrontEnd/Download.html +1 -1
  115. data/docs/api/Brut/FrontEnd/Flash.html +1 -1
  116. data/docs/api/Brut/FrontEnd/Form.html +1 -1
  117. data/docs/api/Brut/FrontEnd/Forms/ConstraintViolation.html +1 -1
  118. data/docs/api/Brut/FrontEnd/Forms/Input/Color.html +1 -1
  119. data/docs/api/Brut/FrontEnd/Forms/Input/TimeOfDay.html +1 -1
  120. data/docs/api/Brut/FrontEnd/Forms/Input.html +1 -1
  121. data/docs/api/Brut/FrontEnd/Forms/InputDeclarations.html +1 -1
  122. data/docs/api/Brut/FrontEnd/Forms/InputDefinition.html +1 -1
  123. data/docs/api/Brut/FrontEnd/Forms/RadioButtonGroupInput.html +1 -1
  124. data/docs/api/Brut/FrontEnd/Forms/RadioButtonGroupInputDefinition.html +1 -1
  125. data/docs/api/Brut/FrontEnd/Forms/SelectInput.html +1 -1
  126. data/docs/api/Brut/FrontEnd/Forms/SelectInputDefinition.html +1 -1
  127. data/docs/api/Brut/FrontEnd/Forms/ValidityState.html +1 -1
  128. data/docs/api/Brut/FrontEnd/Forms.html +1 -1
  129. data/docs/api/Brut/FrontEnd/GenericResponse.html +1 -1
  130. data/docs/api/Brut/FrontEnd/Handler.html +1 -1
  131. data/docs/api/Brut/FrontEnd/Handlers/CspReportingHandler.html +1 -1
  132. data/docs/api/Brut/FrontEnd/Handlers/InstrumentationHandler/TraceParent.html +1 -1
  133. data/docs/api/Brut/FrontEnd/Handlers/InstrumentationHandler.html +1 -1
  134. data/docs/api/Brut/FrontEnd/Handlers/LocaleDetectionHandler.html +1 -1
  135. data/docs/api/Brut/FrontEnd/Handlers/MissingHandler/Form.html +1 -1
  136. data/docs/api/Brut/FrontEnd/Handlers/MissingHandler.html +1 -1
  137. data/docs/api/Brut/FrontEnd/Handlers.html +1 -1
  138. data/docs/api/Brut/FrontEnd/HandlingResults.html +1 -1
  139. data/docs/api/Brut/FrontEnd/HttpMethod.html +1 -1
  140. data/docs/api/Brut/FrontEnd/HttpStatus.html +1 -1
  141. data/docs/api/Brut/FrontEnd/InlineSvgLocator.html +1 -1
  142. data/docs/api/Brut/FrontEnd/Layout.html +1 -1
  143. data/docs/api/Brut/FrontEnd/Middleware.html +1 -1
  144. data/docs/api/Brut/FrontEnd/Middlewares/AnnotateBrutOwnedPaths.html +1 -1
  145. data/docs/api/Brut/FrontEnd/Middlewares/Favicon.html +1 -1
  146. data/docs/api/Brut/FrontEnd/Middlewares/OpenTelemetrySpan.html +1 -1
  147. data/docs/api/Brut/FrontEnd/Middlewares/ReloadApp.html +1 -1
  148. data/docs/api/Brut/FrontEnd/Middlewares.html +1 -1
  149. data/docs/api/Brut/FrontEnd/Page.html +1 -1
  150. data/docs/api/Brut/FrontEnd/Pages/MissingPage.html +1 -1
  151. data/docs/api/Brut/FrontEnd/Pages.html +1 -1
  152. data/docs/api/Brut/FrontEnd/RequestContext.html +1 -1
  153. data/docs/api/Brut/FrontEnd/RouteHook.html +1 -1
  154. data/docs/api/Brut/FrontEnd/RouteHooks/AgeFlash.html +1 -1
  155. data/docs/api/Brut/FrontEnd/RouteHooks/CSPNoInlineScripts.html +1 -1
  156. data/docs/api/Brut/FrontEnd/RouteHooks/CSPNoInlineStylesOrScripts/ReportOnly.html +1 -1
  157. data/docs/api/Brut/FrontEnd/RouteHooks/CSPNoInlineStylesOrScripts.html +1 -1
  158. data/docs/api/Brut/FrontEnd/RouteHooks/LocaleDetection.html +1 -1
  159. data/docs/api/Brut/FrontEnd/RouteHooks/SetupRequestContext.html +1 -1
  160. data/docs/api/Brut/FrontEnd/RouteHooks.html +1 -1
  161. data/docs/api/Brut/FrontEnd/Routing/FormHandlerRoute.html +1 -1
  162. data/docs/api/Brut/FrontEnd/Routing/FormRoute.html +1 -1
  163. data/docs/api/Brut/FrontEnd/Routing/MissingForm.html +1 -1
  164. data/docs/api/Brut/FrontEnd/Routing/MissingHandler.html +1 -1
  165. data/docs/api/Brut/FrontEnd/Routing/MissingPage.html +1 -1
  166. data/docs/api/Brut/FrontEnd/Routing/MissingPath.html +1 -1
  167. data/docs/api/Brut/FrontEnd/Routing/PageRoute.html +1 -1
  168. data/docs/api/Brut/FrontEnd/Routing/Route.html +1 -1
  169. data/docs/api/Brut/FrontEnd/Routing.html +1 -1
  170. data/docs/api/Brut/FrontEnd/Session.html +1 -1
  171. data/docs/api/Brut/FrontEnd.html +1 -1
  172. data/docs/api/Brut/I18n/BaseMethods.html +1 -1
  173. data/docs/api/Brut/I18n/ForBackEnd.html +1 -1
  174. data/docs/api/Brut/I18n/ForCLI.html +1 -1
  175. data/docs/api/Brut/I18n/ForHTML.html +1 -1
  176. data/docs/api/Brut/I18n/HTTPAcceptLanguage/AlwaysEnglish.html +1 -1
  177. data/docs/api/Brut/I18n/HTTPAcceptLanguage.html +1 -1
  178. data/docs/api/Brut/I18n.html +1 -1
  179. data/docs/api/Brut/Instrumentation/LoggerSpanExporter.html +1 -1
  180. data/docs/api/Brut/Instrumentation/OpenTelemetry/NormalizedAttributes.html +1 -1
  181. data/docs/api/Brut/Instrumentation/OpenTelemetry/Span.html +1 -1
  182. data/docs/api/Brut/Instrumentation/OpenTelemetry.html +1 -1
  183. data/docs/api/Brut/Instrumentation.html +1 -1
  184. data/docs/api/Brut/RubocopConfig.html +1 -1
  185. data/docs/api/Brut/SinatraHelpers/ClassMethods.html +1 -1
  186. data/docs/api/Brut/SinatraHelpers.html +1 -1
  187. data/docs/api/Brut/SpecSupport/ClockSupport.html +1 -1
  188. data/docs/api/Brut/SpecSupport/ComponentSupport.html +1 -1
  189. data/docs/api/Brut/SpecSupport/E2ETestServer.html +1 -1
  190. data/docs/api/Brut/SpecSupport/E2eSupport.html +1 -1
  191. data/docs/api/Brut/SpecSupport/EnhancedNode.html +1 -1
  192. data/docs/api/Brut/SpecSupport/FlashSupport.html +1 -1
  193. data/docs/api/Brut/SpecSupport/GeneralSupport/ClassMethods.html +1 -1
  194. data/docs/api/Brut/SpecSupport/GeneralSupport.html +1 -1
  195. data/docs/api/Brut/SpecSupport/HandlerSupport.html +1 -1
  196. data/docs/api/Brut/SpecSupport/Matchers/BeABug.html +1 -1
  197. data/docs/api/Brut/SpecSupport/Matchers/BePageFor.html +1 -1
  198. data/docs/api/Brut/SpecSupport/Matchers/BeRoutingFor.html +1 -1
  199. data/docs/api/Brut/SpecSupport/Matchers/HaveConstraintViolation.html +1 -1
  200. data/docs/api/Brut/SpecSupport/Matchers/HaveGenerated.html +1 -1
  201. data/docs/api/Brut/SpecSupport/Matchers/HaveHTMLAttribute.html +1 -1
  202. data/docs/api/Brut/SpecSupport/Matchers/HaveI18nString.html +1 -1
  203. data/docs/api/Brut/SpecSupport/Matchers/HaveLinkTo.html +1 -1
  204. data/docs/api/Brut/SpecSupport/Matchers/HaveRedirectedTo.html +1 -1
  205. data/docs/api/Brut/SpecSupport/Matchers/HaveReturnedHttpStatus.html +1 -1
  206. data/docs/api/Brut/SpecSupport/Matchers/HaveReturnedRackResponse.html +1 -1
  207. data/docs/api/Brut/SpecSupport/Matchers.html +1 -1
  208. data/docs/api/Brut/SpecSupport/RSpecSetup/OptionalSidekiqSupport.html +1 -1
  209. data/docs/api/Brut/SpecSupport/RSpecSetup.html +1 -1
  210. data/docs/api/Brut/SpecSupport/SessionSupport.html +1 -1
  211. data/docs/api/Brut/SpecSupport.html +1 -1
  212. data/docs/api/Brut.html +1 -1
  213. data/docs/api/Clock.html +1 -1
  214. data/docs/api/ModuleName.html +1 -1
  215. data/docs/api/RichString.html +1 -1
  216. data/docs/api/SemanticLogger/Appender/Async.html +1 -1
  217. data/docs/api/Sequel/Extensions/BrutInstrumentation.html +1 -1
  218. data/docs/api/Sequel/Extensions/BrutMigrations.html +1 -1
  219. data/docs/api/Sequel/Extensions.html +1 -1
  220. data/docs/api/Sequel/Plugins/CreatedAt/InstanceMethods.html +1 -1
  221. data/docs/api/Sequel/Plugins/CreatedAt.html +1 -1
  222. data/docs/api/Sequel/Plugins/ExternalId/ClassMethods.html +1 -1
  223. data/docs/api/Sequel/Plugins/ExternalId/InstanceMethods.html +1 -1
  224. data/docs/api/Sequel/Plugins/ExternalId.html +1 -1
  225. data/docs/api/Sequel/Plugins/FindBang/ClassMethods.html +1 -1
  226. data/docs/api/Sequel/Plugins/FindBang.html +1 -1
  227. data/docs/api/Sequel/Plugins.html +1 -1
  228. data/docs/api/Sequel.html +1 -1
  229. data/docs/api/_index.html +1 -1
  230. data/docs/api/file.README.html +1 -1
  231. data/docs/api/index.html +1 -1
  232. data/docs/api/top-level-namespace.html +1 -1
  233. data/docs/assets/02-confirmation-dialog-browser-element-styled.3NEGM20-.png +0 -0
  234. data/docs/assets/02-confirmation-dialog-browser-element.DPsf0xUW.png +0 -0
  235. data/docs/assets/02-confirmation-dialog-browser.DH8ALFO4.png +0 -0
  236. data/docs/assets/02-confirmation-flow.D9gZ0S5U.png +0 -0
  237. data/docs/assets/{app.vjGWMSnJ.js → app.0-aKXKdt.js} +1 -1
  238. data/docs/assets/chunks/@localSearchIndexroot.DPhqaz1b.js +1 -0
  239. data/docs/assets/chunks/{VPLocalSearchBox.C-ymMW2k.js → VPLocalSearchBox.CW-UBkNA.js} +1 -1
  240. data/docs/assets/chunks/{theme.pJUosGlI.js → theme.a6feKWJO.js} +2 -2
  241. data/docs/assets/{components.md.B543a3Lm.js → components.md.BzVRwegp.js} +3 -3
  242. data/docs/assets/{configuration.md.-foE_iVv.js → configuration.md.eM5wFVi5.js} +1 -1
  243. data/docs/assets/{form-constraints.md.DK5adCgM.js → form-constraints.md.KTv5cdR4.js} +6 -6
  244. data/docs/assets/{forms.md.D5-2rgHh.js → forms.md.B3BHvCV3.js} +1 -1
  245. data/docs/assets/{getting-started.md.Cd4XSZb_.js → getting-started.md.BgR0ZHsl.js} +6 -3
  246. data/docs/assets/{getting-started.md.Cd4XSZb_.lean.js → getting-started.md.BgR0ZHsl.lean.js} +1 -1
  247. data/docs/assets/recipes_form-errors.md.Bv5RCKqH.js +66 -0
  248. data/docs/assets/recipes_form-errors.md.Bv5RCKqH.lean.js +1 -0
  249. data/docs/assets/tutorial.md.BM40jnoq.js +27 -0
  250. data/docs/assets/tutorial.md.BM40jnoq.lean.js +1 -0
  251. data/docs/assets/{tutorial.md.C4zR5XPG.js → tutorials_01-intro.md.BXvYWcO9.js} +5 -25
  252. data/docs/assets/tutorials_01-intro.md.BXvYWcO9.lean.js +1 -0
  253. data/docs/assets/tutorials_02-dialog.md.CIeg8R--.js +274 -0
  254. data/docs/assets/tutorials_02-dialog.md.CIeg8R--.lean.js +1 -0
  255. data/docs/assets.html +4 -4
  256. data/docs/brut-js/api/AjaxSubmit.html +1 -1
  257. data/docs/brut-js/api/AjaxSubmit.js.html +1 -1
  258. data/docs/brut-js/api/Autosubmit.html +1 -1
  259. data/docs/brut-js/api/Autosubmit.js.html +1 -1
  260. data/docs/brut-js/api/BaseCustomElement.html +1 -1
  261. data/docs/brut-js/api/BaseCustomElement.js.html +1 -1
  262. data/docs/brut-js/api/BrutCustomElements.html +1 -1
  263. data/docs/brut-js/api/BufferedLogger.html +1 -1
  264. data/docs/brut-js/api/ConfirmSubmit.html +1 -1
  265. data/docs/brut-js/api/ConfirmSubmit.js.html +1 -1
  266. data/docs/brut-js/api/ConfirmationDialog.html +1 -1
  267. data/docs/brut-js/api/ConfirmationDialog.js.html +1 -1
  268. data/docs/brut-js/api/ConstraintViolationMessage.html +55 -5
  269. data/docs/brut-js/api/ConstraintViolationMessage.js.html +18 -3
  270. data/docs/brut-js/api/ConstraintViolationMessages.html +1 -1
  271. data/docs/brut-js/api/ConstraintViolationMessages.js.html +1 -1
  272. data/docs/brut-js/api/CopyToClipboard.html +1 -1
  273. data/docs/brut-js/api/CopyToClipboard.js.html +1 -1
  274. data/docs/brut-js/api/Form.html +7 -10
  275. data/docs/brut-js/api/Form.js.html +20 -24
  276. data/docs/brut-js/api/I18nTranslation.html +1 -1
  277. data/docs/brut-js/api/I18nTranslation.js.html +1 -1
  278. data/docs/brut-js/api/LocaleDetection.html +1 -1
  279. data/docs/brut-js/api/LocaleDetection.js.html +1 -1
  280. data/docs/brut-js/api/Logger.html +1 -1
  281. data/docs/brut-js/api/Logger.js.html +1 -1
  282. data/docs/brut-js/api/Message.html +1 -1
  283. data/docs/brut-js/api/Message.js.html +1 -1
  284. data/docs/brut-js/api/PrefixedLogger.html +1 -1
  285. data/docs/brut-js/api/RichString.html +1 -1
  286. data/docs/brut-js/api/RichString.js.html +1 -1
  287. data/docs/brut-js/api/Tabs.html +1 -1
  288. data/docs/brut-js/api/Tabs.js.html +1 -1
  289. data/docs/brut-js/api/Tracing.html +1 -1
  290. data/docs/brut-js/api/Tracing.js.html +1 -1
  291. data/docs/brut-js/api/external-CustomElementRegistry.html +1 -1
  292. data/docs/brut-js/api/external-Performance.html +1 -1
  293. data/docs/brut-js/api/external-Promise.html +1 -1
  294. data/docs/brut-js/api/external-ValidityState.html +1 -1
  295. data/docs/brut-js/api/external-Window.html +1 -1
  296. data/docs/brut-js/api/external-fetch.html +1 -1
  297. data/docs/brut-js/api/global.html +1 -1
  298. data/docs/brut-js/api/index.html +1 -1
  299. data/docs/brut-js/api/index.js.html +1 -1
  300. data/docs/brut-js/api/module-testing.html +1 -1
  301. data/docs/brut-js/api/testing.AssetMetadata.html +1 -1
  302. data/docs/brut-js/api/testing.AssetMetadataLoader.html +1 -1
  303. data/docs/brut-js/api/testing.CustomElementTest.html +1 -1
  304. data/docs/brut-js/api/testing.DOMCreator.html +1 -1
  305. data/docs/brut-js/api/testing_AssetMetadata.js.html +1 -1
  306. data/docs/brut-js/api/testing_AssetMetadataLoader.js.html +1 -1
  307. data/docs/brut-js/api/testing_CustomElementTest.js.html +1 -1
  308. data/docs/brut-js/api/testing_DOMCreator.js.html +1 -1
  309. data/docs/brut-js/api/testing_index.js.html +1 -1
  310. data/docs/brut-js.html +4 -4
  311. data/docs/business-logic.html +4 -4
  312. data/docs/cli.html +4 -4
  313. data/docs/components.html +8 -8
  314. data/docs/configuration.html +5 -5
  315. data/docs/css.html +4 -4
  316. data/docs/custom-element-tests.html +4 -4
  317. data/docs/database-access.html +4 -4
  318. data/docs/database-schema.html +4 -4
  319. data/docs/deployment.html +4 -4
  320. data/docs/dev-environment.html +4 -4
  321. data/docs/dir-structure.html +4 -4
  322. data/docs/doc-conventions.html +4 -4
  323. data/docs/end-to-end-tests.html +4 -4
  324. data/docs/features.html +4 -4
  325. data/docs/flash-and-session.html +4 -4
  326. data/docs/form-constraints.html +11 -11
  327. data/docs/forms.html +6 -6
  328. data/docs/getting-started.html +10 -7
  329. data/docs/handlers.html +4 -4
  330. data/docs/hashmap.json +1 -1
  331. data/docs/hooks.html +4 -4
  332. data/docs/i18n.html +4 -4
  333. data/docs/index.html +3 -3
  334. data/docs/instrumentation.html +4 -4
  335. data/docs/javascript.html +4 -4
  336. data/docs/jobs.html +4 -4
  337. data/docs/keyword-injection.html +4 -4
  338. data/docs/layouts.html +4 -4
  339. data/docs/lsp.html +4 -4
  340. data/docs/markdown-examples.html +4 -4
  341. data/docs/middleware.html +4 -4
  342. data/docs/overview.html +4 -4
  343. data/docs/pages.html +4 -4
  344. data/docs/recipes/alternate-layouts.html +4 -4
  345. data/docs/recipes/authentication.html +5 -5
  346. data/docs/recipes/blank-layouts.html +4 -4
  347. data/docs/recipes/custom-flash.html +4 -4
  348. data/docs/recipes/form-errors.html +94 -0
  349. data/docs/recipes/indexed-forms.html +4 -4
  350. data/docs/recipes/migrations.html +5 -5
  351. data/docs/recipes/text-field-component.html +4 -4
  352. data/docs/roadmap.html +4 -4
  353. data/docs/routes.html +4 -4
  354. data/docs/security.html +4 -4
  355. data/docs/seed-data.html +4 -4
  356. data/docs/space-time-continuum.html +4 -4
  357. data/docs/tutorial.html +12 -713
  358. data/docs/tutorials/01-intro.html +736 -0
  359. data/docs/tutorials/02-dialog.html +302 -0
  360. data/docs/unit-tests.html +4 -4
  361. data/docs/why.html +4 -4
  362. data/lib/brut/instrumentation/methods.rb +153 -0
  363. data/lib/brut/instrumentation/open_telemetry.rb +1 -0
  364. data/lib/brut/instrumentation.rb +1 -0
  365. data/lib/brut/version.rb +1 -1
  366. data/mkbrut/Gemfile.lock +1 -1
  367. data/mkbrut/bin/publish +1 -1
  368. data/mkbrut/lib/mkbrut/version.rb +1 -1
  369. data/specs/brut/instrumentation/methods.spec.rb +399 -0
  370. metadata +39 -17
  371. data/docs/assets/chunks/@localSearchIndexroot.Dn1xGMv_.js +0 -1
  372. data/docs/assets/tutorial.md.C4zR5XPG.lean.js +0 -1
  373. /data/docs/assets/{components.md.B543a3Lm.lean.js → components.md.BzVRwegp.lean.js} +0 -0
  374. /data/docs/assets/{configuration.md.-foE_iVv.lean.js → configuration.md.eM5wFVi5.lean.js} +0 -0
  375. /data/docs/assets/{form-constraints.md.DK5adCgM.lean.js → form-constraints.md.KTv5cdR4.lean.js} +0 -0
  376. /data/docs/assets/{forms.md.D5-2rgHh.lean.js → forms.md.B3BHvCV3.lean.js} +0 -0
@@ -0,0 +1,1630 @@
1
+ # Build a Blog in 15 Minutes
2
+
3
+ This will start from nothing and show you the main features of Brut by building a very basic blog.
4
+ You'll learn how to make a new Brut app, how to build pages, submit forms, validate data, and access
5
+ data in a database. You'll also learn how to test it all.
6
+
7
+ ## Set Up
8
+
9
+ The only two pieces of software you need are Docker and a code editor:
10
+
11
+ 1. [Install Docker](https://docker.com)
12
+
13
+ > [!TIP]
14
+ > If you are on Windows, we *highly* recommend you use the
15
+ > Windows Subystem for Linux (WSL2), as this makes Brut, web developement,
16
+ > and, honestly, your entire life as you know it, far easier than trying to
17
+ > get things working natively in Windows.
18
+ 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.
19
+
20
+ To check that docker is installed, open up a terminal and run:
21
+
22
+ ```bash
23
+ docker info
24
+ ```
25
+
26
+ This should produce a ton of output:
27
+
28
+ ```
29
+ # OUTPUT
30
+ Client:
31
+ Version: 28.2.2
32
+ «LOTS OF OUTPUT»
33
+ ```
34
+
35
+ 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:
36
+
37
+ ```bash
38
+ echo $?
39
+ ```
40
+
41
+ ```
42
+ # OUTPUT
43
+ 0
44
+ ```
45
+
46
+ Now, let's create the app by first initializing it.
47
+
48
+
49
+ ## Initialize Your App
50
+
51
+ `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.
52
+
53
+ 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.
54
+
55
+ `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.
56
+
57
+ 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:
58
+
59
+ ```
60
+ docker run \
61
+ --pull always \
62
+ -v "$PWD":"$PWD" \
63
+ -w "$PWD" \
64
+ -u $(id -u):$(id -g) \
65
+ -it \
66
+ thirdtank/mkbrut \
67
+ mkbrut --no-demo blog
68
+ ```
69
+
70
+ You should see this output:
71
+
72
+ ```
73
+ # OUTPUT
74
+ [ mkbrut ] Creating app with these options:
75
+ [ mkbrut ] App name: blog
76
+ [ mkbrut ] App ID: blog
77
+ [ mkbrut ] Prefix: bl
78
+ [ mkbrut ] Organization: blog
79
+ [ mkbrut ] Include demo? false
80
+ [ mkbrut ] Creating Base app
81
+ [ mkbrut ] Creating segment: Bare bones framing
82
+ [ mkbrut ] blog was created
83
+
84
+ [ mkbrut ] Time to get building:
85
+ [ mkbrut ] 1. cd blog
86
+ [ mkbrut ] 2. dx/build
87
+ [ mkbrut ] 3. dx/start
88
+ [ mkbrut ] 4. [ in another terminal ] dx/exec bash
89
+ [ mkbrut ] 5. [ inside the Docker container ] bin/setup
90
+ [ mkbrut ] 6. [ inside the Docker container ] bin/dev
91
+ [ mkbrut ] 7. Visit http://localhost:6502 in your browser
92
+ [ mkbrut ] 8. [ inside the Docker container ] bin/setup help # to see more commands
93
+ ```
94
+
95
+ Before we follow the instructions in the output, `cd` to `blog` and check it out.
96
+
97
+ ```bash
98
+ cd blog
99
+ ls
100
+ ```
101
+
102
+ ```
103
+ #OUTPUT
104
+ app Dockerfile.dx Procfile.development specs
105
+ bin dx Procfile.test
106
+ config.ru Gemfile puma.config.rb
107
+ docker-compose.dx.yml package.json README.md
108
+ ```
109
+
110
+ * `app` is where all the code your app will be
111
+ * `bin` has command line tools to manage your app
112
+ * `dx` has command line tools to manage your development environment
113
+ * `specs` is where your tests will go
114
+
115
+ OK, let's start up the dev environment:
116
+
117
+ ```bash
118
+ dx/build
119
+ ```
120
+
121
+ ```
122
+ # OUTPUT
123
+ [ dx/build ] Could not find Gemfile.lock, which is needed to determine the playwright-ruby-client version
124
+ [ dx/build ] Assuming your app is brand-new, this should be OK
125
+ [+] Building 0.2s
126
+ «LOTS OF OUTPUT»
127
+ ```
128
+
129
+ 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.
130
+
131
+ When this is done, you should see a message like so:
132
+
133
+ ```
134
+ # OUTPUT
135
+ [ dx/build ] 🌈 Your Docker image has been built tagged 'blog/blog:ruby-3.4'
136
+ [ dx/build ] 🔄 You can now run dx/start to start it up, though you may need to stop it first with Ctrl-C
137
+ ```
138
+
139
+ Now, start up the environment:
140
+
141
+ ```bash
142
+ dx/start
143
+ ```
144
+
145
+ ```
146
+ #OUTPUT
147
+ [ dx/start ] 🚀 Starting docker-compose.dx.yml
148
+ [+] Running 1/5
149
+ ⠙ Container blog-postgres-1
150
+ ⠙ Container blog-app-1
151
+ ⠙ Container blog-otel-desktop-viewer-1
152
+ «LOTS OF OUTPUT»
153
+ app-1 | 2025-08-11T16:39:11.568390000-04:00
154
+ app-1 | 2025-08-11T16:39:11.568978000-04:00
155
+ app-1 | 2025-08-11T16:39:11.569430000-04:00
156
+ app-1 | 2025-08-11T16:39:11.569825000-04:00 🎉 Dev Environment Initialized! 🎉
157
+ app-1 | 2025-08-11T16:39:11.570214000-04:00
158
+ app-1 | 2025-08-11T16:39:11.570599000-04:00 ℹ️ To use this environment, open a new terminal and run
159
+ app-1 | 2025-08-11T16:39:11.570980000-04:00
160
+ app-1 | 2025-08-11T16:39:11.571250000-04:00 dx/exec bash
161
+ app-1 | 2025-08-11T16:39:11.571521000-04:00
162
+ app-1 | 2025-08-11T16:39:11.571795000-04:00 🕹 Use `ctrl-c` to exit.
163
+ app-1 | 2025-08-11T16:39:11.572064000-04:00
164
+ app-1 | 2025-08-11T16:39:11.572327000-04:00
165
+ app-1 | 2025-08-11T16:39:11.572596000-04:00
166
+ ```
167
+
168
+ `dx/start` will keep running. If you stop it, your dev environment will stop. It's running three containers:
169
+
170
+ * `app`, which is where the app and its test will run
171
+ * `postgres`, which is running PostgreSQL, a SQL database you'll use
172
+ * `otel-desktop-viewer` which can view telemetry of your app. We'll see that later.
173
+
174
+ Now, let's access the container we started.
175
+
176
+ Open a new terminal window, `cd` to where `blog` is, and use `dx/exec` to run `bash`, effectively "logging in" to the container:
177
+
178
+ ```bash
179
+ dx/exec bash
180
+ ```
181
+
182
+ ```
183
+ # OUTPUT
184
+ [ dx/exec ] 🚂 Running 'ssh-agent bash' inside container with service name 'app'
185
+ Now using node v22.18.0 (npm v10.9.3)
186
+ docker-container - Projects/blog
187
+ >
188
+ ```
189
+
190
+ 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:
191
+
192
+ ```bash
193
+ ls
194
+ ```
195
+
196
+ ```
197
+ #OUTPUT
198
+ app Dockerfile.dx Procfile.development specs
199
+ bin dx Procfile.test
200
+ config.ru Gemfile puma.config.rb
201
+ docker-compose.dx.yml package.json README.md
202
+ ```
203
+
204
+ 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.
205
+
206
+ **From here on out, all command line invocations are run inside this container**, unless stated otherwise.
207
+
208
+ ## Set Up the App Itself
209
+
210
+ `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:
211
+
212
+ * Installing RubyGems
213
+ * Installing Node Modules
214
+ * Installing Shopfiy's Ruby LSP, and Microsoft's JS and CSS LSPs
215
+ * Creating your dev and test databases
216
+ * Setting up Chromium, which we'll use to run end-to-end tests
217
+
218
+ Run it now (remember, this is inside the container, so you should've run `dx/exec bash` on your computer first)
219
+
220
+ ```bash
221
+ bin/setup
222
+ ```
223
+
224
+ ```
225
+ # OUTPUT
226
+ [ bin/setup ] Installing gems
227
+ [ bin/setup ] Executing ["bundle check --no-color || bundle install --no-color --quiet"]
228
+ «LOTS OF OUTPUT»
229
+ [ bin/setup ] All set up.
230
+
231
+ USEFUL COMMANDS
232
+
233
+ bin/dev
234
+ # run app locally, rebuilding and reloading as needed
235
+
236
+ bin/ci
237
+ # runs all tests and checks as CI would
238
+
239
+ bin/console
240
+ # get an IRB console with the app loaded
241
+
242
+ bin/db
243
+ # interact with the DB for migrations, information, etc
244
+
245
+ bin/dbconsole
246
+ # get a PSQL session to the database
247
+
248
+ bin/scaffold
249
+ # Create various structures in your app, like pages or forms
250
+
251
+ bin/setup help
252
+ # show this help
253
+ ```
254
+
255
+ 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.
256
+
257
+ ```bash
258
+ bin/ci
259
+ ```
260
+
261
+ ```
262
+ # OUTPUT
263
+ [ bin/ci ] Building Assets
264
+ «LOTS OF OUTPUT»
265
+ [ bin/ci ] Running non E2E tests
266
+ «LOTS OF OUTPUT»
267
+ [ bin/ci ] Running JS tests
268
+ «LOTS OF OUTPUT»
269
+ [ bin/ci ] Running E2E tests
270
+ «LOTS OF OUTPUT»
271
+ [ bin/ci ] Analyzing Ruby gems for
272
+ «LOTS OF OUTPUT»
273
+ [ bin/ci ] security vulnerabilities
274
+ «LOTS OF OUTPUT»
275
+ [ bin/ci ] Checking to see that all classes have tests
276
+ «LOTS OF OUTPUT»
277
+ [ bin/ci ] Done
278
+ ```
279
+
280
+ Finally, we'll run the app itself via `bin/dev`
281
+
282
+ ```bash
283
+ bin/dev
284
+ ```
285
+
286
+ `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:
287
+
288
+ ```
289
+ # OUTPUT
290
+ « LOTS OF OUTPUT »
291
+ 20:43:41 startup_message.1 | Your app is now running at
292
+ 20:43:41 startup_message.1 |
293
+ 20:43:41 startup_message.1 | http://localhost:6502
294
+ 20:43:41 startup_message.1 |
295
+ ```
296
+
297
+ Go to http://localhost:6502 in your web browser. You should see a welcome screen like so:
298
+
299
+ ![Screenshot of the Brut welcome screen](/images/tutorial/welcome-to-brut.png)
300
+
301
+ ## The Blog We'll Build
302
+
303
+ We're ready to write some code! Here's how the blog is going to work:
304
+
305
+ * A blog post has a title and content, with each paragraph of the content separated with `\n\r`, which
306
+ is what the browser inserts when you hit return.
307
+ * The home page will show all the blog posts in reverse chronological order.
308
+ * The home page will link to the edit blog post page where a blog post can be created.
309
+ * Blog posts will be submitted to the backend to be saved, with the following constraints:
310
+ - title and content are required
311
+ - title must be at least three characters
312
+ - content must be at least 5 words (i.e. space-separated tokens)
313
+
314
+ 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.
315
+
316
+ Let's start not from the database, but from the user experience.
317
+
318
+ ## Building and Styling Pages
319
+
320
+ 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`.
321
+
322
+ 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.
323
+
324
+ Open up `app/src/front_end/pages/home_page.rb` in your editor. You should see something like this:
325
+
326
+ ```ruby
327
+ class HomePage < AppPage
328
+ def page_template
329
+ # The duplication and excessive class sizes here are to
330
+ # make it easier for you to remove this when you start working
331
+ # on your app. There are pros and cons to how this code
332
+ # is written, so don't take this is as a directive on how to
333
+ # build your app. You do you!
334
+ img(src: "/static/images/LogoPylon.png",
335
+ class: "dn db-ns pos-fixed top-0 left-0 h-100vh w-auto z-2")
336
+
337
+ header(class: "flex flex-column items-center justify-center h-100vh") do
338
+
339
+ # A lot more code
340
+
341
+ end
342
+ end
343
+ end
344
+ ```
345
+
346
+ `page_template` is where you can call Phlex to generate HTML.
347
+
348
+ > [!NOTE]
349
+ > Phlex components use `view_template`, and that's what
350
+ > components in Brut use, too. Pages, however, use
351
+ > `page_template` so that the HTML can be placed inside
352
+ > a layout. `page_template` is a Brut concept, not a Phlex one.
353
+
354
+ ### Creating the HomePage
355
+
356
+ Delete all the code in `page_template` and replace it with this:
357
+
358
+ ```ruby
359
+ def page_template
360
+ header do
361
+ h1 { "My Amazing Blog" }
362
+ a(href: "") { "Write New Blog Post" }
363
+ end
364
+ main do
365
+ p { "Posts go here" }
366
+ end
367
+ end
368
+ ```
369
+
370
+ 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.
371
+
372
+ If your server is still running, refresh the page and you'll see this wonderful UI (otherwise, start your server with `bin/dev`):
373
+
374
+ ![Screenshot of the page we built](/images/tutorial/initial-home-page.png)
375
+
376
+ Let's make it a bit nicer.
377
+
378
+ ### Using CSS
379
+
380
+ Open up `app/src/front_end/css/index.css` in your editor. You should see this:
381
+
382
+ ```css
383
+ @import "brut-css/dist/brut.css";
384
+ @import "svgs.css";
385
+ ```
386
+
387
+ 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.
388
+
389
+ 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.
390
+
391
+ Add this below `@import "svgs.css";`
392
+
393
+ ```css
394
+ body {
395
+ width: 50%;
396
+ margin-left: auto;
397
+ margin-right: auto;
398
+ }
399
+
400
+ header {
401
+ border-bottom: solid thin gray;
402
+ display: flex;
403
+ align-items: baseline;
404
+ justify-content: space-between;
405
+ width: 100%;
406
+ gap: 0.5rem;
407
+ }
408
+ ```
409
+
410
+ If you reload the home page in your browser, it now looks at least a little bit respectible:
411
+
412
+ ![Screenshot of the styled home page](/images/tutorial/styled-home-page.png)
413
+
414
+ Now, let's build the blog post editor.
415
+
416
+ ## Creating Forms
417
+
418
+ To create blog posts, we need three things:
419
+
420
+ * A page where the creation happens, which will host an HTML `<form>`
421
+ * A URL where that `<form>` will be submitted
422
+ * Some code to handle the submissions
423
+
424
+ ### Creating a New Page
425
+
426
+ 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:
427
+
428
+ ```bash
429
+ bin/scaffold page /blog_post_editor
430
+ ```
431
+
432
+ Your output should look like so:
433
+
434
+ ```
435
+ # OUTPUT
436
+ [ bin/scaffold ] Inserted route into app/src/app.rb
437
+ [ bin/scaffold ] Page source is in app/src/front_end/pages/blog_post_editor_page.rb
438
+ [ bin/scaffold ] Page test is in specs/front_end/pages/blog_post_editor_page.spec.rb
439
+ [ bin/scaffold ] Added title to app/config/i18n/en/2_app.rb
440
+ [ bin/scaffold ] Added route to app/src/app.rb
441
+ ```
442
+
443
+ Restart your server (Brut currently cannot auto-reload new routes).
444
+
445
+ 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.
446
+
447
+ 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.
448
+
449
+ Open up `app/src/front_end/pages/home_page.rb` and make this change:
450
+
451
+ ```diff
452
+ - a(href: "") { "Write New Blog Post" }
453
+ + a(href: BlogPostEditorPage.routing) { "Write New Blog Post" }
454
+ ```
455
+
456
+ All page classes have a `.routing` method. By using this instead of building a URL ourselves, we get some advantages:
457
+
458
+ * If we rename or remove `BlogPostEditorPage`, any page referencing it will generate a nice, easy-to-understand error.
459
+ * `routing` can manage query strings and anchors in a safe way, plus it can check that if a page has
460
+ required routing parameters (e.g. the `123` in `/posts/123`), that they are provided.
461
+
462
+ With this change, you can now click the link and see the `BlogPostEditorPage`'s template we saw earlier.
463
+
464
+ 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*.
465
+
466
+ ### Create a Form and Handler
467
+
468
+ 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.
469
+
470
+ In this case, we'll use the URL `/new_blog_post`. Stop your server and run this command:
471
+
472
+ ```bash
473
+ bin/scaffold form /new_blog_post
474
+ ```
475
+
476
+ ```
477
+ # OUTPUT
478
+ [ bin/scaffold ] NewBlogPostForm in app/src/front_end/forms/new_blog_post_form.rb
479
+ [ bin/scaffold ] NewBlogPostHandler in app/src/front_end/handlers/new_blog_post_handler.rb
480
+ [ bin/scaffold ] Spec in specs/front_end/handlers/new_blog_post_handler.spec.rb
481
+ [ bin/scaffold ] Inserted route into app/src/app.rb
482
+ ```
483
+
484
+ When creating a new form, the first thing we have to do is edit the form class (in this case,
485
+ `NewBlogPostForm`, located in `app/src/front_end/forms/new_blog_post_form.rb`) to describe the values being accepted by the server.
486
+
487
+ 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.
488
+
489
+ 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.
490
+
491
+ Here's the code:
492
+
493
+ ```ruby
494
+ class NewBlogPostForm < AppForm
495
+ input :title, minlength: 3
496
+ input :content
497
+ end
498
+ ```
499
+
500
+ Each field is required by default (you can set `required: false` on fields that aren't required).
501
+
502
+ With these declarations, we can use an instance of this class to generate HTML.
503
+
504
+ ### Generating an HTML Form
505
+
506
+ The `BlogPostEditorPage` will contain the form used to write a blog post. This page must make sure two things happen:
507
+
508
+ * When someone navigates to it, it should show the form with nothing in the fields.
509
+ * When there is an error in what the blog post author has provided, it should show those errors, but
510
+ also maintain the inputs the author provided.
511
+
512
+ 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:
513
+
514
+ ```ruby
515
+ class BlogPostEditorPage < AppPage
516
+
517
+ def initialize
518
+ @form = NewBlogPostForm.new
519
+ end
520
+
521
+ # ...
522
+
523
+ end
524
+ ```
525
+
526
+ 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.
527
+
528
+ This will require four parts of Brut's API and use one optional one:
529
+
530
+ * `brut_form`, a custom element (`<brut-form>`) that will progressively enhance the form to provide
531
+ constraint violation visitor experience if JavaScript is enabled.
532
+ * `FormTag`, a Phlex component provided by Brut that generates the correct `<form>` element, as well as
533
+ CSRF protection.
534
+ * `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.
535
+ * `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.
536
+ * *(optional)* `t` provides access to localized strings, instead of hard-coding English.
537
+
538
+ Create `page_template` to look like so:
539
+
540
+ ```ruby
541
+ def page_template
542
+ h1 { t(:write_new_post) }
543
+ brut_form do
544
+ FormTag(for: @form) do
545
+ label do
546
+ Inputs::InputTag(form: @form,input_name: :title, autofocus: true)
547
+ div { t([:form, :title]) }
548
+ ConstraintViolations(form: @form, input_name: :title)
549
+ end
550
+ label do
551
+ Inputs::TextareaTag(form: @form,input_name: :content, rows: 10)
552
+ div { t([:form, :content] ) }
553
+ ConstraintViolations(form: @form, input_name: :content)
554
+ end
555
+
556
+ button { t([:form, :post]) }
557
+ end
558
+ end
559
+ end
560
+ ```
561
+
562
+ > [!TIP]
563
+ > You'll notice that we mentioned classes like `Brut::FrontEnd::Components::Inputs::InputTag`, but the
564
+ > code above is only using `Input::InputTag`. This is due to [*Phlex
565
+ > Kits*](https://www.phlex.fun/components/kits.html), which allow you to use relative class names
566
+ > in certain circumstances.
567
+ >
568
+ > Brut makes use of this so there is a clear and organized name for a component, but you almost never
569
+ > have to type or read the entire thing.
570
+
571
+ Make sure your server is running, then reload the blog post editor page. You should see an error like so:
572
+
573
+ > `Translation missing. Options considered were: - en.pages.BlogPostEditorPage.write_new_post - en.write_new_post`
574
+
575
+ Let's add those keys.
576
+
577
+ ### Adding Translation Keys
578
+
579
+ 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`:
580
+
581
+ ```ruby
582
+ # All app-specific translations, or overrides of Brut's defaults, go here.
583
+ {
584
+ # en: must be the first entry, thus indicating this is the EN translations
585
+ en: {
586
+ cv: {
587
+ cs: {
588
+ },
589
+ ss: {
590
+ email_taken: "This email has been taken",
591
+ },
592
+ },
593
+ pages: { # Page-specific messages - this key is relied-upon by Brut to exist
594
+ HomePage: {
595
+ title: "Welcome!",
596
+ },
597
+ BlogPostEditorPage: {
598
+ title: "BlogPostEditorPage"
599
+ },
600
+ },
601
+ # ... more code
602
+ ```
603
+
604
+ When you use `t` on a page in Brut, it looks for `pages.«page name».«key»`, so Brut needs from our form:
605
+
606
+ * `pages.BlogPostEditorPage.write_new_post`
607
+ * `pages.BlogPostEditorPage.form.title`
608
+ * `pages.BlogPostEditorPage.form.content`
609
+ * `pages.BlogPostEditorPage.form.post`
610
+
611
+ Give them values like so:
612
+
613
+ ```ruby
614
+ # All app-specific translations, or overrides of Brut's defaults, go here.
615
+ {
616
+ # en: must be the first entry, thus indicating this is the EN translations
617
+ en: {
618
+ cv: {
619
+ cs: {
620
+ },
621
+ ss: {
622
+ email_taken: "This email has been taken",
623
+ },
624
+ },
625
+ pages: { # Page-specific messages - this key is relied-upon by Brut to exist
626
+ HomePage: {
627
+ title: "Welcome!",
628
+ },
629
+ BlogPostEditorPage: {
630
+ title: "BlogPostEditorPage",
631
+ write_new_post: "Write a new post!",
632
+ form: {
633
+ title: "Title",
634
+ content: "Post Content",
635
+ post: "Post It!",
636
+ }
637
+ },
638
+ },
639
+ # ... more code
640
+ ```
641
+
642
+ Now, when you reload the page, it should work:
643
+
644
+ ![screenshot of the form working, but unstyled](/images/tutorial/basic-form.png)
645
+
646
+ Without filling anything in, click the submit button. The form should show you some error messages that are unstyled:
647
+
648
+ ![screenshot of the form working, but unstyled](/images/tutorial/basic-form-with-violations.png)
649
+
650
+ Let's style them and learn about how the `<brut-cv>` tags created by `ConstraintViolations` work.
651
+
652
+ ### Styling Constraint Violations
653
+
654
+ If you view source, you should see HTML like so:
655
+
656
+ ```html
657
+ <brut-cv-messages input-name='title'>
658
+ </brut-cv-messages>
659
+ ```
660
+
661
+ If you click submit and view source, you'll see something like this:
662
+
663
+ ```html
664
+ <brut-cv-messages input-name='title'>
665
+ <brut-cv>This field is required</brut-cv>
666
+ </brut-cv-messages>
667
+ ```
668
+
669
+ 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.
670
+
671
+ 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.
672
+
673
+ ```css
674
+ brut-cv {
675
+ display: none;
676
+ color: #A60053;
677
+ font-weight: bold;
678
+ font-size: 1rem;
679
+ }
680
+ ```
681
+
682
+ 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:
683
+
684
+ ```css
685
+ brut-form[submitted-invalid] brut-cv,
686
+ brut-cv[server-side] {
687
+ display: block;
688
+ }
689
+ ```
690
+
691
+ Let's also do some styling for the form and its elements. Add this below the CSS you just wrote:
692
+
693
+ ```css
694
+ .BlogPostEditorPage {
695
+ brut-form {
696
+ display: block;
697
+ padding: 1rem;
698
+ border: solid thin gray;
699
+ border-radius: 0.25rem;
700
+ background-color: #eeeeee;
701
+
702
+ form {
703
+ display: flex;
704
+ flex-direction: column;
705
+ gap: 1rem;
706
+ align-items: start;
707
+ }
708
+
709
+ input, textarea {
710
+ width: 100%;
711
+ padding: 0.5rem;
712
+ font-size: 130%;
713
+ }
714
+ label {
715
+ width: 100%;
716
+ font-size: 120%;
717
+ display: block;
718
+ div {
719
+ font-weight: bold;
720
+ font-style: italic;
721
+ }
722
+ }
723
+ button {
724
+ padding-left: 2rem;
725
+ padding-right: 2rem;
726
+ padding-top: 1rem;
727
+ padding-bottom: 1rem;
728
+ background-color: #E5FFE5;
729
+ border: solid thin #006300;
730
+ color: #006300;
731
+ border-radius: 1rem;
732
+ font-size: 150%;
733
+ align-self: end;
734
+ cursor: pointer;
735
+ &:hover {
736
+ background-color: #ACFFAC;
737
+ }
738
+ }
739
+ }
740
+ }
741
+ ```
742
+
743
+ Two notes about this CSS:
744
+
745
+ * It's using nesting, which is part of
746
+ [Baseline](https://developer.mozilla.org/en-US/docs/Glossary/Baseline/Compatibility)
747
+ * We've nested all the CSS inside the `.BlogPostEditorPage` class. The default layout Brut provides
748
+ includes this:
749
+
750
+ ```ruby
751
+ body(class: @page_name) do
752
+ yield
753
+ end
754
+ ```
755
+
756
+ This means all pages have their page name set on the `<body>` tag, allowing nested CSS, if you like.
757
+
758
+ *Now*, if you submit the form without providing any values, you'll see a decent-looking experience:
759
+
760
+ ![screenshot of the styled form with constraint violations](/images/tutorial/styled-form-with-violations.png)
761
+
762
+ If you fill out the fields correctly, you should see an error that you need to implement your handler. Let's do that next.
763
+
764
+ ## Handling Form Submissions
765
+
766
+ 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:
767
+
768
+ ```ruby
769
+ class NewBlogPostHandler < AppHandler
770
+ def initialize(form:)
771
+ @form = form
772
+ end
773
+
774
+ def handle
775
+ raise "You need to implement your handler"
776
+ end
777
+ end
778
+ ```
779
+
780
+ 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`.
781
+
782
+ 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`).
783
+
784
+ 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.
785
+
786
+ 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.
787
+
788
+ ```ruby
789
+ class NewBlogPostHandler < AppHandler
790
+ def initialize(form:)
791
+ @form = form
792
+ end
793
+
794
+ def handle
795
+ if @form.valid?
796
+ if @form.content.split(/\s/).length < 5
797
+ @form.server_side_constraint_violation(
798
+ input_name: :content,
799
+ key: :not_enough_words,
800
+ context: { num_words: 5 }
801
+ )
802
+ end
803
+ end
804
+ if @form.valid?
805
+ # TODO: Actually save the post
806
+ redirect_to(HomePage)
807
+ else
808
+ BlogPostEditorPage.new(form: @form)
809
+ end
810
+ end
811
+ end
812
+ ```
813
+
814
+ 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.
815
+
816
+ 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:
817
+
818
+ ```ruby{2,3}
819
+ class BlogPostEditorPage < AppPage
820
+
821
+ def initialize(form: nil)
822
+ @form = form || NewBlogPostForm.new
823
+ end
824
+ ```
825
+
826
+ 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.
827
+
828
+ > `Translation missing. Options considered were: -
829
+ > en.components.Brut::FrontEnd::Components::ConstraintViolations.cv.ss.not_enough_words -
830
+ > en.cv.ss.not_enough_words`
831
+
832
+ Add it to `app/config/i18n/en/2_app.rb`, under `en`, `cv` (for constraint violations), `ss` (for server-side):
833
+
834
+ ```ruby {10}
835
+ # All app-specific translations, or overrides of Brut's defaults, go here.
836
+ {
837
+ # en: must be the first entry, thus indicating this is the EN translations
838
+ en: {
839
+ cv: {
840
+ cs: {
841
+ },
842
+ ss: {
843
+ email_taken: "This email has been taken",
844
+ not_enough_words: "%{field} does not have enough words. Must have %{num_words}",
845
+ },
846
+ },
847
+ ```
848
+
849
+ *Now*, try again, and you'll see this message, rendered exactly like client-side errors:
850
+
851
+ ![screenshot of the styled form with server-generated constraint violations](/images/tutorial/styled-form-with-server-side-violations.png)
852
+
853
+ Now that we have the user experience in place, let's actually store the blog post in the database.
854
+
855
+ ## Using a Database
856
+
857
+ 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.
858
+
859
+ 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*.
860
+
861
+ The steps to take when creating a new table you want to access are:
862
+
863
+ 1. Create a migration that creates the schema for the new table.
864
+ 2. Create the database model class itself.
865
+ 3. Create a FactoryBot factory that can create sample instances of rows in the table, useful for testing and development
866
+ 4. Modify seed data to create sample data for development.
867
+
868
+ Most of this can be done via `bin/scaffold db_model`.
869
+
870
+ ### Creating a New Database Table
871
+
872
+ Stop your server and run `bin/scaffold` like so:
873
+
874
+ ```bash
875
+ bin/scaffold db_model blog_post
876
+ ```
877
+
878
+ ```
879
+ # OUTPUT
880
+ [ bin/scaffold ] Executing ["bin/db new_migration create_blog_post"]
881
+ [ bin/db ] Migration created:
882
+ app/src/back_end/data_models/migrations/20250811213758_create_blog_post.rb
883
+ [ bin/scaffold ] ["bin/db new_migration create_blog_post"] succeeded
884
+ [ bin/scaffold ] Creating DB::BlogPost in app/src/back_end/data_models/db/blog_post.rb
885
+ [ bin/scaffold ] Creating spec for DB::BlogPost in specs/back_end/data_models/db/blog_post.spec.rb
886
+ [ bin/scaffold ] Creating factory for DB::BlogPost in specs/factories/db/blog_post.factory.rb
887
+ ```
888
+
889
+ Your migration file name will be different than ours, since it has a timestamp embedded into it.
890
+
891
+ Open that file in your editor and use `create_table`, as provided by Sequel, to describe the table.
892
+
893
+ Brut enhances Sequel's `create_table` in the following ways:
894
+
895
+ * A numeric primary key called `id` is created.
896
+ * `comment:` is required.
897
+ * `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.
898
+ * A timestamped field, `created_at` is created and will be set when new rows are created from your app.
899
+
900
+ 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`.
901
+
902
+ All of this goes inside a block given to the method `up`, like so:
903
+
904
+ ```ruby
905
+ Sequel.migration do
906
+ up do
907
+ create_table :blog_posts,
908
+ comment: "All the posts fit to post",
909
+ external_id: true do
910
+ column :title, :text
911
+ column :content, :text
912
+ end
913
+ end
914
+ end
915
+ ```
916
+
917
+ 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.
918
+
919
+ Save this file, then apply this migration to your development database:
920
+
921
+ ```bash
922
+ bin/db migrate
923
+ ```
924
+
925
+ ```
926
+ # OUTPUT
927
+ [ bin/db ] Migrations applied
928
+ ```
929
+
930
+ Now, apply it to your test database:
931
+
932
+ ```bash
933
+ bin/db migrate -e test
934
+ ```
935
+
936
+ ```
937
+ # OUTPUT
938
+ [ bin/db ] Migrations applied
939
+ ```
940
+
941
+ You can examine the table that was created by running `bin/dbconsole`:
942
+
943
+ ```bash
944
+ bin/dbconsole
945
+ ```
946
+
947
+ ```
948
+ # OUTPUT
949
+ psql (16.9 (Debian 16.9-1.pgdg120+1), server 16.4 (Debian 16.4-1.pgdg120+2))
950
+ Type "help" for help.
951
+
952
+ blog_development=#
953
+ ```
954
+
955
+ This will give you a new prompt where you can type commands to `psql`, the Postgres command-line client. Try describing the table:
956
+
957
+ ```bash
958
+ \d blog_posts
959
+ ```
960
+
961
+ ```
962
+ Table "public.blog_posts"
963
+ Column | Type | Collation | Nullable | Default
964
+ -------------+--------------------------+-----------+----------+----------------------------------
965
+ id | integer | | not null | generated by default as identity
966
+ title | text | | not null |
967
+ content | text | | not null |
968
+ created_at | timestamp with time zone | | not null |
969
+ external_id | citext | | not null |
970
+ Indexes:
971
+ "blog_posts_pkey" PRIMARY KEY, btree (id)
972
+ "blog_posts_external_id_key" UNIQUE CONSTRAINT, btree (external_id)
973
+ ```
974
+
975
+ `bin/scaffold` created the database model for you in `app/src/back_end/data_models/db/blog_post.rb`:
976
+
977
+ ```ruby
978
+ class DB::BlogPost < AppDataModel
979
+ has_external_id :bl
980
+ end
981
+ ```
982
+
983
+ In Brut, database models are in the `DB::` namespace, so you do not conflate them with a *domain* model.
984
+
985
+ > [!TIP]
986
+ > Note `has_external_id`. This tells Brut and Sequel that the underlying table is expected
987
+ > to have the field `external_id` and that it is expected to be unique. You can see this in
988
+ > the output of `\d blog_posts`, where it says `UNIQUE CONSTRAINT, btree (external_id)`.
989
+ >
990
+ > `has_external_id` configures the database model to provide a value for this key when saving or
991
+ > creating a row. To aid in understanding the values out of context, external ids are prefixed
992
+ > with two values: one is an app-wide value, in our case `bl`. The other is a model-specific
993
+ > value, also `bl`. Thus, external ids might look like `blbl_9783245789345789345789345`.
994
+ >
995
+ 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.
996
+
997
+ ### Creating Test and Development Data
998
+
999
+ 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.
1000
+
1001
+ 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.
1002
+
1003
+ 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.
1004
+
1005
+ Make `specs/factories/db/blog_post.factory.rb` look like so:
1006
+
1007
+ ```ruby
1008
+ FactoryBot.define do
1009
+ factory :blog_post, class: "DB::BlogPost" do
1010
+ title { Faker::Hipster.sentence }
1011
+ content {
1012
+ (rand(4) + 2).times.map {
1013
+ Faker::Hipster.paragraph_by_chars(characters: 200)
1014
+ }.join("\n\r")
1015
+ }
1016
+ end
1017
+ end
1018
+ ```
1019
+
1020
+ 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:
1021
+
1022
+ ```bash
1023
+ bin/test run specs/lint_factories.spec.rb
1024
+ ```
1025
+
1026
+ ```
1027
+ # OUTPUT
1028
+ [ 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\""]
1029
+ Run options: exclude {e2e: true}
1030
+
1031
+ Randomized with seed 29315
1032
+
1033
+ factories
1034
+ should be possible to create them all
1035
+
1036
+ Finished in 0.59465 seconds (files took 0.7718 seconds to load)
1037
+ 1 example, 0 failures
1038
+
1039
+ Randomized with seed 29315
1040
+
1041
+ [ 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
1042
+ ```
1043
+
1044
+ 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:
1045
+
1046
+ ```ruby
1047
+ require "brut/back_end/seed_data"
1048
+ class SeedData < Brut::BackEnd::SeedData
1049
+ include FactoryBot::Syntax::Methods
1050
+ def seed!
1051
+ 10.times do |i|
1052
+ create(:blog_post, created_at: Date.today - i)
1053
+ end
1054
+ end
1055
+ end
1056
+ ```
1057
+
1058
+ `create` is a method provided by Factory Bot that is brought in via `FactoryBot::Syntax::Methods`.
1059
+
1060
+ Now, load the seed data into the development database with `bin/db seed`:
1061
+
1062
+ ```bash
1063
+ bin/db seed
1064
+ ```
1065
+
1066
+ We can now show this data on the home page.
1067
+
1068
+ ## Accessing the Database
1069
+
1070
+ 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.
1071
+
1072
+ 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.
1073
+
1074
+ 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.
1075
+
1076
+ 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.
1077
+
1078
+ Here's what `HomePage`'s `page_template` should look like now:
1079
+
1080
+ ```ruby
1081
+ def page_template
1082
+ header do
1083
+ h1 { "My Amazing Blog" }
1084
+ a(href: "") { "Write New Blog Post" }
1085
+ end
1086
+ main do
1087
+ DB::BlogPost.order(Sequel.desc(:created_at)).each do |blog_post|
1088
+ article do
1089
+ h2 { blog_post.title }
1090
+ blog_post.content.split(/\n\r/).each do |paragraph|
1091
+ p { paragraph }
1092
+ end
1093
+ end
1094
+ hr
1095
+ end
1096
+ end
1097
+ end
1098
+ ```
1099
+
1100
+ Start your server if you stopped it before. Go to the home page, and you should see our fake blog posts:
1101
+
1102
+ ![Home page showing two posts from the seed data, formatted properly](/images/tutorial/styled-home-page-with-posts.png)
1103
+
1104
+ If we modify our handler to save new posts to the database, they'll show up here.
1105
+
1106
+ ## Saving to the Database
1107
+
1108
+ 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):
1109
+
1110
+ ```ruby {12-15}
1111
+ def handle
1112
+ if !@form.constraint_violations?
1113
+ if @form.content.split(/\s/).length < 5
1114
+ @form.server_side_constraint_violation(
1115
+ input_name: :content,
1116
+ key: :not_enough_words,
1117
+ context: { num_words: 5 }
1118
+ )
1119
+ end
1120
+ end
1121
+ if @form.valid?
1122
+ DB::BlogPost.create(
1123
+ title: @form.title,
1124
+ content: @form.content
1125
+ )
1126
+ redirect_to(HomePage)
1127
+ else
1128
+ NewBlogPostPage.new(form: @form)
1129
+ end
1130
+ end
1131
+ ```
1132
+
1133
+ The form object provides access to the values of any field we've declared via a method call.
1134
+
1135
+ Now, create a new blog post, provide valid data, and submit it.
1136
+
1137
+ ![Screenshot of the blog post editor, with a new post filled in](/images/tutorial/new-post-editor.png)
1138
+
1139
+ Once you submit it, you should see the homage page with your new post at the top:
1140
+
1141
+ ![Screenshot of the home page, showing the new blog post](/images/tutorial/new-post-home-page.png)
1142
+
1143
+ Our work isn't quite done. We need tests.
1144
+
1145
+ ## Testing Brut Apps
1146
+
1147
+ We'll create the following tests:
1148
+
1149
+ * Check that the logic in the handler is sound
1150
+ * Check that blog posts show up on the home page
1151
+ * Check that the entire workflow of create a blog post and seeing it show up on the home page works in a
1152
+ real web browser
1153
+
1154
+ Let's test our handler first, as that is where the main logic is.
1155
+
1156
+ ### Testing Handlers
1157
+
1158
+ Our handler will need three tests:
1159
+
1160
+ * If the form was submitted without client-side validations happening, we should not create a new blog
1161
+ post and re-generate the blog post editor page, showing the errors.
1162
+ * If client-side validations pass, but the blog post isn't five words or more, we should not create a
1163
+ new blog post and re-generate the blog post editor page, showing the errors.
1164
+ * If everything looks good, we save the new blog post and redirect to the home page.
1165
+
1166
+ Brut apps are tested with RSpec, and Brut provides several convenience methods and matchers to make testing as painless as possible.
1167
+
1168
+ When testing a handler, the public method is `handle!`, not `handle`, so we want to call that (Brut implements `handle!` to call `handle`).
1169
+
1170
+ 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:
1171
+
1172
+ ```ruby
1173
+ require "spec_helper"
1174
+
1175
+ RSpec.describe NewBlogPostHandler do
1176
+ describe "#handle!" do
1177
+ context "client-side violations got to the server" do
1178
+ it "re-generates the HTML for the BlogPostEditorPage" do
1179
+ form = NewBlogPostForm.new(params: {})
1180
+
1181
+ result = described_class.new(form:).handle!
1182
+
1183
+ expect(result).to have_generated(BlogPostEditorPage)
1184
+ expect(form).to have_constraint_violation(:title, key: :valueMissing)
1185
+ expect(form).to have_constraint_violation(:content, key: :valueMissing)
1186
+ end
1187
+ end
1188
+ end
1189
+ end
1190
+ ```
1191
+
1192
+ `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.
1193
+
1194
+ `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`.
1195
+
1196
+ > [!TIP]
1197
+ > Client-side constraint violations use the same keys on the server as they do in the browser.
1198
+ > In the case of a required field, the browser's
1199
+ > [`ValidityState`](https://developer.mozilla.org/en-US/docs/Web/API/ValidityState) would set
1200
+ > `valueMissing` to true. So, that's what Brut would do on the server-side, when reflecting
1201
+ > client-side constraints.
1202
+
1203
+ Next, we'll check that the server-side constraint violations are being checked. Add this just below the `context` you just added:
1204
+
1205
+ ```ruby
1206
+ context "post is not enough words" do
1207
+ it "re-generates the HTML for the BlogPostEditorPage, with server-side errors indicated" do
1208
+ form = NewBlogPostForm.new(params: {
1209
+ title: "What a great post",
1210
+ content: "Not enough words",
1211
+ })
1212
+
1213
+ confidence_check { expect(form.constraint_violations?).to eq(false) }
1214
+
1215
+ result = described_class.new(form:).handle!
1216
+
1217
+ expect(result).to have_generated(BlogPostEditorPage)
1218
+ expect(form).to have_constraint_violation(:content, key: :not_enough_words)
1219
+ end
1220
+ end
1221
+ ```
1222
+
1223
+ This test introduces two new concepts:
1224
+
1225
+ * To initialize a form with data, you must pass a `Hash` to the keyword argument `params:`. If the
1226
+ `Hash` contains parameters that the form doesn't recognize, they are ignored and discarded.
1227
+ * Although we aren't expecting the form to have client-side constraint violations, if there are any, the
1228
+ 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.
1229
+
1230
+
1231
+ Lastly, we'll check that everything worked when there aren't any constraint violations. Add this below the `context` you just added:
1232
+
1233
+ ```ruby
1234
+ context "post is good!" do
1235
+ it "saves the post and redirects to the HomePage" do
1236
+ form = NewBlogPostForm.new(params: {
1237
+ title: "What a great post",
1238
+ content: "This post is the best post that has been written in the history of posts",
1239
+ })
1240
+
1241
+ confidence_check { expect(form.constraint_violations?).to eq(false) }
1242
+
1243
+ result = nil
1244
+ expect {
1245
+ result = described_class.new(form:).handle!
1246
+ }.to change { DB::BlogPost.count }.by(1)
1247
+
1248
+ expect(result).to have_redirected_to(HomePage)
1249
+
1250
+ blog_post = DB::BlogPost.last
1251
+ expect(blog_post.title).to eq("What a great post")
1252
+ expect(blog_post.content).to eq("This post is the best post that has been written in the history of posts")
1253
+
1254
+ end
1255
+ end
1256
+ ```
1257
+
1258
+ 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.
1259
+
1260
+ Let's run the test with `bin/test run`
1261
+
1262
+ ```bash
1263
+ bin/test run specs/front_end/handlers/new_blog_post_handler.spec.rb
1264
+ ```
1265
+
1266
+ ```
1267
+ # OUTPUT
1268
+ [ 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\""]
1269
+ Run options: exclude {e2e: true}
1270
+
1271
+ Randomized with seed 61034
1272
+
1273
+ NewBlogPostHandler
1274
+ post is not enough words
1275
+ re-generates the HTML for the BlogPostEditorPage, with server-side errors indicated
1276
+ post is good!
1277
+ saves the post and redirects to the HomePage
1278
+ #handle!
1279
+ client-side violations got to the server
1280
+ re-generates the HTML for the BlogPostEditorPage
1281
+
1282
+ Finished in 0.0138 seconds (files took 0.73976 seconds to load)
1283
+ 3 examples, 0 failures
1284
+
1285
+ Randomized with seed 61034
1286
+
1287
+ [ 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
1288
+ ```
1289
+
1290
+ It passes!
1291
+
1292
+ Next, let's test `HomePage`.
1293
+
1294
+ ### Testing Pages
1295
+
1296
+ 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.
1297
+
1298
+ 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.
1299
+
1300
+ `mkbrut` created `specs/front_end/pages/home_page.spec.rb`, so let's open that up on your editor.
1301
+
1302
+ 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.
1303
+
1304
+ 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>`.
1305
+
1306
+ 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.
1307
+
1308
+ `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.
1309
+
1310
+ Here's the test:
1311
+
1312
+ ```ruby
1313
+ require "spec_helper"
1314
+
1315
+ RSpec.describe HomePage do
1316
+ it "should show the blog posts" do
1317
+
1318
+ blog_posts = 4.times.map { create(:blog_post) }
1319
+
1320
+ result = generate_and_parse(described_class.new)
1321
+
1322
+ expect(result.e!("h1").text).to eq("My Amazing Blog")
1323
+
1324
+ blog_posts.each do |blog_post|
1325
+ post_article = result.e!("article##{blog_post.external_id}")
1326
+ expect(post_article.e!("h2").text).to eq(blog_post.title)
1327
+ blog_post.content.split(/\n\r/).each do |paragraph|
1328
+ expect(post_article.text).to include(paragraph)
1329
+ end
1330
+ end
1331
+ end
1332
+ end
1333
+ ```
1334
+
1335
+ Let's run the test, which should fail:
1336
+
1337
+ ```bash
1338
+ bin/test run specs/front_end/pages/home_page.spec.rb
1339
+ ```
1340
+
1341
+ ```
1342
+ # OUTPUT
1343
+ [ 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\""]
1344
+ Run options: exclude {e2e: true}
1345
+
1346
+ Randomized with seed 44491
1347
+
1348
+ HomePage
1349
+ should show the blog posts (FAILED - 1)
1350
+
1351
+ Failures:
1352
+
1353
+ 1) HomePage should show the blog posts
1354
+ Failure/Error: post_article = result.e!("article##{blog_post.external_id}")
1355
+
1356
+ article#blbl_6f04feaefb9520d86b19c3ac4ad22c4f matched 0 elements, not exactly 1:
1357
+
1358
+ «HUGE HTML DOCUMENT»
1359
+
1360
+ # ./local-gems/gem-home/gems/brut-0.5.0/lib/brut/spec_support/enhanced_node.rb:32:in 'Brut::SpecSupport::EnhancedNode#e!'
1361
+ # ./specs/front_end/pages/home_page.spec.rb:13:in 'block (3 levels) in <top (required)>'
1362
+ # ./specs/front_end/pages/home_page.spec.rb:12:in 'Array#each'
1363
+ # ./specs/front_end/pages/home_page.spec.rb:12:in 'block (2 levels) in <top (required)>'
1364
+ # ./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!'
1365
+ # ./local-gems/gem-home/gems/sequel-5.95.1/lib/sequel/database/transactions.rb:264:in 'Sequel::Database#_transaction'
1366
+ # ./local-gems/gem-home/gems/sequel-5.95.1/lib/sequel/database/transactions.rb:239:in 'block in Sequel::Database#transaction'
1367
+ # ./local-gems/gem-home/gems/sequel-5.95.1/lib/sequel/connection_pool/timed_queue.rb:90:in 'Sequel::TimedQueueConnectionPool#hold'
1368
+ # ./local-gems/gem-home/gems/sequel-5.95.1/lib/sequel/database/connecting.rb:283:in 'Sequel::Database#synchronize'
1369
+ # ./local-gems/gem-home/gems/sequel-5.95.1/lib/sequel/database/transactions.rb:197:in 'Sequel::Database#transaction'
1370
+ # ./local-gems/gem-home/gems/brut-0.5.0/lib/brut/spec_support/rspec_setup.rb:156:in 'block in Brut::SpecSupport::RSpecSetup#setup!'
1371
+
1372
+ Finished in 0.54876 seconds (files took 0.73025 seconds to load)
1373
+ 1 example, 1 failure
1374
+
1375
+ Failed examples:
1376
+
1377
+ bin/test run ./specs/front_end/pages/home_page.spec.rb:4 # HomePage should show the blog posts
1378
+
1379
+ Randomized with seed 44491
1380
+
1381
+ [ 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
1382
+ ```
1383
+
1384
+ 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.
1385
+
1386
+ To make it pass, we'll need to add `id:` to each `<article>`. Make this one-line change in `HomePage`:
1387
+
1388
+ ```diff
1389
+ - article do
1390
+ + article(id: blog_post.external_id) do
1391
+ ```
1392
+
1393
+ > [!TIP]
1394
+ > This shows a useful feature of the `external_id`: Because it's not only unique
1395
+ > to the database table, but also across *all* database tables, it makes a pretty
1396
+ > good `ID` inside an HTML page, since it's highly unlikely any other part of the page
1397
+ > would use that value for the `id=` of an element.
1398
+
1399
+ Now, the test should pass:
1400
+
1401
+ ```bash
1402
+ bin/test run specs/front_end/pages/home_page.spec.rb
1403
+ ```
1404
+
1405
+ ```
1406
+ # OUTPUT
1407
+ [ 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\""]
1408
+ Run options: exclude {e2e: true}
1409
+
1410
+ Randomized with seed 56951
1411
+
1412
+ HomePage
1413
+ should show the blog posts
1414
+
1415
+ Finished in 0.53858 seconds (files took 0.69257 seconds to load)
1416
+ 1 example, 0 failures
1417
+
1418
+ Randomized with seed 56951
1419
+
1420
+ [ 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
1421
+ ```
1422
+
1423
+ 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.
1424
+
1425
+ In our case, it's going to be covered by an end-to-end test we'll write next.
1426
+
1427
+ Open up `specs/front_end/pages/blog_post_editor_page.spec.rb` and make it look like so:
1428
+
1429
+ ```ruby
1430
+ require "spec_helper"
1431
+
1432
+ RSpec.describe BlogPostEditorPage do
1433
+ implementation_is_covered_by_other_tests "end-to-end test"
1434
+ end
1435
+ ```
1436
+
1437
+ Now, all unit tests should pass, which we can check via `bin/test run`:
1438
+
1439
+ ```bash
1440
+ bin/test run
1441
+ ```
1442
+
1443
+ ```
1444
+ # OUTPUT
1445
+ [ bin/test ] Running all tests
1446
+ [ 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/"]
1447
+ Run options: exclude {e2e: true}
1448
+
1449
+ Randomized with seed 63173
1450
+ ...........
1451
+
1452
+ Finished in 0.53248 seconds (files took 0.7012 seconds to load)
1453
+ 11 examples, 0 failures
1454
+
1455
+ Randomized with seed 63173
1456
+
1457
+ [ 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
1458
+ ```
1459
+
1460
+ As our last test, we'll write an end-to-end that uses a browser.
1461
+
1462
+ ### Writing Browser End-to-End Tests
1463
+
1464
+ 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:
1465
+
1466
+ 1. Go to the post editor page and make sure client-side validations show us errors.
1467
+ 2. Submit a post that's too short and make sure server-side errors show up.
1468
+ 3. Submit a valid post and check that it makes it back to the home page.
1469
+
1470
+ 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.
1471
+
1472
+ 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.
1473
+
1474
+ The way this test will work is:
1475
+
1476
+ 1. Use `HomePage.routing` to kick everything off
1477
+ 2. Find a link to `BlogPostEditorPage.routing` on the page
1478
+ 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).
1479
+ 4. We'll use `fill` to fill in data for the form fields and `click` to submit the form by clicking the submit button.
1480
+ 5. The matcher `have_text` will be used assert that text appears inside an element.
1481
+
1482
+ 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.
1483
+
1484
+ Open up `specs/e2e/home_page.spec.rb` and replace it with this:
1485
+
1486
+ ```ruby
1487
+ require "spec_helper"
1488
+
1489
+ RSpec.describe "Posting blog posts" do
1490
+ it "allows posting a post" do
1491
+
1492
+ # 1. Go to the blog post editor page from the home page
1493
+ page.goto(HomePage.routing)
1494
+ new_post_link = page.locator("a[href='#{BlogPostEditorPage.routing}']")
1495
+ new_post_link.click
1496
+
1497
+ expect(page).to be_page_for(BlogPostEditorPage)
1498
+
1499
+ # 2. Provide data that violates client-side constraints and check for error messages
1500
+ title_field = page.locator("brut-form input[name='title']")
1501
+ content_field = page.locator("brut-form textarea[name='content']")
1502
+
1503
+ title_field.fill("XX")
1504
+
1505
+ submit_button = page.locator("brut-form button")
1506
+ submit_button.click
1507
+
1508
+ expect(page).to be_page_for(BlogPostEditorPage)
1509
+
1510
+ title_error_message = page.locator("brut-cv-messages[input-name='title'] brut-cv")
1511
+ content_error_message = page.locator("brut-cv-messages[input-name='content'] brut-cv")
1512
+
1513
+ expect(title_error_message).to have_text("This field is too short")
1514
+ expect(content_error_message).to have_text("This field is required")
1515
+
1516
+ # 3. Provide data that passes client-side constraints but violates server-side ones
1517
+ title_field.fill("New blog post")
1518
+ content_field.fill("Too short")
1519
+
1520
+ submit_button.click
1521
+
1522
+ expect(page).to be_page_for(BlogPostEditorPage)
1523
+
1524
+ expect(content_error_message).to have_text("This field does not have enough words")
1525
+
1526
+ # 4. Provide a valid blog post
1527
+ content_field.fill("This is a longer post, so we should be OK")
1528
+
1529
+ submit_button.click
1530
+ expect(page).to be_page_for(HomePage)
1531
+
1532
+ new_post = DB::BlogPost.order(Sequel.desc(:created_at)).first
1533
+
1534
+ article = page.locator("article##{new_post.external_id}")
1535
+
1536
+ expect(article).to have_text("New blog post")
1537
+ expect(article).to have_text("This is a longer post, so we should be OK")
1538
+
1539
+ end
1540
+ end
1541
+ ```
1542
+
1543
+ Run it now with `bin/test e2e`:
1544
+
1545
+ ```bash
1546
+ bin/test e2e
1547
+ ```
1548
+
1549
+ It should pass:
1550
+
1551
+ ```
1552
+ # OUTPUT
1553
+ [ bin/test ] Rebuilding test database schema
1554
+ [ bin/test ] Executing ["bin/db rebuild --env=test"]
1555
+ [ bin/db ] Database exists. Dropping...
1556
+ [ bin/db ] blog_test does not exit. Creating...
1557
+ [ bin/db ] Migrations applied
1558
+ [ bin/test ] ["bin/db rebuild --env=test"] succeeded
1559
+ [ bin/test ] Running all tests
1560
+ [ 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/"]
1561
+ Run options: include {e2e: true}
1562
+
1563
+ Randomized with seed 27681
1564
+ [ bin/test-server ] Building assets
1565
+ «TONS OF OUTPUT»
1566
+ [ bin/test-server ] Starting server
1567
+ [ bin/run ] No pidfile-Starting up
1568
+ [3352] Configuration:
1569
+ «TONS OF OUTPUT»
1570
+ [3352] Use Ctrl-C to stop
1571
+ [3352] - Worker 0 (PID: 3361) booted in 0.0s, phase: 0
1572
+ [3352] - Worker 1 (PID: 3364) booted in 0.0s, phase: 0
1573
+ .[3352] === puma shutdown: 2025-08-11 22:18:16 +0000 ===
1574
+ [3352] - Goodbye!
1575
+ [3352] - Gracefully shutting down workers...
1576
+
1577
+
1578
+ Finished in 3.45 seconds (files took 0.69401 seconds to load)
1579
+ 1 example, 0 failures
1580
+
1581
+ Randomized with seed 27681
1582
+
1583
+ [ 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
1584
+ [ bin/test ] Re-Rebuilding test database schema
1585
+ [ bin/test ] Executing ["bin/db rebuild --env=test"]
1586
+ [ bin/db ] Database exists. Dropping...
1587
+ [ bin/db ] blog_test does not exit. Creating...
1588
+ [ bin/db ] Migrations applied
1589
+ [ bin/test ] ["bin/db rebuild --env=test"] succeeded
1590
+ ```
1591
+
1592
+ With that test done, `bin/ci`, which we ran at the start, should run all tests, plus check for CVEs in our installed gems.
1593
+
1594
+ ```bash
1595
+ bin/ci
1596
+ ```
1597
+
1598
+ It should also pass:
1599
+
1600
+ ```
1601
+ # OUTPUT
1602
+ «TONS OF OUTPUT»
1603
+ [ bin/ci ] Analyzing Ruby gems for
1604
+ [ bin/ci ] security vulnerabilities
1605
+ Updating ruby-advisory-db ...
1606
+ From https://github.com/rubysec/ruby-advisory-db
1607
+ * branch master -> FETCH_HEAD
1608
+ Already up to date.
1609
+ Updated ruby-advisory-db
1610
+ ruby-advisory-db:
1611
+ advisories: 998 advisories
1612
+ last updated: 2025-08-08 10:26:18 -0700
1613
+ commit: 43149b540b701c9683e402fcd7fa4e5b6e5b60e9
1614
+ No vulnerabilities found
1615
+ [ bin/ci ] Checking to see that all classes have tests
1616
+ [ bin/test ] All tests exists!
1617
+ [ bin/ci ] Done
1618
+ ```
1619
+
1620
+ That's it!
1621
+
1622
+ ## Areas for Self-Exploration
1623
+
1624
+ Here are a few enhancement you can try to make:
1625
+
1626
+ * Create a client-side constraint requiring the title to match a certain regexp.
1627
+ * Add a server-side constraint requiring at least two paragraphs.
1628
+ * Allow editing the blog post creation date
1629
+ * Add an author field to allow entering the author's name
1630
+ * Add pagination to the home page