У меня следующая модель, и я хочу выполнить метод при сохранении и обновлении, проблема в том, что при обновлении ловушка не выполняется.
class User
include DataMapper::Resource
include BCrypt
property :id, Serial
property :email, String, :index => true
property :crypted_password, String, :accessor => :private
...
attr_accessor :password, :password_confirmation
before :save, :encrypt_password!
# also tried the following with no success:
# before :update, :encrypt_password!
# and tried this but hell was never raised
# before :update do
# raise 'hell'
# end
def encrypt_password!
self.crypted_password = Password.create password
end
end
Эта спецификация не работает:
it 'should call encrypt_password! on update' do
subject.save.should be_true
subject.should_receive(:encrypt_password!)
subject.update(:password => 'other-password', :password_confirmation => 'other-password').should be_true
end
И это проходит:
it 'should call encrypt_password! on create' do
subject.should_receive(:encrypt_password!)
subject.save.should be_true
end
Я также пытался использовать after: update в дополнение к after: сохранить безуспешно.
Я что-то упустил?