素敵なgem、rails_best_practicesを導入する

先輩のアドバイスを受け、Railsのベストプラクティスに従っているかを確認できるgemを導入しました。

手順

Gemfileにrails_best_practicesを追加(今回は開発環境だけで使用する)

group :development do
  # Access an IRB console on exception pages or by using <%= console %> in views
  gem 'web-console', '~> 2.0'
  gem 'thin'
  gem 'rails_best_practices'

  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
end

bundleコマンドを実行し、gemをインストールする

$ cat .bundle/config
---
BUNDLE_PATH: vendor/bundle
BUNDLE_JOBS: 4
BUNDLE_DISABLE_SHARED_GEMS: '1'
$
$
$ bundle
Fetching gem metadata from https://rubygems.org/...........
Fetching version metadata from https://rubygems.org/...
Fetching dependency metadata from https://rubygems.org/..
Resolving dependencies...
Using json 1.8.3
Using minitest 5.8.4
・・・略・・・
Installing code_analyzer 0.4.5
Installing rails_best_practices 1.16.0
Bundle complete! 13 Gemfile dependencies, 64 gems now installed.
Bundled gems are installed into ./vendor/bundle.
Post-install message from rails_best_practices:
********************************************************************************

  rails_best_practices is a code metric tool to check the quality of rails codes.

  I highly recommend you browse the Rails Best Practices website first.

      http://rails-bestpractices.com

  Please also try our online service

      http://railsbp.com

  Enjoy!

      Richard Huang (flyerhzm@gmail.com)

********************************************************************************
$

これでrails_best_practicesが使える状態になる。

早速実行してみる!
プロジェクトのディレクトリに入り、 rails_best_practices . を実行する。

$ bundle exec rails_best_practices .
Source Code: |==============================================================================================================================|
/Users/maetoo11/workspace/rails-app-name/db/schema.rb:34 - always add db index (model_name => [column_name])
/Users/maetoo11/workspace/rails-app-name/app/views/hoge/index.html.erb:53 - law of demeter
/Users/maetoo11/workspace/rails-app-name/app/helpers/humus_helper.rb:1 - remove empty helpers
/Users/maetoo11/workspace/rails-app-name/app/views/humus/_form.html.erb:1 - replace instance variable with local variable
/Users/maetoo11/workspace/rails-app-name/app/views/hoge/_form.html.erb:7 - replace instance variable with local variable

Please go to http://rails-bestpractices.com to see more useful Rails Best Practices.

Found 28 warnings.

各警告メッセージの意味(日本語)は下記の記事を参考にしました。
Rails Best Practices の警告をちゃんと考える - Qiita

Rails Best Practicesの本家ページ(?)です。
Rails Best Practices - Rails Best Practices
※どうやってプラクティスを検索していいかが不明…。

これでベストプラクティスをできるだけ守ったコードがかけるはず!!