LoginSignup
3
3

事前準備 から Rails 6 セットアップ(Postgresqlバージョン)

Last updated at Posted at 2019-10-16

事前準備

Postgresql をインストール

インストール

$ brew reinstall postgresql

initdbで初期化する際、既に存在する場合にエラーとなるので、事前にフォルダを削除しておく必要がある

$ rm -rf /usr/local/var/postgres

initdb する(ソートが辞書順に並んでしまうことやソート処理が遅くなる等の弊害があるので--no-localeオプションつまりロケールをCにする)

参考)https://se.ykysd.com/2015/09/28/orderby/

$ initdb /usr/local/var/postgres -E utf8 --no-locale

自動起動設定

$ ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents

自動起動を手動で叩いて postgresql を起動する

$ launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

ユーザー作成・権限割当

開発用なのでフル権限を与える

  • ユーザー名: root
  • パスワード: pass
  1. ユーザー作成

    $ createuser -P root
    
  2. ロールに権限割当

    $ psql -d postgres
    postgres=# ALTER ROLE root WITH SUPERUSER;
    

yarn をインストール

Rails で webpacker を使用する際に yarn が必要ためインストールする。
※ node と yarn の関係などを知らない場合はネットで調べること

また、node のバージョン管理できるように、nodenv もインストールする。

$ brew install nodenv
$ echo 'eval "$(nodenv init -)"' >> ~/.zprofile
$ exec $SHELL -l
$ nodenv install --list
$ nodenv install 12.10.0
$ nodenv global 12.10.0
$ npm install -g yarn

グローバルにある Ruby を汚さずに Railsインストール

gem install rails を行うと ~/.rbenv/versions/2.5.1/lib/ruby/gems/2.6.4/gems の中に rails がインストールされます。 rails new をする時しか使用しないのにグローバルにインストールされてしまうのは無駄なので、インストールしない方法で進めます。

rbenv インストール

$ brew install rbenv
$ echo 'eval "$(rbenv init -)"' >> ~/.zprofile
$ exec $SHELL -l

rbenv で ruby をインストール

$ rbenv install 2.6.4
$ rbenv global 2.6.4

bundler をインストール

$ gem install bundler

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

$ mkdir report_manager
$ cd report_manager

.gitignore ファイルを配置

プロジェクトディレクトリ直下に配置します。本ファイル配置後 git init を行い、手順完了ごとに commit を追加しましょう。

.gitignore
 # Ignore bundler config.
 /.bundle
 /vendor/bundle

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

 # Ignore uploaded files in development.
 /storage/*
 !/storage/.keep

 /public/assets
 .byebug_history

 # Ignore master key for decrypting credentials and more.
 /config/master.key

 /public/packs
 /public/packs-test
 /node_modules
 /yarn-error.log
 yarn-debug.log*
 .yarn-integrity

.ruby-version ファイルを配置

rbenv の機能で プロジェクトディレクトリに移動した際に自動で ruby のバージョンを切り替えてくれる設定ファイルを配置する。

.ruby-version
2.6.4

Gemfile 作成

$ bundle init

Gemfile を編集し、 rails のコメントアウトを外す

Gemfile
# gem "rails" ← 削除
gem "rails", "6.0.0" ← 追加

gem インストール

$ bundle install --path vendor/bundle

Rails プロジェクト作成

$ bundle exec rails new . -d postgresql --skip-sprockets

--skip-sprockets は sprockets を使わず、webpacker を使用するため追加
実行すると、Gemfileを上書きしていいかと表示されるので Yes にする。

database.yml 設定

config/database.yml を以下の通り編集する

database.yml
  username: root
  password: pass

Rails 起動

$ bin/rails db:create
$ bin/rails s

起動確認

ブラウザのアドレスバーに localhost:3000 と入力しEnter

Webpacker の設定

  1. app/assets を削除する

  2. layout/application.html.erbstylesheet_link_tagstylesheet_pack_tag に差し替える

  3. 画面のちらつきを抑えるため config/webpacker.ymlextract_csstrue にする

  4. Webpacker を使うので Sprockets のディレクトリ app/assets を削除する

Turbolinks の無効化

他のJSライブラリと相性が悪いため turbolinks を無効化する

  1. Gemfiletarbolinks を削除
  2. app/javascript/packs/application.jsrequire("turbolinks").start() を削除
  3. app/views/layouts/application.html.hamldata-turbolinks-track を削除
  4. package.jsonturbolinks を削除

Slim 導入

  1. Gemfilegem 'slim-rails' を追加
  2. 既存の erb ファイルを slim に変換する
    https://qiita.com/mocchicc/items/abab4cdfac7b542742a6

RSpec 導入

  1. Gemfile に以下追加

    group :development do
      gem 'factory_bot_rails'
    end
    
    group :test do
      gem 'rspec-rails', '~> 4.0.0.beta2'
      gem 'rails-controller-testing'
    end
    
  2. rspecに必要なファイルを生成する

    $ bin/rails generate rspec:install
    
  3. bin/rspec コマンドを生成する

    $ bundle binstubs rspec-core
    
  4. rspec の設定ファイルを変更する

    rails_helper.rb に設定追加

    rails_helper.rb
    
    # FactoryBot.create を create で呼べるようにする
    config.include FactoryBot::Syntax::Methods
    
    # rspec3.3の aggregate_failures オプションをデフォルトとする
    config.define_derived_metadata do |meta|
      meta[:aggregate_failures] = true unless meta.key?(:aggregate_failures)
    end
    
    # focus を付与したテストのみ実行する
    config.filter_run_when_matching :focus
    
  5. Rspec を使うので Minitest のディレクトリ test を削除する

Rails Console を便利にする

  • Rails Console や デバッグを便利にするための gem を導入する

    Gemfile
    group :development, :test do
      gem 'awesome_print'
      gem 'pry-byebug'
      gem 'pry-rails'
    end
    
  • .pryrc ファイルを作成する

    • Railsプロジェクトのrootディレクトリに.pryrcファイルを配置しておけばデバッグ中に利用できるエイリアスが貼れる
    .pryrc
    if defined?(PryByebug)
      Pry.commands.alias_command 'c', 'continue'
      Pry.commands.alias_command 's', 'step'
      Pry.commands.alias_command 'n', 'next'
      Pry.commands.alias_command 'f', 'finish'
    end
    

Git リモートリポジトリ の設定

今回は Bitbucket でソース管理を行うための手順です。

  1. Bitbucket にログインして 新規リポジトリを作成します。
    https://ja.confluence.atlassian.com/bitbucket/create-a-repository-800695642.html

  2. 新規リポジトリ作成後、画面の手順に従い push しましょう。

一通りの基本設定は完了。それではアプリの実装に入りましょう。

3
3
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
3
3