Ruby on rails 设计:什么时候定制太多?

Ruby on rails 设计:什么时候定制太多?,ruby-on-rails,devise,customization,ruby-on-rails-3.1,Ruby On Rails,Devise,Customization,Ruby On Rails 3.1,我正在为自己写一份申请,所以我不着急,我唯一的目标就是把事情做好。 对于身份验证,我使用Desive,但结果是我对它进行了大量定制。 我在Rails3.1中看到了一些很好的特性,这些特性可以使自己更容易实现auth 一般来说,Desive什么时候停止发挥作用并开始妨碍您的工作? 下面是我目前的自定义列表,当然除了视图之外,但我仍然希望实现至少对SEO友好的URL # model/User.rb #this method is called by devise to check for "ac

我正在为自己写一份申请,所以我不着急,我唯一的目标就是把事情做好。 对于身份验证,我使用Desive,但结果是我对它进行了大量定制。 我在Rails3.1中看到了一些很好的特性,这些特性可以使自己更容易实现auth

一般来说,Desive什么时候停止发挥作用并开始妨碍您的工作? 下面是我目前的自定义列表,当然除了视图之外,但我仍然希望实现至少对SEO友好的URL

# model/User.rb
  #this method is called by devise to check for "active" state of the model
  def active?
    #remember to call the super
    #then put our own check to determine "active" state using 
    #our own "is_active" column
    super and self.is_active?
  end


protected #====================================================================

  # find in db the user with username or email login
  def self.find_record(login)
    where(attributes).where(["name = :value OR email = :value", { :value => login }]).first
  end

  # allow no case sensitive email
  # (saved downcase, fetched downcase)
  def self.find_for_authentication(conditions)
    conditions[:email].downcase!
    super(conditions)
  end

  # find the user in the db by username or email
  def self.find_for_database_authentication(conditions)
    login = conditions.delete(:login)
    where(conditions).where(["name = :value OR email = :value", { :value => login }]).first
  end

  # Attempt to find a user by it's email. If a record is found, send new
  # password instructions to it. If not user is found, returns a new user
  # with an email not found error.
  def self.send_reset_password_instructions(attributes={})
    recoverable = find_recoverable_or_initialize_with_errors(reset_password_keys, attributes, :not_found)
    recoverable.send_reset_password_instructions if recoverable.persisted?
    recoverable
  end

  def self.find_recoverable_or_initialize_with_errors(required_attributes, attributes, error=:invalid)
    case_insensitive_keys.each { |k| attributes[k].try(:downcase!) }

    attributes = attributes.slice(*required_attributes)
    attributes.delete_if { |key, value| value.blank? }

    if attributes.size == required_attributes.size
      if attributes.has_key?(:login)
        login = attributes.delete(:login)
        record = find_record(login)
      else
        record = where(attributes).first
      end
    end

    unless record
      record = new

      required_attributes.each do |key|
        value = attributes[key]
        record.send("#{key}=", value)
        record.errors.add(key, value.present? ? error : :blank)
      end
    end
    record
  end

  # password not required on edit
  # see: https://github.com/plataformatec/devise/wiki/How-To:-Allow-users-to-edit-their-account-without-providing-a-password
  def password_required?
    new_record?
  end


# controllers/registrations_controller.rb
# devise controller for registration
class RegistrationsController < Devise::RegistrationsController

  # update_attributes (with final S) without providing password
  # overrides devise
  # see: https://github.com/plataformatec/devise/wiki/How-To:-Allow-users-to-edit-their-account-without-providing-a-password
  def update
    # Devise use update_with_password instead of update_attributes.
    # This is the only change we make.
    if resource.update_attributes(params[resource_name])
      set_flash_message :notice, :updated
      # Line below required if using Devise >= 1.2.0
      sign_in resource_name, resource, :bypass => true
      redirect_to after_update_path_for(resource)
    else
      clean_up_passwords(resource)
      render_with_scope :edit
    end
  end

