title 一些用來檢驗rails程式碼的工具
datetime 2016-05-14 12:27:43
tags rails,ruby_gem
category coding
link tools-for-cleanup-rails-code
file 2016-05-14-122743-tools-for-cleanup-rails-code
template post
end
前言
1. 這篇文章部分內容擷錄於 Top 8 tools for Ruby on Rails code optimization and cleanup - by Damir Svrtan,它對於每個工具都有簡短的描述與說明,有興趣可以過去看看。
2. 下面提到的gem,在Gemfile中應該要放在development的group之下,因為production應該不會跑這些工具。
Traceroute
Github
What
幫你列出無用的routes。
How
- gem 'traceroute'
- rake traceroute
Unused routes (3):
users/omniauth_callbacks#(?-mix:facebook)
courses#show
my/student/courses#index
Unreachable action methods (3):
grape_swagger_rails/application#index
emergency#show
devise#_prefixes
rack-mini-profiler
Github
What
profiling每一頁花的時間。
How
- gem 'rack-mini-profiler', require: false
- 建立config/initializers/rack_profiler.rb
if Rails.env == 'development'
require 'rack-mini-profiler'
# initialization is skipped so trigger it
Rack::MiniProfilerRails.initialize!(Rails.application)
end
- gem 'flamegraph' (如果要顯示圖)
- gem 'stackprof' (如果要顯示圖)
- 裝了之後在每一頁的左上角會出現時間,按下去會顯示更多的資訊。
bullet
Github
What
找出 N+1 query。
How
- gem 'bullet'
- 在config/environments/development.rb中設定bullet。
config.after_initialize do
Bullet.enable = true
Bullet.alert = true
Bullet.bullet_logger = true
Bullet.rails_logger = true
end
- 裝了之後,如果有出現 N+1 query ,就會跳出警告視窗。
brakeman
Github
What
檢查rails是否有安全性漏洞。
How
- gem 'brakeman', require: false
- brakeman -o result.html
- 執行brakeman之後,它會開始掃描rails專案,並列出可能的問題。
-o 檔名
可以將結果輸出至檔案。
rails_best_practices
Github
What
檢查rails專案中是否有違反best practices。
How
- gem 'rails_best_practices', require: false
- rails_best_practices -f html .
rubocop
Github
What
檢查rails專案中是否有違反ruby style guide。
How
- gem 'rubocop', require: false
- rubocop --format html -o rubocop.html
RubyCritic
Github
What
使用reek, flay, flog檢查rails專案中是否有違反ruby style guide,重點是報表還蠻好看的。
How
- gem 'rubycritic', require: false
- rubycritic
- 執行完之後,會將報表放在tmp/rubycritic/overview.html
RailsPanel
Github
Chrome WebStore
What
在chrome的developer tool加上rails的分析工具。
How
- 到上面的Chrome WebStore安裝RailsPanel這個plugin。
- gem 'meta_request'
- 安裝完啟動rails server,在瀏覽網頁時,開啟developer tool就會發現有多一個Rails的分頁可以觀察rails相關的資訊。
Refs