Doorkeeper: Add columns to Doorkeeper models?

Created on 21 Oct 2012  路  11Comments  路  Source: doorkeeper-gem/doorkeeper

Hello, I recently started exploring Doorkeeper. How do I build on the Doorkeeper models i.e.

1) Add say a logo and description columns to Applications
2) I want each application to define some more configuration e.g. 1 application has many process_maps. I want to add has_many :process_maps to Application model.

Is all this possible?

Most helpful comment

my solution

# config/initializers/doorkeeper_extend.rb
doorkeeper_extend_dir = File.join(Rails.root, "app", "models", "doorkeeper")
Dir.glob("#{doorkeeper_extend_dir}/*.rb").each { |f| require(f) }

# app/models/doorkeeper/application.rb
module Doorkeeper
  class Application < ActiveRecord::Base
    has_many :things
  end
end

All 11 comments

There's not best way to do this. You can easily add any field with active record migration and then monkey patch the Doorkeeper::Application model.

Another way to do that is to use ActiveSupport::Concern

module ApplicationExtension
  extend ActiveSupport::Concern

  included do
    has_many :things
  end
end

Doorkeeper::Application.send :include, ApplicationExtension

Of course this doesn't change that much, you still have to inject behaviour on the model.

For the next gem version, the application model would be moved out from doorkeeper so you'll be able to customize anything easily. It will be a similar approach of what devise does to the User model, for example.

Hope I answered your question. If you find out a better solution or have any more questions, please let me know.

Thank you :)

+1 for the modular version :)

@debreczeni it's currently working on branch 1.0, check it out

Hi, is there today any other way to add fields to application model more easily ?
I'm using mongoid and concerns doesn't appear to be working.

For anyone trying out the Rails concerns method, it looks like the line "Doorkeeper::Application.send :include, ApplicationExtension" isn't executed if you leave it at the end of the concern file. To make it work, I had to move it to a before_action ApplicationController method (probably not the better place for that - I don't know).

@dfabreguette-ap I would guess that you could just move it to an initializer?

@danschultzer Yes that's what I've done in the end, thank you !

Thanks.

For anyone still lost on this, this is how I got a logo column working on the OAuthApplication model:

  1. Add a column to oauth_applications table, for example rails g migration AddImageUrlToOauthApplications image_url:string and run rake db:migrate
  2. Generate the doorkeeper views so you can customize them, run in terminal rails generate doorkeeper:views this will copy the default Doorkeeper templates into your views folder so you can change them (see: https://github.com/doorkeeper-gem/doorkeeper/wiki/Customizing-views)
  3. Go to your doorkeeper/authorizations/new.html.erb template
  4. Now you can access your column with <%= @pre_auth.client.application.image_url %>
  5. Add the image tag <img class="client-logo" src="<%= @pre_auth.client.application.image_url %>" alt="" />

Of course you have to add an image url to the record in the database, I just added it directly to the DB column using Postico, I think if you want to include this new field in the new/edit templates of the application you'd have to add it as a permitted parameter, see: https://github.com/doorkeeper-gem/doorkeeper/issues/391

my solution

# config/initializers/doorkeeper_extend.rb
doorkeeper_extend_dir = File.join(Rails.root, "app", "models", "doorkeeper")
Dir.glob("#{doorkeeper_extend_dir}/*.rb").each { |f| require(f) }

# app/models/doorkeeper/application.rb
module Doorkeeper
  class Application < ActiveRecord::Base
    has_many :things
  end
end
Was this page helpful?
0 / 5 - 0 ratings

Related issues

amngibson picture amngibson  路  5Comments

eebasadre picture eebasadre  路  4Comments

ianbayne picture ianbayne  路  3Comments

rafaelsales picture rafaelsales  路  4Comments

beweinreich picture beweinreich  路  5Comments