How to override model from the doorkeeper gem

2019-08-10 21:46发布

问题:

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.

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

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.

回答1:

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

I'm doing this using ActiveRecord, maybe for Mongoid/Mongomapper some changes need to be done.

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

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

And you're done.

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