特定のパラメーター (コード) がオブジェクト属性 (temporary_code) と一致する場合にのみ遷移を提供するには、state_machine イベントが必要です。
このコードをテストすると:
class User < ActiveRecord::Base
def initialize
@temporary_code = 'right'
end
state_machine :initial => :inactive do
event :activate! do
transition :inactive => :active, :if => lambda{ |code| code == @temporary_code }
end
state :inactive do
def active?
false
end
end
state :active do
def active?
true
end
end
end
end
しかし、どんなコードが与えられても遷移しません。以下の Rspec テストはエラーを返します。
describe "activation" do
let(:user) { User.create }
before { user.activate!('right') }
specify { user.should be_active }
end
それの何が問題なのですか?