WEB+DB PRESS Vol.93 Rails 5の特集が良い

WEB+DB Vol.93

WEB+DB PRESS Vol.93

WEB+DB PRESS Vol.93

WEB+DB をため続けたのを今読んでる関係で今更感はありますが、ご容赦ください。
ActionCableの詳細などの記載があります。
以下、個人的に気になったとこ。

rails test

rakeの代わりになるものが標準機能で追加されていました。
行番号指定、-f でエラー時は即終了などは良いですね。
Ruby on Rails 5.0 リリースノート | Rails ガイド

Active Record Attributes API

システムのInputと内部やDBの型変換を実装する機能。
こういう要件はちょくちょくあるので、これは良さそう。

#app/types/subscripton.rb

class Subscription < ActiveRecord::Type::Boolean
  def cast(value)
    case value
    when 'OK'
      true
    when 'NG'
      false
    else
      super
    end
  end

  def serialize(value)
    if value
      'OK'
    else
      'NG'
    end
  end
end

#config/initializers/types.rb
ActiveRecord::Type.register(:subscription, Subscription)

#app/models/user.rb
class User < ApplicationRecord
  attribute :email_subscription, :subscription
end

ActiveRecord::Relation#or

or 使えるようになったんだ。

has_secure_token

これも良いです。

class User < ApplicationRecord
  has_secure_token :auth_token
end

としておくと、regenerate_auth_tokenで再生成可能。
(Unique Index 張っておくのがbetter)

See Also