如何从守门员宝石中覆盖模型 [英] How to override model from the doorkeeper gem

查看:98
本文介绍了如何从守门员宝石中覆盖模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您是否知道如何覆盖Doorkeeper gem提供的Doorkeeper::Application.假设我要添加验证,回调等. db表被命名为auth_applications.

Do you have any idea how to override Doorkeeper::Application provided by the Doorkeeper gem.Let's say I want to add validation, callbacks and so on. Db table is named auth_applications.

我创建了一个名为application.rb的模型,其中包含以下内容,但我的before_create调用未触发.最好的方法是什么?

I created a model named application.rb containing the following but my before_create call is not triggered. What's the best approach?

module Doorkeeper
  class Application < ActiveRecord::Base
    include ApplicationMixin

    require 'identicon'
    before_create :generate_identicon

    def generate_identicon
      self.identicon = Identicon.data_url_for name, 128, [255, 255, 255]
    end

  end
end

按照此SO答案代码应声明作为初始值设定项.但是,我想拥有一个经典模型,因为有很多我想补充.

As per this SO answer code should be declared as an initializer. However I'd like to have a classic model since there is a lot I want to add.

推荐答案

这是我现在正在做的事情,无论如何仍在开发中,所以如果发现问题,我会进行更新.

This is how I'm doing right now, still developing anyway so I'll update if I'll find issues.

我正在使用ActiveRecord进行此操作,也许对于Mongoid/Mongomapper来说,需要做一些更改.

幸运地 Doorkeeper :: Application 具有用于设置正确表名的所有配置,因此您不必为此烦恼.

Luckily Doorkeeper::Application has all the configuration to set the correct table name so you don't have to bother about that.

考虑到这一点,您可以像这样添加app/models/application.rb:

With this in mind you can just add app/models/application.rb like this:

class Application < Doorkeeper::Application
  require 'identicon'
  before_create :generate_identicon

  def generate_identicon
    self.identicon = Identicon.data_url_for name, 128, [255, 255, 255]
  end
end

您完成了.

我正在使用它通过RailsAdmin自定义Doorkeepe :: Application(如果有人登陆这里,只是添加一些关键字)

I'm using this to customize Doorkeepe::Application with RailsAdmin (just to add some keyword if someone lands here)

这篇关于如何从守门员宝石中覆盖模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