Skip to content

Commit

Permalink
[ci skip] Fix specs, p2
Browse files Browse the repository at this point in the history
  • Loading branch information
nbulaj committed Jan 31, 2022
1 parent 4721b2a commit 4ca8a60
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 5 deletions.
4 changes: 4 additions & 0 deletions bin/console
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

require "bundler/setup"
require "rails/all"
require "active_support/all"
require "doorkeeper"

# You can add fixtures and/or initialization code here to make experimenting
Expand Down Expand Up @@ -31,5 +32,8 @@ ActiveRecord::Base.establish_connection(
# Load database schema
load File.expand_path("../spec/dummy/db/schema.rb", __dir__)

# Call engine #to_prepare block
Doorkeeper.setup

require "irb"
IRB.start(__FILE__)
13 changes: 12 additions & 1 deletion lib/doorkeeper/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ def configuration

alias config configuration

def setup_orm
def setup
setup_orm_adapter
run_orm_hooks
clear_config_cache

# Deprecated, will be removed soon
unless configuration.orm == :active_record
Expand All @@ -58,6 +59,16 @@ def setup_orm_adapter
ERROR_MSG
end

def clear_config_cache
%i[
application_model
access_token_model
access_grant_model
].each do |var|
config.remove_instance_variable("@#{var}") if config.instance_variable_defined?("@#{var}")
end
end

def run_orm_hooks
if @orm_adapter.respond_to?(:run_hooks)
@orm_adapter.run_hooks
Expand Down
2 changes: 1 addition & 1 deletion lib/doorkeeper/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Engine < Rails::Engine
end

config.to_prepare do
Doorkeeper.setup_orm
Doorkeeper.setup
end

if defined?(Sprockets) && Sprockets::VERSION.chr.to_i >= 4
Expand Down
14 changes: 13 additions & 1 deletion lib/doorkeeper/orm/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,19 @@ module ActiveRecord
autoload :StaleRecordsCleaner, "doorkeeper/orm/active_record/stale_records_cleaner"

def self.run_hooks
# nop
if (options = Doorkeeper.config.active_record_options[:establish_connection])
Doorkeeper::Orm::ActiveRecord.models.each do |model|
model.establish_connection(options)
end
end
end

def self.models
[
Doorkeeper.config.access_grant_model,
Doorkeeper.config.access_token_model,
Doorkeeper.config.application_model,
]
end
end
end
Expand Down
6 changes: 4 additions & 2 deletions spec/lib/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
it "adds specific error message to NameError exception" do
expect do
Doorkeeper.configure { orm "hibernate" }
Doorkeeper.setup_orm
Doorkeeper.setup
end.to raise_error(NameError, /ORM adapter not found \(hibernate\)/)
end

Expand All @@ -64,7 +64,7 @@

expect do
Doorkeeper.configure { orm "hibernate" }
Doorkeeper.setup_orm
Doorkeeper.setup
end.to raise_error(NoMethodError)
end
end
Expand Down Expand Up @@ -578,6 +578,8 @@ class FakeCustomModel; end
establish_connection: Rails.configuration.database_configuration[Rails.env],
)
end

Doorkeeper.setup
end
end

Expand Down

0 comments on commit 4ca8a60

Please sign in to comment.