end
#model/User.rb
#Desive调用此方法来检查模型的“活动”状态
def激活?
#记得打电话给超级警察
#然后使用我们自己的检查来确定“活动”状态
#我们自己的“活跃”专栏
超级和自我。你是活跃的吗?
结束
保护#====================================================================
#在db中查找用户名或电子邮件登录的用户
def self.find_记录(登录)
where(attributes).where([“name=:value或email=:value”,{:value=>login}])。首先
结束
#不允许使用区分大小写的电子邮件
#(保存的downcase,获取的downcase)
def self.find_用于_身份验证(条件)
条件[:电子邮件]。停机!
超级(条件)
结束
#通过用户名或电子邮件在数据库中查找用户
为数据库身份验证定义self.find_(条件)
login=条件。删除(:login)
where(条件)。where([“name=:value或email=:value”,{:value=>login}])。首先
结束
#尝试通过电子邮件查找用户。如果找到记录,则发送新记录
#它的密码说明。如果未找到用户,则返回新用户
#出现电子邮件未找到错误。
def self.send_reset_password_指令(属性={})
可恢复=查找\u可恢复\u或\u初始化\u时出错(重置\u密码\u键、属性,:未找到)
可恢复。如果可恢复,发送\u重置\u密码\u指令。是否持续?
可恢复
结束
def self.find_recoverable_或带有错误的_initialize_(必需的_属性,属性,错误=:无效)
不区分大小写的键。每个{k |属性[k]。请尝试(:downcase!))
属性=属性。切片(*必需的属性)
attributes.delete\u if{124; key,value | value.blank?}
如果attributes.size==必需的\u attributes.size
if attributes.has_key?(:登录)
login=属性。删除(:login)
记录=查找记录(登录)
其他的
记录=其中(属性)。首先
结束
结束
除非有记录
记录=新
必需的|属性。每个do |键|
值=属性[键]
record.send(“#{key}=”,value)
record.errors.add(key,value.present??error::blank)
结束
结束
记录
结束
#编辑时不需要密码
#见:https://github.com/plataformatec/devise/wiki/How-To:-允许用户在不提供密码的情况下编辑其帐户
是否需要def密码?
新纪录?
结束
#控制器/注册\u controller.rb
#设计控制员进行注册
类注册控制器<设计::注册控制器
#在不提供密码的情况下更新_属性(带有最后的S)
#设计
#见:https://github.com/plataformatec/devise/wiki/How-To:-允许用户在不提供密码的情况下编辑其帐户
def更新
#设计使用带有密码的update_,而不是update_属性。
#这是我们唯一的改变。
if resource.update_属性(参数[资源名称])
设置闪光信息:通知:更新
#如果使用Desive>=1.2.0,则需要下面的行
在资源中签名\u名称,资源::旁路=>true
将(资源)的路径重定向到更新后路径
其他的
清除密码(资源)
使用\u范围渲染\u:编辑
结束
结束
结束

谢谢你

好问题-我的观点可能是,只要它让事情变得更简单,它就有用。你总是可以在github上进行设计,并在某种程度上把你的定制放在那里——这就是我在一个项目上所做的。我对自己的认证也有点紧张,因为正确的认证非常重要,特别是如果其他人想看到他们不应该看到的东西。但我有兴趣看看其他人怎么想。

我暂时还是用Desive,你的变化不大。然而,我会将您所做的更改设计并提取为新功能。然后试着把他们拉进设计本身。这样维护它们不会落在你身上,而是落在很多人身上

维护一个完整的身份验证系统可能是一个真正令人头痛的问题,最终它会重新发明轮子。只要一个错误就可以让你敞开心扉

另外,您的新
查找\u以进行\u身份验证
方法,现在已在designe中得到支持,请输入designe初始值设定项

config.case_insensitive_keys = [ :email ]

+1非常感谢,如果你不使用gem附带的修改,那么更新gem的目的是什么?