@masuidrive のRailsプロジェクトの始め方

## rbenv, nvm, MySQL, redisが入ったUbuntu Vagrantfile

すぐに開発に使えるVagrantfile。依存物を少なくするためにShellでプロビジョニング。

`vagrant up`時に各種パッケージのインストールとRubyのコンパイルが走るので、30分ぐらいかかります。初回でOSイメージが無い場合はもっとかかるかも。

```rb:Vagrantfile
Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/trusty64"
  config.vm.network :forwarded_port, guest: 3000, host: 3000
  config.vm.synced_folder ".", "/vagrant"

  GUEST_RUBY_VERSION = '2.2.4'

  GUEST_NVM_VERSION = '0.30.1'
  GUEST_NODE_VERSION = 'stable'

  # 必要なパッケージをインストール
  config.vm.provision "shell", privileged: true, inline: <<-__SCRIPT__
    export DEBIAN_FRONTEND=noninteractive
    apt-get update -y

    # database
    apt-get install -y debconf-utils
    debconf-set-selections <<< "mysql-server-5.5 mysql-server/root_password password \"\""
    debconf-set-selections <<< "mysql-server-5.5 mysql-server/root_password_again password  \"\""
    apt-get install -y mysql-server-5.5 mysql-client-5.5 libmysqlclient-dev
    apt-get install -y redis-server

    # develop env
    apt-get install -y git-core curl zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev python-software-properties libffi-dev

    # for testing
    apt-get install -y phantomjs qt5-default libqt5webkit5-dev
  __SCRIPT__

  # RubyとNodeをコンパイル
  config.vm.provision "shell", privileged: false, inline: <<-__SCRIPT__
    # install rbenv
    git clone git://github.com/sstephenson/rbenv.git ~/.rbenv
    git clone git://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
    git clone git://github.com/sstephenson/rbenv-gem-rehash.git ~/.rbenv/plugins/rbenv-gem-rehash
    echo $'export PATH="\$HOME/.rbenv/bin:\$HOME/.rbenv/plugins/ruby-build/bin:\$PATH"' >> ~/.bashrc
    echo $'eval "\$(rbenv init -)"' >> ~/.bashrc
    echo $'. ~/.nvm/nvm.sh' >> ~/.bashrc
    echo $'gem: --no-ri --no-rdoc' > ~/.gemrc

    # install nvm
    curl -s https://raw.githubusercontent.com/creationix/nvm/v#{GUEST_NVM_VERSION}/install.sh | bash

    export PATH="$HOME/.rbenv/bin:$HOME/.rbenv/plugins/ruby-build/bin:$PATH"
    eval "$(rbenv init -)"
    . ~/.nvm/nvm.sh

    #{defined?(GUEST_RUBY_VERSION) ? "rbenv install %s; rbenv global %s; gem install bundler" % [GUEST_RUBY_VERSION, GUEST_RUBY_VERSION] : ''}
    #{defined?(GUEST_NODE_VERSION) ? "nvm install %s; nvm alias default %s" % [GUEST_NODE_VERSION, GUEST_NODE_VERSION] : ''}
  __SCRIPT__

  config.vm.provider "virtualbox" do |vb|
    vb.memory = ENV["VM_MEMORY"] || "1024"
    vb.customize ["guestproperty", "set", :id, "/VirtualBox/GuestAdd/VBoxService/--timesync-set-threshold", 10000]
  end
end

=begin
シンプルなRails開発環境

- Ruby
- Node (オプション)
- MySQL
- Redis
- PhantomJS
- Webkit5 (for Capybara)

参照:
https://gorails.com/setup/ubuntu/14.10
=end
```

## 初回起動

provisioningの時にNodeのバージョンを指定した場合、コンパイルでメモリを食うので、VM_MEMORYを指定します。

```
VM_MEMORY=2048 vagrant up
```


## 新規プロジェクトの作り方

```
cd /vagrant
gem install rails
rails new . --database=mysql --skip-turbolinks
rake db:create

vi Gemfile
:development, :testに下記の3行を追加
gem 'rspec-rails', '~> 3.0' 
gem 'factory_girl_rails', '~> 4'
gem 'guard'


bundle install
guard init
rails generate rspec:install
```

## .gitignore

node周りのファイルも追加

```
# Ignore bundler config.
/.bundle

# Ignore bundler gems.
vendor/bundle

# Ignore the default SQLite database.
/db/*.sqlite3
/db/*.sqlite3-journal

# Ignore all logfiles and tempfiles.
/log/*
!/log/.keep
/tmp

# Ignore node files
/node_modules
/bower_components

# Ignore other unneeded files.
/.vagrant
doc/
*.swp
*~
.project
.DS_Store
```

## テンプレートの編集

`app/views/layouts/application.html.erb`のbodyにクラスを追加

```app/views/layouts/application.html.erb
<body class="<%= 'controller-%s controller-%s-%s action-%s' % [controller.controller_name, controller.controller_name, controller.action_name, controller.action_name] %>">
```

## Vagrant上での開発の注意点

- Guardで自動テストが走らないときは、`guard -p`とポーリングオプションを付ける
- サーバを起動するときは、`rails s -b 0.0.0.0`