メモ置き場

いろんなメモを置いておく場所。自分用ですが、誰かの助けにもなるかも。

Railsアプリ開発メモ -rails newからmvc作成まで- (02)

Rails new から mvcそれぞれの作成くらいまでのメモ。執筆時点のRailsは4.1.6。

目次

  1. Railsアプリ開発メモ -rails newからmvc作成まで- (01)

    • rails newGitHub初期設定
  2. Railsアプリ開発メモ -rails newからmvc作成まで- (02)

    • gemインストール〜初期設定関連
  3. Railsアプリ開発メモ -rails newからmvc作成まで- (03)

    • rails g modelrails g controller(とrails g integration_test)

README.rdoc -> README.md

rails newで吐かれるREADMEは.rdoc形式だけど、.mdに慣れているのでそちらに変更。

$ git mv README.rdoc README.md
$ vim README.md
    ... いい感じに編集

.gitignore修正

gitignore.ioというサービスが便利。

gitignore.io - Create Useful .gitignore Files For Your Project

適当に入力してできた.gitignoreを差し替える。 ただし、デフォルトだと.rspecも対象に挙がっているので、必要であれば該当行を削除しておく。

config/application.rb修正

ja.ymlダウンロード

$ wget https://raw.githubusercontent.com/svenfuchs/rails-i18n/master/rails/locale/ja.yml -P config/locales

修正後のconfig/application.rb(抜粋)

module <prj>
  class Application < Rails::Application
    ...
    config.time_zone = 'Tokyo'
    ...
    config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
    config.i18n.default_locale = :ja

    config.generators do |g|
      g.javascripts false
      g.stylesheets false
      g.helper false
      g.test_framework :rspec,
        fixture: true,
        view_specs: false,
        helper_specs: false,
        routing_specs: false,
        controller_specs: true,
        request_specs: true
      g.fixture_replacement :factory_girl, dir: 'spec/factories'
    end
  end
end

Gemfile整形

使うGem

開発作業効率化

  • spring
  • guard
  • better_errors

N+1問題対処

  • bullet

テスト

修正後のGemfile

source 'https://rubygems.org'


group :default do
  gem 'rails', '4.1.6'
  gem 'sass-rails', '~> 4.0.3'
  gem 'uglifier', '>= 1.3.0'
  gem 'jbuilder', '~> 2.0'
end

group :test, :development do
  gem 'sqlite3'
end

group :doc do
  gem 'sdoc', '~> 0.4.0'
end

group :development do
  gem 'spring'
  gem 'spring-commands-rspec'

  gem 'guard-rspec'
  gem 'guard-spring'

  gem 'better_errors'

  gem 'bullet'
end

group :test do
  gem 'rspec-rails'
  gem 'factory_girl_rails'
  gem 'database_cleaner'
end

bundle install

  • --path オプションで指定の場所にインストール
  • --jobs=4オプションでbundlerが早くなるらしい
bundle install --path vendor/bundle --jobs=4

rspecインストールとrspec/factorygirl設定

$ rails g rspec:install

修正後のspec/rails_helper.rb(抜粋)

RSpec.configure do |config|
  ...
  config.before :all do
    FactoryGirl.reload
  end
end

修正後のspec/spec_helper.rb(抜粋) ※コメントアウトをいじるだけ

RSpec.configure do |config|
  ...
  config.order = :random
  ...
  Kernel.srand config.seed
end

bullet設定

好みにあわせて設定

修正後のconfig/environments/development.rb(抜粋)

Rails.application.configure do
  …
  config.after_initialize do
    Bullet.enable = true
    Bullet.alert = false
    Bullet.bullet_logger = true
    Bullet.console = true
    Bullet.rails_logger = true
  end
end

guard設定

$ spring binstub --all
* bin/rake: spring already present
* bin/rspec: generated with spring
* bin/rails: spring already present
$ bundle exec guard init spec spring
hh:mm:ss - INFO - Writing new Guardfile to /*****/<prj>/Guardfile
hh:mm:ss - INFO - rspec guard added to Guardfile, feel free to edit it
hh:mm:ss - ERROR - Could not load 'guard/spec' or '~/.guard/templates/spec' or find class Guard::Spec
hh:mm:ss - ERROR - Could not load 'guard/spring' or '~/.guard/templates/spring' or find class Guard::Spring

エラーメッセージでぐぐったけど情報出てこない。(多分テンプレートが無いと言っているだけだが、)とりあえず保留のまま進める。

bin/にパスを通す

binstubで生成したbin/rake bin/rspec bin/railsを実行するたびにbin/をつけるのが面倒臭いので、環境変数PATH<rails_root>/binを追加しておく

以降次回。

参考