【问题标题】:Devise: remove module for extended model设计:移除扩展模型的模块
【发布时间】:2020-02-10 18:49:23
【问题描述】:

我有一个大型的旧版应用程序,型号为 User。我添加了新类型的用户:

class ExtendedUser < User
  devise :database_authenticatable, ...
end

新类型的用户正在扩展现有用户,因此为User 编写的所有代码都应该适用于ExtendedUser,不扩展是不可能的。

问题是如何从来自UserExtendedUser 中删除设计模块?

User 是可确认的,ExtendedUser 不应该是。

我怀疑删除模块是不可能的,最简单的方法是将.skip_confirmation! 放在代码中将创建此用户以及电子邮件可以更改的所有位置之前。

【问题讨论】:

  • 我真的不认为这是可能的。这是Module#include 之上的语法糖,Ruby 没有“取消包含”机制来删除包含后的模块。
  • 你可以重写子类中的confirmed? 方法(返回true),它的行为就像它的确认一样。
  • @max 已检查,确认指令仍在发送,.update(email: 'new@ema.il') 仍在更新列 unconfirmed_email 并发送电子邮件,所以这个技巧不会否定所有效果

标签: ruby-on-rails model devise


【解决方案1】:

您可以只使用继承来动态评估包含哪些模块:

class User < User
  devise *devise_modules
  # ...
  def self.devise_modules
    [:database_authenticatable, ...]
  end
end

class ExtendedUser < User
  # ...
  def self.devise_modules
    super.excluding(:confirmable)
  end
end

【讨论】:

    猜你喜欢
    • 2018-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-15
    • 1970-01-01
    • 2011-06-04
    • 2011-02-14
    • 2012-07-04
    相关资源
    最近更新 更多