Update To Rails4

( Rails )

自分のサービスのRailsのversionがまだ3.2だったので地道にRails4にアップデートしました。 その際に必要になった作業をメモっときます。

attr_accessible is extracted out of Rails into a gem.

E, [2013-07-04T09:54:39.926880 #2726] ERROR -- : `attr_accessible` is extracted out of Rails into a gem. Please use new recommended protection model for params(strong_parameters) or add `protected_attributes` to your Gemfile to use old one. (RuntimeError)

attr_accessibleはRailsから省いてstrong_parametersを利用してとのこと。 振るいものを利用するならprotected_attributesっていうGem使ってね。 しかし、今回はstrong_parametersを記述していきます。

とりあえずModelからの排除を行う。

DEPRECATION WARNING: config.whiny_nils option is deprecated and no longer works.

DEPRECATION WARNING: config.whiny_nils option is deprecated and no longer works. (called from block in <top (required)> at /private/var/www/frustration/config/environm
ents/development.rb:10)
  • whiny_nils
    • 初期化されていないオブジェクトが呼び出させたときに、警告を表示する
-  # Log error messages when you accidentally call methods on nil
-  config.whiny_nils = true
-

config.eager_load is set to nil.

config.eager_load is set to nil. Please update your config/environments/*.rb files accordingly:

  * development - set it to false
  * test - set it to false (unless you use a tool that preloads your test environment)
  * production - set it to true

必須の設定項目増えたのでrails newして作成された環境ファイルを眺めてみる。

The provided regular expression

E, [2013-07-04T12:00:26.429744 #7376] ERROR -- : The provided regular expression is using multiline anchors (^ or $), which may present a security risk. Did you mean to use \A and \z, or forgot to add the :multiline => true option? (ArgumentError)

正規表現の処理にもセキュリティが強化されていました。 一行を考慮したデータのチェック^なら\A$なら\zにすると意図したチェックになるよ。 複数行を考慮してるんだよ!ってときは:multiline => trueにしてね!

自分のValidationは一行のデータを考慮しているので修正します。

You should not use the match method in your router without specifying an HTTP method.

E, [2013-07-04T12:07:42.318503 #7626] ERROR -- : You should not use the `match` method in your router without specifying an HTTP method.
If you want to expose your action to both GET and POST, add `via: [:get, :post]` option.
If you want to expose your action to GET, use `get` in the router:
  Instead of: match "controller#action"
  Do: get "controller#action" (RuntimeError)

matchの代わりにHTTPメソッドをちゃんと宣言しようぜ!っていうメッセージ。

-  match 'home' => 'home#index', :via => :get
+  get 'home' => 'home#index'

なかんじで修正。

これで起動はうまくいった。strong_parameterによる警告はあるけどこれは後ほど修正することにする。 その後は、テストを流して全部通す。

bundle exec rspec spec

DEPRECATION WARNING: Calling #find(:first) is deprecated.

DEPRECATION WARNING: Calling #find(:first) is deprecated. Please call #first directly instead. You have also used finder options. These are also deprecated. Please build a scope instead of using finder options. (called from block in create_with_item at /private/var/www/frustration/app/services/service_fuman.rb:51)

以下のような推奨されていない使い方をしていたので修正しました。

Hoge.find(:first, condition: {user_id: @user.id})
↓
Hoge.where(user_id: @user.id).first

strong_parameter

ここはひたすらcontroller側に制限の内容を記述。

ホワイトリストとして許可するパラメータを記述していきます。

  private
    def category_params
      params.require(:category).permit(:user_id, :name)
    end

DEPRECATION WARNING: You didn’t set config.secret_key_base.

DEPRECATION WARNING: You didn't set config.secret_key_base. Read the upgrade documentation to learn more about this new config option. (called from env_config at /private/var/www/frustration/vendor/bundle/ruby/2.0.0/gems/railties-4.0.0/lib/rails/application.rb:141)

secret_tokenはもう推奨されていなくてsecret_key_baseに名前が変更されたようです。

        if config.secret_key_base.blank?
          ActiveSupport::Deprecation.warn "You didn't set config.secret_key_base. " +
            "Read the upgrade documentation to learn more about this new config option."

          if config.secret_token.blank?
            raise "You must set config.secret_key_base in your app's config."
          end
        end

まとめ

これですべてテストがうまくいったのでPull Requestを自分に送ってfin.

https://github.com/nakajijapan/frustrationme_app/pulls

メジャーバージョンが上がってるということで、新しい機能が追加されたり、機能が廃止されたり、名前が変更されていたりと、いろいろと変更するべき点は多かったですがテストを書いていたので実行させては修正、実行させては修正の連続で効率的に修正作業ができたんじゃないかとおもいます(最終的にはWEB上での確認はしましたが)。

あと、今回は規模が小さいし、修正箇所もそんなに多くなかったのでstrong_parametersの部分も変更させてしましましたが、

gem 'strong_parameters'

であらかじめ3系で実装しておくと細かい粒度で修正できて安心かもしれませんね。

さてさて、アップデートできてよかった。。。

Ref

http://www.upgradingtorails4.com/ http://railscasts.com/episodes/415-upgrading-to-rails-4?language=ja&view=asciicast