LoginSignup
19
20

More than 5 years have passed since last update.

2015年春のrails new

Last updated at Posted at 2015-04-12

大体において入れてるGemとその初期設定のメモ。
Ruby 2.2.0 + Rails 4.2.0。

まだ途中だけど春が終わりそうなので一旦公開。随時直していく。

rails _4.2.0_ new app_name --skip-bundle --skip-test-unit --database=mysql
# 短く書くと
rails _4.2.0_ new app_name -B -T -d mysql
Gemfile
# ここより上はデフォルトのままのことが多い

# slim
gem 'slim-rails'
# 環境ごとの定数管理
gem 'rails_config'
gem 'dotenv-rails'
# 認証
gem 'sorcery'
# 管理画面
gem 'rails_admin'
# 補足されない例外をメール送信
gem 'exception_notification'
# ページネーション
gem 'kaminari'
# パンくず
gem 'gretel'

group :development, :test do
  # この2つは最初から入ってる
  # beybugはpry-beybugがあるので外す
  gem 'web-console', '~> 2.0'
  gem 'spring'
  gem 'spring-commands-rspec'

  # console
  gem 'pry-byebug'
  gem 'pry-doc'
  gem 'pry-rails'
  gem 'pry-stack_explorer'
  gem 'awesome_print'
  gem 'hirb-unicode'

  # guard
  gem 'guard'
  gem 'guard-rspec'

  # lint
  gem 'guard-rubocop'
  # ログ出力整形
  gem 'quiet_assets'

  # パラメータ、SQL整形
  # gem 'rails-flog' # いらん気がしてきた

  # エラー画面拡張
  gem 'better_errors'
  gem 'binding_of_caller'
end

group :development do
  # クエリ発行箇所特定
  gem 'activerecord-cause'
  # N+1問題検出
  gem 'bullet'
  # 行毎のプロファイラ
  gem 'peek-rblineprof'
  # 簡易プロファイラ
  gem 'rack-mini-profiler', require: false
end

group :test do
  gem 'rspec-rails'
  gem 'factory_girl_rails'
  gem "poltergeist"
  gem "database_cleaner"
end

bundleのパスは.bundleにすると.gitignoreに追加しなくて済む。

bundle install --path .bundle

以下各Gemの設定など。

# rbenv使ってるので
echo "2.2.0" > .ruby-version
# rspecの初期化
bin/rails g rspec:install
echo "--format documentation" >> .rspec
# Guard+rspec
bin/bundle exec guard init rspec
# Guard+rubocop
bin/bundle exec guard init rubocop

# rubocopの初期ルールを生成するため先に除外するファイルを指定
cat <<EOF > .rubocop.yml
AllCops:
  Exclude:
    - db/schema.rb
    - tmp/**/*
    - .bundle/**/*
    - bin/*
    - config/initializers/*.rb
EOF

# railsが生成したファイルのコーディング規約エラーを除外
# .rubocop_todo.ymlが作成される
bin/bundle exec rubocop --auto-gen-config

.rubocop.ymlを以下のような感じに調整する。

.rubocop.yml
inherit_from: .rubocop_todo.yml

AllCops:
  Exclude:
    - db/schema.rb
    - tmp/**/*
    - .bundle/**/*
    - bin/*
    - config/initializers/*.rb

# ブロック前後の空行を許可
Style/EmptyLinesAroundBlockBody:
  Enabled: false

# 引数前のスペースは複数許可
Style/SingleSpaceBeforeFirstArg:
  Enabled: false
# bin/rails c した時の表示を改善
cat <<EOF > .pryrc
require "awesome_print"
AwesomePrint.pry!

begin
  require 'hirb'
rescue LoadError
  # Missing goodies, bummer
end

if defined? Hirb
  # Slightly dirty hack to fully support in-session Hirb.disable/enable toggling
  Hirb::View.instance_eval do
    def enable_output_method
      @output_method = true
      @old_print = Pry.config.print
      Pry.config.print = proc do |*args|
        Hirb::View.view_or_page_output(args[1]) || @old_print.call(*args)
      end
    end

    def disable_output_method
      Pry.config.print = @old_print
      @output_method = nil
    end
  end

  Hirb.enable
end
EOF

# binにguardを追加
bin/bundle binstubs guard

# spring対応
bin/bundle exec spring binstub --all

Guardからのspring経由でrspecできるようにGuardfileを変更。

Guardfile
guard :rspec, cmd: "bundle exec rspec" do

guard :rspec, cmd: "bin/rspec" do

bin/rails gで余計なものを生成しないように設定。

config/application.rb
    # Generator
    config.generators do |g|
      g.assets false
      g.helper false
      g.test_framework :rspec,
        view_specs: false,
        helper_specs: false,
        fixture: true
    end

bin/rails cでパラメータ、SQLなどを整形して表示するようにする。

config/environments/development.rb
  # rails-flog
  # If this value is true, not format on cached query
  config.ignore_cached_query = false
  # If query duration is under this value, not format
  config.query_duration_threshold = 2.0
  # If key count of parameters is under this value, not format
  config.params_key_count_threshold = 2
  # If this value is true, nested Hash parameter is formatted coercively in any situation
  config.force_on_nested_params = false

N+1問題が検出された際にログとalertで知らせるようにする。

config/environments/development.rb
  config.after_initialize do
    Bullet.enable = true
    Bullet.alert = true
    Bullet.bullet_logger = true
    Bullet.console = true
    Bullet.rails_logger = true
  end

rack-mini-profiler


cat <<EOF > config/initializers/rack_profiler.rb 
if Rails.env == 'development'
  require 'rack-mini-profiler'
  Rack::MiniProfilerRails.initialize!(Rails.application)
end
EOF

activerecord-cause

config/environments/development.rb
  ActiveRecord::Cause.log_with_sql = true 
  ActiveRecord::Cause.log_mode = :all 

rblineprof

cat <<EOF > config/initializers/peek.rb
Peek.into Peek::Views::Rblineprof
EOF

cssとjavascriptも追記

*= require peek
*= require peek/views/rblineprof
//= require peek
//= require peek/views/rblineprof
19
20
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
19
20