[RUBY] Build a bulletin board API with authentication authorization with Rails 6 # 2 Introducing git and rubocop

Building a bulletin board API with certification authorization with Rails 6 # 1 Environment construction

Raise to Github

From the continuation of the last time. I've reached the point where it works for the time being, so let's push it to Github.

Select New repository.

Then create it with an appropriate name. If you don't want others to see the source code, go to Private. If there is no problem, go to Public.

rep.png

Cloud9 includes git by default, so you can use the git command as it is.

$ git add -A
$ git commit -m 'initial commit'
$ git remote add origin https://github.com/{YOUR_ACCOUNT}/{YOUR_REPOSITORY}.git
$ git branch -M master
$ git push -u origin master

The YOUR_ part of the above command is written in the repository you just created, so please refer to it.

Create a key pair and register your public key on Github

With the above support, you will be asked for your Github user ID and password each time you push.

$ ssh-keygen -t rsa

Make a key pair by hitting the Enter key repeatedly.

$ cat ~/.ssh/id_rsa.pub 

The character string displayed in is the public key, so copy it.

Go to Github and change from your icon on the upper right to "Settings" → "SSH and GPG keys" → "New SSH key". Give the title an easy-to-understand one, paste the public key to the key, and save it.

However, you should still be asked for your ID path when pushing.

$ git remote set-url origin [email protected]:{YOUR_ACCOUNT}/{YOUR_REPOSITORY}.git

If so, it's OK. This will prevent you from being asked for your ID path each time you push or pull.

Reference: Procedure for ssh connection on GitHub ~ From generation of public / private key ~

Delete the test directory

This time we will implement the test using RSpec, so delete the test directory for minitest.

$ git rm -r test/

Insert rubocop

First, let's insert rubocop that will perform static analysis. This is because it is a gem that checks the coding standards, but if you put it in later, a lot of indications that violate the coding standards will come out and you will not be able to handle it.

Gemfile


...

+ gem 'rubocop-rails'

...

Installation


$ bundle

Create a rubocop config file

yml:.rubocop.yml


AllCops:
  Exclude:
    - bin/*
    - db/schema.rb

#Japanese comment permission
AsciiComments:
  Enabled: false
  
#Unified to double quotes
StringLiterals:
  EnforcedStyle: double_quotes

Place it in your application's root directory (/ home / ec2-user / environment / bbs). Don't forget to put a dot at the beginning of the file name.

rubocop Correct the pointed out part

$ rubocop -a

When executed, it spits out about 40 errors, but almost all should be the following errors.

C: Style/FrozenStringLiteralComment: Missing frozen string literal comment.

this is

Add to the first line of each Ruby file


+ # frozen_string_literal: true
+

This is OK. What it means is that strings are mutable by default in Ruby, but it may cause unintended problems, so it's a good idea to freeze it to make it immutable. In Ruby3, it is decided that it will be immutable by default, so if you include this description in preparation for that, the character string in the file will be immutable.

It's like that.

If you don't want to open dozens of files, edit and save them ...

#At the beginning of the Gemfile# frozen_string_literal:Add true
$ sed -i '1s/^/# frozen_string_literal: true\n\n/' Gemfile

You can add to the beginning of the file by doing like. If you specify like db / *, you can also batch directories (a little dangerous).

If you have no children, you can disable this check with rubocop within the scope of the tutorial. In that case

diff:.rubocop.yml


...

+ Style/FrozenStringLiteralComment:
+   Enabled: false

Please add. However, as mentioned above, it will be immutable by default from Ruby3, so if you start creating new applications in the future, you should include it.

All you have to do is eliminate the following error.

C: Style/Documentation: Missing top-level class documentation comment.

app/mailers/application_mailer.rb app/models/application_record.rb config/application.rb It should have occurred in the 3 files of, but it is an error that appears when there is no documentation of the class.

config/application.rb



 module Bbs
+  #
+  #setting file
+  #
   class Application < Rails::Application

Let's add a comment before the class definition like.

Fix everything,

Inspecting 26 files
..........................

26 files inspected, no offenses detected

It is OK if the error disappears like.

Continued

Build a bulletin board API with authentication authorization with Rails 6 # 3 RSpec, FactoryBot is introduced and a post model is created [To the serial table of contents]

Recommended Posts

Build a bulletin board API with authentication authorization with Rails 6 # 2 Introducing git and rubocop
Build a bulletin board API with authentication and authorization with Rails 6 # 1 Environment construction
Introducing # 15 pundit to build a bulletin board API with authentication authorization in Rails 6
Build a bulletin board API with authentication authorization in Rails # 13 Add authentication header
Build a bulletin board API with authentication and authorization with Rails # 18 ・ Implementation of final user controller
Build a bulletin board API with authentication authorization with Rails 6 # 3 RSpec, FactoryBot introduced and post model
Build a bulletin board API with authentication authorization in Rails # 12 Association of user and post
Build a bulletin board API with authentication authorization in Rails 6 # 5 controller, routes implementation
Introduced # 10 devise_token_auth to build a bulletin board API with authentication authorization in Rails 6
Build a bulletin board API with authentication authorization in Rails # 17 Add administrator privileges
Build a bulletin board API with authentication authorization in Rails 6 # 14 seed Execution time display
Building a bulletin board API with authentication authorization with Rails 6 Validation and test implementation of # 4 post
# 16 policy setting to build bulletin board API with authentication authorization in Rails 6
# 8 seed implementation to build bulletin board API with authentication authorization in Rails 6
Introduced # 9 serializer to build bulletin board API with authentication authorization in Rails 6
# 6 show, create implementation to build bulletin board API with authentication authorization in Rails 6
# 7 update, destroy implementation to build bulletin board API with authentication authorization in Rails 6
How to build API with GraphQL and Rails
I tried to make a group function (bulletin board) with Rails
Create a SPA with authentication function with Rails API mode + devise_token_auth + Vue.js 3 (Rails edition)
Create a simple bulletin board with Java + MySQL
[Rails] [Docker] Copy and paste is OK! How to build a Rails development environment with Docker
Build Rails (API) x MySQL x Nuxt.js environment with Docker
Build a local development environment for Rails tutorials with Docker-Introduce Bootstrap and Font Awesome with Webpack-
Build a Node-RED environment with Docker to move and understand