Brut Web Framework

repository·main·Indexed 42 days ago

https://github.com/thirdtank/brut

A standards-based web framework for Ruby that emphasizes core web technologies like HTTP, HTML, CSS, and JavaScript. The ecosystem includes the core Ruby gem, brut-css (a minimal utility CSS library version 0.22.0), and brut-js for utility custom elements.

Tokens
124.7K
Snippets
438
Records
583
Agent score
52%

What's inside Brut

  1. Overview of Brut CSS

    main
    Brut CSS is a minimal utility CSS library that provides single-purpose classes. It is designed to allow developers to prototype and design directly within their HTML or HTML-generation code. It is primarily intended to be used alongside BrutRB to provide immediate UI capabilities without additional setup, though it can serve as a foundation for full-scale CSS solutions.
  2. Overview of BrutCSS

    main

    BrutCSS is a utility-based CSS library intended for rapid prototyping and designing directly within your HTML. It is not a replacement for CSS, but rather a tool to bootstrap designs using two main components:

    1. A Design System: Built on CSS custom properties (variables).
    2. Utility Classes: A set of single-purpose CSS classes for direct application in HTML.

    Developers can use the low-level utility classes for speed and then abstract them into components (e.g., using Phlex) or custom CSS classes as their project grows.

  3. Configure CLI bootstrapping and environment

    main

    Depending on whether your CLI needs access to the database or Brut configuration, you must implement specific methods in your Brut::CLI::Commands::BaseCommand subclass. These methods apply to each command separately.

    RequirementMethod to implementDescription
    Access Brut configdef default_rack_env = "..."Allows access to configuration values based on a specific RACK_ENV (e.g., development).
    Access Database/Business Logicdef default_rack_env = "..." AND def bootstrap? = trueFully bootstraps the application, allowing access to Brut.container.XXX and external data stores.

    Note: If you do not implement these, Brut.container.XXX calls will fail.

    class NotificationsCLI < Brut::CLI::Commands::BaseCommand
    
      def description = "Manages app notifications"
      def default_rack_env = "development"
      def bootstrap? = true
    
      def run
        # Brut.container.XXX will work
        # Business logic will work
      end
    end
  4. Accessing HTTP Headers via Keyword Injection

    main

    Brut allows you to access any HTTP header by declaring a keyword argument starting with http_.

    Naming Convention: Brut converts the standard Rack/CGI header format (e.g., HTTP_USER_AGENT) to lowercase Ruby-friendly names. To access the User-Agent header, use http_user_agent.

    Behavior based on argument requirement:

    • Required arguments (no default value): If the header is missing, nil is injected. If present, the value is injected (even if it is an empty string).
    • Optional arguments (has a default value): If the header is missing, the default value is used. If present, the value is injected.
  5. Create reusable Components

    main

    Components are Phlex components (classes implementing view_template) used to manage HTML complexity. You can instantiate and render them within any view using the render method.

    class FlashComponent < AppComponent
      def initialize(flash:)
        # ... logic to set @role and @message_key
      end
    
      def view_template
        div(role: @role) do
          t([ :flash, @message_key ])
        end
      end
    end
    
    # Usage in a page
    def page_template
      render FlashComponent.new(flash:)
    end
  6. Configure and manage the `app/` directory

    main

    The app/ directory is the heart of your application. It contains the following key components:

    • bootstrap.rb: A Ruby file that sets up your app and ensures all necessary files are required correctly.
    • config/: Application configuration (e.g., localizations and translations). Note: Brut avoids using YAML for configuration.
    • public/: The root directory for public assets served by the application.
    • src/: The location for all application source code.
  7. Core concepts and architectural mental model of Brut

    main

    Brut organizes web application development around four primary domains. Understanding these boundaries is essential for structuring your application code:

    • Client (Client Side): The web browser or HTTP client. This is the environment where CSS is applied to HTML, JavaScript is executed, and HTTP requests are initiated.
    • Server (Server Side): The environment where all code not running in the browser executes, including HTML generation and SQL queries.
    • Front End: Code responsible for producing the user interface or HTTP API. While this code often runs on the server side, its purpose is to provide the interface.
    • Back End: Code responsible for everything else, including database access, business logic execution, and background job management.

    Brut is designed to leverage web standards (HTML, CSS, SQL, and the Web Platform) rather than abstracting them away into proprietary DSLs.

  8. Manage database timezones using timestamp with time zone

    main

    To avoid ambiguity when reading and storing dates across different system or database timezones, Brut recommends using the timestamp with time zone SQL data type.

    If your system is set to UTC and you store a timestamp using this type, the value will be read back correctly even if the system timezone is later changed (e.g., to America/Los_Angeles). Using timestamp without time zone (or the standard SQL timestamp) can lead to ambiguity during timezone shifts.

  9. Style Constraint Violations

    main

    Brut uses custom elements for constraint violations. The <brut-form> element sets a submitted-invalid attribute when a user attempts to submit an invalid form.

    To show error messages only after a submission attempt or when they are provided by the server, use the following CSS selectors:

    • brut-cv: The custom element for individual error messages.
    • brut-form[submitted-invalid] brut-cv: Targets error messages when a form has been submitted and is invalid.
    • brut-cv[server-side]: Targets error messages generated by the server.
    /* Hide messages by default */
    brut-cv {
      display: none;
      color: #A60053;
      font-weight: bold;
      font-size: 1rem;
    }
    
    /* Show messages on invalid submission or server-side errors */
    brut-form[submitted-invalid] brut-cv,
    brut-cv[server-side] {
      display: block;
    }
  10. Understand why Brut is not an MVC framework

    main

    Brut explicitly avoids the Model-View-Controller (MVC) pattern and does not use 'resources' as a core abstraction.

    Unlike MVC frameworks that attempt to map HTTP verbs to specific 'resources' (e.g., mapping DELETE to a specific resource action), Brut focuses on the practical reality of web development: serving pages and handling requests. This avoids the complexity and ambiguity often found when trying to force-map business logic and database tables into a strict resource-based RESTful structure.