使用 dm 1.0.2 时,未调用 DataMapper 模型更新挂钩之前
我有以下模型,我想在保存和更新时执行一个方法,问题是钩子在更新时没有执行。
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 :save 之外,我还尝试了 after :update ,但没有成功。
我错过了什么吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为这是 datamapper 的一个错误,但您可以采取一些措施来解决它,直到他们解决问题。
您可以重写 User 类中的 save 方法,然后调用必要的 encrypt_password!自定义保存方法中的方法。然后只需调用父级的 save 方法即可执行 datamapper db save。
您的保存方法可能如下所示,
我知道这违反了 datamapper 使用挂钩的面向方面的设计方法,但这将允许您在需要时立即完成项目。
I think this is a bug with datamapper but there are a couple of things that you could do to get around it until they fix the problem.
You could override the save method in your User class and then call then necessary encrypt_password! method from within your custom save method. Then simply call the parent's save method to perform the datamapper db save.
Your save method could look like this
I know this violates the aspect-oriented design approach that datamapper has using hook but this will allow you to get your project done now if you need to.
我知道有点晚了,但我不认为这是一个错误。仅当资源有效时才会调用 create 和 save 挂钩。您想要将
before :save, :encrypt_password!
更改为:I know it's a little late, but I don't think this is a bug. The create and save hooks will only be called if a resource is valid. You want to change your
before :save, :encrypt_password!
to: