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.
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
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.
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
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