Rails事始め
新しくプロジェクト作る時に毎回見直しをする。
前提
- 開発環境はMac
- Rubyのインストールにはrbenvを使用する
- gemは全てプロジェクト以下に入れたい(システムgemには入れたくない)
- DBはMySQLを使う
- Turbolinksは使わない
- テストはRspec
- 環境依存な値は環境変数を使っていく(direnv使用)
- その時その時で有用なgemを使いたいので、Templateは使用しない
プロジェクト作成
$ mkdir project_name $ cd project_name $ rbenv local 2.2.0 # rails 4.2のインストール準備 $ bundle init $ sed -i '' -e '1d' -e 's/# //g' Gemfile $ bundle install --path vendor/bundle --without staging production --jobs 4 # -T = test-unit skip # -B = bundle install skip # -f = force overwrite files $ bundle exec rails new . -TBf -d mysql --skip-turbolinks $ bundle install $ curl -o .gitignore https://raw.githubusercontent.com/github/gitignore/master/Rails.gitignore $ git init $ git remote add origin your:repository $ git add -A $ git commit -m 'rails setup'
ここまでで一旦Commitしておく。Railsの(ほぼ)デフォルトの状態。
初期設定
Gemfile設定
source 'https://rubygems.org' # rubyのバージョン指定とrailsだけ最初に指定 # 他のgemはASCII順に並べておく ruby '2.2.0' gem 'rails', '4.2.0' gem 'airbrake' # HerokuにErrbit立ててるので入れる gem 'bootstrap-sass' # 大体Bootstrapで作る gem 'bootstrap-sass-extras' gem 'coffee-rails', '~> 4.1.0' gem 'jbuilder', '~> 2.0' gem 'jquery-rails' gem 'kaminari' # ページ送りは大体入れる gem 'mysql2' gem 'newrelic_rpm' # 便利 gem 'sass-rails', '~> 5.0' gem 'slim-rails' # テンプレートエンジンはSlimがお好き gem 'uglifier', '>= 1.3.0' group :development, :test do gem 'pry-byebug' gem 'pry-rails' gem 'pry-rescue' gem 'rspec-rails' gem 'spring' gem 'web-console', '~> 2.0' end group :test do gem 'database_rewinder' gem 'faker' gem 'factory_girl_rails' end
$ bundle
spring(環境変数)設定
$ bundle exec spring binstub --all $ echo 'export PATH=$PWD/bin:$PATH' > .envrc $ echo 'export DATABASE_PASSWORD=xxxxx' >> .envrc $ direnv allow
database.yml設定
default: &default adapter: mysql2 encoding: utf8 pool: 5 username: root password: <%= ENV['DATABASE_PASSWORD'] %> socket: /tmp/mysql.sock development: <<: *default database: project_name_development test: <<: *default database: project_name_test production: url: <%= ENV['DATABASE_URL'] %>
application.rb設定
require File.expand_path('../boot', __FILE__) require "active_model/railtie" require "active_job/railtie" require "active_record/railtie" require "action_controller/railtie" require "action_mailer/railtie" require "action_view/railtie" require "sprockets/railtie" Bundler.require(*Rails.groups) module ProjectName class Application < Rails::Application config.time_zone = 'Tokyo' config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}').to_s] config.i18n.default_locale = :ja config.generators do |g| g.assets false g.helper false g.template_engine :slim g.fixture_replacement: :factory_girl, dir: 'spec/factories' g.test_framework :rspec, view_specs: false, routing_specs: false, helper_specs: false, integration_tool: false end config.active_record.raise_in_transactional_callbacks = true end end
Airbrake(Errbit)設定
$ touch config/initializers/airbrake.rb
Airbrake.configure do |config| config.api_key = "#{ENV["ERRBIT_API_KEY"]}" config.host = 'hostname' config.port = 80 config.secure = config.port == 443 end
rspec設定
$ rails generate rspec:install $ bundle binstubs rspec-core $ cat spec/rails_helper.rb
ENV["RAILS_ENV"] ||= 'test' require 'spec_helper' require File.expand_path("../../config/environment", __FILE__) require 'rspec/rails' ActiveRecord::Migration.maintain_test_schema! RSpec.configure do |config| config.use_transactional_fixtures = true config.infer_spec_type_from_file_location! config.include FactoryGirl::Syntax::Methods config.before :all do FactoryGirl.reload end config.before :suite do DatabaseRewinder.clean_all end config.after :each do DatabaseRewinder.clean end end
大体こんな感じだろうか