After update hook not being called for DataMapper model with dm 1.0.2

Posted by Macario on Stack Overflow See other posts from Stack Overflow or by Macario
Published on 2010-12-27T23:47:33Z Indexed on 2010/12/27 23:54 UTC
Read the original article Hit count: 204

Filed under:
|
|

Hi, I've the following model and I want to execute a method on save and update, problem is that the hook is not being executed on update.

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

This spec fails:

  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

And this passes:

  it  'should call encrypt_password! on create' do
    subject.should_receive(:encrypt_password!) 
    subject.save.should be_true
  end

I've also tried with after :update in addition to after :save with no success.

Am I missing something?

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about ruby