使用 dm 1.0.2 时,未调用 DataMapper 模型更新挂钩之前

发布于 2024-10-09 12:46:17 字数 1194 浏览 2 评论 0原文

我有以下模型,我想在保存和更新时执行一个方法,问题是钩子在更新时没有执行。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

初与友歌 2024-10-16 12:46:17

我认为这是 datamapper 的一个错误,但您可以采取一些措施来解决它,直到他们解决问题。

您可以重写 User 类中的 save 方法,然后调用必要的 encrypt_password!自定义保存方法中的方法。然后只需调用父级的 save 方法即可执行 datamapper db save。

您的保存方法可能如下所示,

def save
  encrypt_password!
  super
end

我知道这违反了 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

def save
  encrypt_password!
  super
end

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.

煞人兵器 2024-10-16 12:46:17

我知道有点晚了,但我不认为这是一个错误。仅当资源有效时才会调用 createsave 挂钩。您想要将 before :save, :encrypt_password! 更改为:

before :valid?, :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:

before :valid?, :encrypt_password!
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文