lib/doorkeeper.rb in doorkeeper-5.6.2 vs lib/doorkeeper.rb in doorkeeper-5.6.3

- old
+ new

@@ -88,10 +88,11 @@ module Models autoload :Accessible, "doorkeeper/models/concerns/accessible" autoload :Expirable, "doorkeeper/models/concerns/expirable" autoload :ExpirationTimeSqlMath, "doorkeeper/models/concerns/expiration_time_sql_math" autoload :Orderable, "doorkeeper/models/concerns/orderable" + autoload :PolymorphicResourceOwner, "doorkeeper/models/concerns/polymorphic_resource_owner" autoload :Scopes, "doorkeeper/models/concerns/scopes" autoload :Reusable, "doorkeeper/models/concerns/reusable" autoload :ResourceOwnerable, "doorkeeper/models/concerns/resource_ownerable" autoload :Revocable, "doorkeeper/models/concerns/revocable" autoload :SecretStorable, "doorkeeper/models/concerns/secret_storable" @@ -111,13 +112,79 @@ autoload :Plain, "doorkeeper/secret_storing/plain" autoload :Sha256Hash, "doorkeeper/secret_storing/sha256_hash" autoload :BCrypt, "doorkeeper/secret_storing/bcrypt" end - def self.authenticate(request, methods = Doorkeeper.config.access_token_methods) - OAuth::Token.authenticate(request, *methods) - end + class << self + attr_reader :orm_adapter - def self.gem_version - ::Gem::Version.new(::Doorkeeper::VERSION::STRING) + def configure(&block) + @config = Config::Builder.new(&block).build + setup + @config + end + + # @return [Doorkeeper::Config] configuration instance + # + def configuration + @config || configure + end + + def configured? + !@config.nil? + end + + alias config configuration + + def setup + setup_orm_adapter + run_orm_hooks + config.clear_cache! + + # Deprecated, will be removed soon + unless configuration.orm == :active_record + setup_orm_models + setup_application_owner + end + end + + def setup_orm_adapter + @orm_adapter = "doorkeeper/orm/#{configuration.orm}".classify.constantize + rescue NameError => e + raise e, "ORM adapter not found (#{configuration.orm})", <<-ERROR_MSG.strip_heredoc + [DOORKEEPER] ORM adapter not found (#{configuration.orm}), or there was an error + trying to load it. + + You probably need to add the related gem for this adapter to work with + doorkeeper. + ERROR_MSG + end + + def run_orm_hooks + if @orm_adapter.respond_to?(:run_hooks) + @orm_adapter.run_hooks + else + ::Kernel.warn <<~MSG.strip_heredoc + [DOORKEEPER] ORM "#{configuration.orm}" should move all it's setup logic under `#run_hooks` method for + the #{@orm_adapter.name}. Later versions of Doorkeeper will no longer support `setup_orm_models` and + `setup_application_owner` API. + MSG + end + end + + def setup_orm_models + @orm_adapter.initialize_models! + end + + def setup_application_owner + @orm_adapter.initialize_application_owner! + end + + def authenticate(request, methods = Doorkeeper.config.access_token_methods) + OAuth::Token.authenticate(request, *methods) + end + + def gem_version + ::Gem::Version.new(::Doorkeeper::VERSION::STRING) + end end end