LoginSignup
2
2

More than 5 years have passed since last update.

Rails 5 セットアップメモ2(完全に個人的)

Last updated at Posted at 2016-08-19

config

https://github.com/railsconfig/config
の通りにいれる。

# Gemfile
gem 'config'
$ bundle install
$ rails g config:install

config/database.ymlを修正する。こんな感じに。

default: &default
  adapter: mysql2
  encoding: utf8
  pool: 5
  username: <%= Settings.db.default.username %>
  password: <%= Settings.db.default.password %>

development:
  <<: *default
  database: abc_development

test:
  <<: *default
  database: abc_test

staging:
  <<: *default
  database: abc_staging
  host: <%= Settings.db.default.host %>
  port: <%= Settings.db.default.port %>
  password: <%= Settings.db.default.password %>

production:
  <<: *default
  database: abc_production
  host: <%= Settings.db.default.host %>
  port: <%= Settings.db.default.port %>
  password: <%= Settings.db.default.password %>

そしたら、とりあえずdevelopment.ymlとdevelopment.local.ymlつくる。

config/settings/development.yml

db:
  default:
    username: 'local.yml'
    password: 'local.yml'
    host: 127.0.0.1
    port: 3306

config/settings/development.local.yml
(差分だけ書くのがよいかと)

db:
  default:
    username: actual_username
    password: actual_password

同じように
config/settings/test.yml
config/settings/test.local.yml
も書く。 (この段階だとそれぞれcpでよさそう)

JSON Schema

prmd, committeeを導入

# Gemfile
gem 'committee'
group :development do
  gem 'prmd'
end
$ bundle install

RSpec 3

rails g modelとかに関わるので、早めにセットアップ。

# Gemfile
group :development, :test do # 多分このブロックすでにある。あるときはその中に追記する。
  gem 'rspec-rails'
end
$ bundle install
$ bundle exec rails g rspec:install

焦らず1個ずついれてcommitしてこ。

Factory Girl

これもrails g modelとかに関わるので、早めにセットアップ。

# Gemfile
group :development, :test do # 多分このブロックすでにある。あるときはその中に追記する。
  gem 'factory_girl_rails'
end
$ bundle install

これで bundle exec rails g model abcすると、以下が生成される

  • app/models/abc.rb
  • db/migrate/20160819182859_create_abcs.rb
  • spec/models/abc_spec.rb
  • spec/factories/abcs.rb

これで十分であれば、そろそろ開発開始できる :muscle:

spec/rails_helper.rb に以下を追記しておく。

# RSpec.configure do |config| の中
  config.include FactoryGirl::Syntax::Methods

database_cleaner (database_rewinder)

テストのお供にdatabase_cleaner
https://github.com/DatabaseCleaner/database_cleaner

database_cleanerのstrategyを :transaction で動くようにちゃんとすすめたい、とかいう理由があり (truncationだとおそいので) 早めにセットアップしておきたかった。

しかし今回はdatabase_rewinderの方を使ってみることにする。
https://github.com/amatsuda/database_rewinder

Why is it fast?
database_rewinder memorizes every table name into which INSERT SQL was performed during each test case. Then it executes DELETE SQL only against these tables when cleaning. So, the more number of tables you have in your database, the more benefit you will get.

セットアップは以下.

# Gemfile
group :development, :test do # 多分このブロックすでにある。あるときはその中に追記する。
  gem 'database_rewinder'
end
$ bundle install

公式ドキュメント通りの追記をする。
spec/rails_helper.rb に追記。

# RSpec.configure do |config| のブロックの中に書く。

  config.before(:suite) do
    DatabaseRewinder.clean_all
  end

  config.after(:each) do
    DatabaseRewinder.clean
  end

Services の追加

mkdir app/services する

config/application.rbの中に追加。

config.autoload_paths << %W(#{config.root}/services)

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