module Doorkeeper

def self.authenticate(request, methods = Doorkeeper.configuration.access_token_methods)

def self.authenticate(request, methods = Doorkeeper.configuration.access_token_methods)
  OAuth::Token.authenticate(request, *methods)
end

def self.check_for_missing_columns

def self.check_for_missing_columns
  if Doorkeeper.configuration.orm == :active_record &&
      ActiveRecord::Base.connected? &&
      ActiveRecord::Base.connection.table_exists?(
        Doorkeeper::Application.table_name
      ) &&
      !Doorkeeper::Application.new.attributes.include?("scopes")
    puts <<-MSG.squish
oorkeeper] Missing column: `oauth_applications.scopes`.
 you are using ActiveRecord run `rails generate doorkeeper:application_scopes
 rake db:migrate` to add it.
    MSG
  end
end

def self.configuration

def self.configuration
  @config || (fail MissingConfiguration.new)
end

def self.configure(&block)

def self.configure(&block)
  @config = Config::Builder.new(&block).build
  enable_orm
  check_for_missing_columns
  setup_application_owner if @config.enable_application_owner?
end

def self.configured?

def self.configured?
  @config.present?
end

def self.database_installed?

def self.database_installed?
  [AccessToken, AccessGrant, Application].all? { |model| model.table_exists? }
end

def self.enable_orm

def self.enable_orm
  class_name = "doorkeeper/orm/#{configuration.orm}".classify
  class_name.constantize.initialize_models!
rescue NameError => e
  if e.instance_of?(NameError)
    fail e, "ORM adapter not found (#{configuration.orm})", <<-error_msg
oorkeeper] ORM adapter not found (#{configuration.orm}), or there was an error
ying to load it.
u probably need to add the related gem for this adapter to work with
orkeeper.
 you are working on the adapter itself, double check that the constant exists,
d that your `initialize_models!` method doesn't raise any errors.\n
    error_msg
  else
    raise e
  end
end

def self.installed?

def self.installed?
  configured? && database_installed?
end

def self.setup_application_owner

def self.setup_application_owner
  require File.join(File.dirname(__FILE__), 'models', 'concerns', 'ownership')
  Application.send :include, Models::Ownership
end