[RUBY] Try create with Trailblazer

Introduction

After learning about the existence of Gem and Trailblazer, which provide an abstract layer, I tried Create. The outline of Trailblazer is explained in other articles in an easy-to-understand manner, so I will omit it.

This article was very easy to understand. TrailBlazer overview I tried to summarize

environment

ruby '2.7.2' 'rails', '~> 6.0.3' template engine: haml

Create

Trailblazer directory structure

.
└── todo
    ├── cell
    │   ├── index.rb
    │   └── new.rb
    ├── contract
    │   └── form.rb
    ├── operation
    │   ├── create.rb
    │   └── index.rb
    └── view
        ├── form.haml
        ├── index.haml
        └── new.haml

Operation


module Todo::Operation
  class Create < Trailblazer::Operation
    class Present < Trailblazer::Operation
      step Model(Todo, :new)
      step Contract::Build( constant: Todo::Contract::Form )
    end

    step Subprocess(Present) #present class step call
    step Contract::Validate(key: :todo) #Validation using contract
    step Contract::Persist() #Save to model
  end
end

Contract


module Todo::Contract
  class Form < Reform::Form
    include Reform::Form::ActiveModel
    property :title
    property :description
    property :completed
      
    validates :title, presence: true
    validates :description, length: { minimum: 2 }

    #You can also override
    # def title
    #  super.capitalize
    # end
  end
end

Cell

module Todo::Cell
  class New < Trailblazer::Cell
    include ActionView::RecordIdentifier
    include ActionView::Helpers::FormOptionsHelper
    include SimpleForm::ActionViewExtensions::FormHelper
  end
end

View

= simple_form_for(model, html: {novalidate: true}) do |f|
  = f.input :title, placeholder: "Title"
  %br
  = f.input :description, as: :text, placeholder: "Description"
  %br
  = f.submit 'Submit'
  = link_to 'Back', todos_path

Verification

trab-todo

[Project] I tried this time (https://github.com/izumiiii/trail-todo)

Reference article

I tried to summarize the outline of TrailBlazer trailblazer

Recommended Posts

Try create with Trailblazer
Try DI with Micronaut
Try WebSocket with jooby
Try WildFly with Docker
Create XML-RPC API with Wicket
Try using GloVe with Deeplearning4j
Try using view_component with rails
Try DB connection with Java
Try gRPC with Java, Maven
Create a playground with Xcode 12
Try reading XML with JDOM
Create microservices with Spring Boot
Create an immutable class with JAVA
Create a Vue3 environment with Docker!
Create PDF with itext7-Free layout: Text-
Create portfolio with rails + postgres sql
Let's create Ubuntu environment with vmware
Create an app with Spring Boot 2
Create RUNTEQ's environment with Windows DockerDesktop
Try running cloudera manager with docker
Create PDF with itext7-Free layout: Table-
Try implementing recaptcha with Jetty embedded.
Try manipulating PostgreSQL arrays with JDBC
Create pagination function with Rails Kaminari
Try using Redis with Java (jar)
Create SolrCloud verification environment with Docker
Create an excel file with poi
Try to imitate marshmallows with MiniMagick
Create an app with Spring Boot
Create two DatePickers linked with TextField
Create Laravel environment with Docker (docker-compose)
Create command line app with maven
Try parsing English with Stanford CoreNLP
Create PDF with itext7 ~ Hello World ~
Try bidirectional communication with gRPC Java
Try to create a server-client app
Create My Page with Rails devise
Create exceptions with a fluid interface
Create a Maven project with a command
Let's create an instance with .new yourself. .. ..
Create an infinite scroll with Infinite Scroll and kaminari
[Java] Create an executable module with Gradle
Create a jar file with the command
[Rails6] Create a new app with Rails [Beginner]
Create a simple web application with Dropwizard
Create realistic dummy data with gem Faker
Create your own validator with Bean Validation
Create Rails 6 + MySQL environment with Docker compose
Create a simple on-demand batch with Spring Batch
Try running MySql and Blazor with docker-compose
Create a GUI JSON Viewer with Ruby/GTK3
Let's try WebSocket with Java and javascript!
[Rails withdrawal] Create a simple withdrawal function with rails
Try running Slack's (Classic) Bot with docker
Create a MySQL environment with Docker from 0-> 1
Create Spring Boot-gradle-mysql development environment with Docker
Try using Spring Boot with VS Code
Try shaking your hands with ARKit + Metal
[Docker] Create Node.js + express + webpack environment with Docker
Create a simple bar chart with MPAndroidChart
Try to implement login function with Spring-Boot