[Ruby on Rails 4] 新規プロジェクトの開始手順

スカッと忘れている rails new まわり。

[markdown]
こちらを参考にさせて頂きました。

> * [rails new 手順書 – Qiita](http://qiita.com/youcune/items/312178c54c65f3ab4d42)

## rails プロジェクトディレクトリの新規作成

プロジェクトディレクトリの作成。

`mkdir` や `git clone` をする。

“`prettyprinted
% git clone git@github.com:foo/bar.git
% cd bar
“`

ディレクトリ移動後、Gemfile を作成・編集。

“`prettyprinted
% bundle init
% vim Gemfile
“`

rails のコメントを外し、必要であればバージョンを指定する。

“`ruby:Gemfile
# frozen_string_literal: true
source “https://rubygems.org”
gem ‘rails’, ‘~> 4.2.7’
# for macOS Sierra & ruby 2.4
gem ‘json’, git: ‘https://github.com/flori/json.git’, branch: ‘v1.8’
“`
rails を `bundle install` する。
bundler は、設定したオプションを .bundle/config に記録して覚える。

“`prettyprinted
% bundle install –path vendor/bundle –without staging production –jobs=4
“`

プロジェクトに必要な gem を入れ替える必要があるので、skip-bundle オプションを付けて `rails new` を実行する。

– bundle install しない(–skip-bundle / -B)。
– RSpec 使うため Test::Unit を組み込まない(–skip-test-unit / -T)。

`bundle exec rails help new` でオプションを確認できる。

“`prettyprinted
% bundle exec rails new . -BT
“`

Gemfile の上書きを `y` で許可する。

## プロジェクトに必要な gem をインストールする

Gemfile にプロジェクトに必要が gem を追加する。

“`ruby:Gemfile
#
# Project gems
#
gem ‘haml-rails’
gem ‘rspec-rails’
gem ‘factory_girl_rails’
“`

`bundle install` を実行する。

“`prettyprinted
% bundle install
“`

## generator の出力を調整する

config.generators を調整する。
ERB -> HAML, Test::Unit -> RSpec, Fixture -> FactoryGirl

“`ruby:config/application.rb
config.generators do |g|
g.javascript_engine :js
g.jbuilder false
g.template_engine :haml
g.test_framework :rspec, view_specs: false, fixture: true
g.fixture_replacement :factory_girl, dir: ‘spec/factories’
end
“`

RSpec のひな型を作成する。

“`prettyprinted
% bundle exec rails generate rspec:install
“`
[/markdown]