LoginSignup
7
7

More than 5 years have passed since last update.

Factory GirlのファイルをGuardで読み直す

Posted at
Guardfile
require 'active_support/inflector'

guard :rspec, version: 2 do
  watch(%r{^spec/factories/(.*)\.rb$}) { |m| "app/models/#{m[1].singularize}.rb" }
end

Factory GirlのRailsジェネレータは spec/factories/ 以下にモデル名の複数形のファイルをつくる。
単純に "#{m[1]}s.rb" としたのでは不規則変化に対応できない。

RailsやPadrinoはActiveSupportを使用しているので、これが備える単語の活用形を変換する機能、ActiveSupport::Inflectorを使う。

require 'active_support/all' とするとActiveSupportのすべてのファイルが読み込まれるが、かなりの量があり体感ではっきりと遅いと感じるほどのラグもあるため、特定の機能のみを使いたい場合はそれを提供するファイルのみを読み込むほうがよい。

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