[RUBY] How to get along with Rails

What's this

The members of the company I'm in now aren't very familiar with Rails, and I have a lot of questions about myself, so I put them together on an internal wiki. I posted it on Qiita because it was well received.

It is a prohibited item because it includes personal thoughts.

If you need it, install as much as you need.

architecture

Presenter Prepare data for view Describe the specific logic of view that tends to become fat if described in controller

--view and 1: 1 --In terms of image, it is used to prepare data for view. --Basically, let the presenter have everything, the controller only generates the presenter, and the view uses the necessary values ​​from the presenter. --Example: I want to prepare recommend_articles for the list of recommended articles on the screen.

Decorator Decorate the model for the model Describe a method that tends to be fat if described in model

--model and 1: 1 --In terms of image, use it when you want to use the model value after formatting in the entire application. --Example: We want to use the model last_name and first_name as last_name + first_name, so define the full_name method.

Finder Generate search queries that tend to be complicated Write a query that becomes fat or spaghetti if written in model

Since my friend implemented it nicely, the details are as follows https://furaji.hatenablog.jp/entry/2020/05/09/043924

Service Describe processes that are difficult to write in a single model, such as processes that span multiple models

--Basically, I want to have a single responsibility, so I'll name it verb + target + service. --Example: Create CreateUserService because it processes not only User model but also multiple models when creating a user. --The user is not involved in Service as much as possible --Example: The side using CreateUserService.call (args) wants to be completed

ValueObject By making the value of an object (especially model) an object instead of a value, you take responsibility for the value. --ActiveRecord makes it easy because you can use composed_of --Example: If you divide the class with address of user.address as ValueObject, you can write the process that is only interested in address without polluting the User model. user.address.tokyo? etc. --If the Decorator is interested in the entire model, then the ValueObject is interested in one value in the model.

It's hard to explain, so throw it round https://qiita.com/okuramasafumi/items/16bcda8f6382a3db47c9

Concerns ~~ rails new Then erase it ~~ It is powerful but most likely to be insecure, so proper design is required.

Cut out the interest of model and controller. This interest is largely functional.

For example

  1. I want to link some tags (Article :: Tag) to the main article (Article) of the site
  2. I want to link some tags (News :: Tag) to the site news (News)

In this case, both 1 and 2 are interested in ** tagging/removing **, so it is possible to standardize around tags by conens such as Taggable.

If you don't think about development at the requirement definition stage, you tend to have many ** similar and different functions **. It will be easier to standardize if engineers add tea and make it simple from the requirements definition stage.

routing

Basically RESTful For example, if you want to download user's csv, you want to take action directly on user, It's clear what happens in RESTful if you enable get with user/csv.

Read this and write well https://tech.kitchhike.com/entry/2017/03/07/190739

test

Minitest comes with Rails, but the de facto standard is RSpec. If you write too strong test code, you will need to test the test code, so be careful.

――Clarify "what you want to test" ――You don't have to be very conscious of DRY --Basically the object is disposable and ok ――If you use it strangely, it may have unintended effects. --Time-related tests use travel_to etc. to manipulate time ――If you use Time.now, you can make a time bomb, so let's stop.

Let's read this and write it well https://www.betterspecs.org/

Recommended Posts

How to get along with Rails
How to get started with slim
How to get started with creating a Rails app
[Rails] How to use rails console with docker
[Note] How to get started with Rspec
How to build Rails 6 environment with Docker
How to write Rails
How to uninstall Rails
How to get boolean value with jQuery in rails simple form
How to get started with Eclipse Micro Profile
How to build API with GraphQL and Rails
Rails beginners tried to get started with RSpec
How to get resource files out with spring-boot
[Rails] How to build an environment with Docker
[Rails] How to get success and error messages
[Rails] How to get the user information currently logged in with devise
[rails] How to post images
[Rails] How to use enum
[Rails] How to install devise
[Rails] How to use enum
How to read rails routes
How to use rails join
How to number (number) with html.erb
How to update with activerecord-import
How to terminate rails server
How to write Rails validation
How to write Rails seed
[Rails] How to use validation
[Rails] How to disable turbolinks
[Rails] How to use authenticate_user!
[Rails] How to use "kaminari"
[Rails] How to implement scraping
[Rails] How to make seed
How to write Rails routing
[Rails] How to install simple_calendar
[Rails] How to install reCAPTCHA
[Rails] How to use Scope
How to make batch processing with Rails + Heroku configuration
[Rails] How to search by multiple values ​​with LIKE
How to push an app developed with Rails to Github
Rails / Ruby: How to get HTML text for Mail
[My memo] Let's get along with Pry / DB with Rails
How to delete a new_record object built with Rails
How to make an almost static page with rails
How to manually generate a JWT with Rails Knock
[How to insert a video in haml with Rails]
How to query Array in jsonb with Rails + postgres
[Rails] How to get the contents of strong parameters
[Docker + Rails] How to deal with Rails server startup failure
I want to get along with Map [Java beginner]
How to get jdk etc from oracle with cli
How to automatically generate ER diagram when migrating with Rails6
[Rails] How to use gem "devise"
How to scroll horizontally with ScrollView
How to use Java HttpClient (Get)
How to deploy jQuery on Rails
How to set environment variables when using Payjp with Rails
[Rails] How to install Font Awesome
[Rails] How to use flash messages
[rails] How to display db information
[Rails] How to write in Japanese