config: make danbooru_local_config.rb optional.

Make it so that if danbooru_local_config.rb doesn't exist, we continue
with the default config instead of failing.
This commit is contained in:
evazion
2020-06-05 15:49:51 -05:00
parent 82c09858f2
commit aa9d34a3f0
2 changed files with 16 additions and 14 deletions

View File

@@ -14,10 +14,22 @@ require "rails/test_unit/railtie"
Bundler.require(*Rails.groups) Bundler.require(*Rails.groups)
require_relative "danbooru_default_config" begin
require_relative "danbooru_local_config" require_relative "danbooru_default_config"
require_relative "danbooru_local_config"
rescue LoadError
end
module Danbooru module Danbooru
mattr_accessor :config
# if danbooru_local_config exists then use it as the config, otherwise use danbooru_default_config.
if defined?(CustomConfiguration)
self.config = EnvironmentConfiguration.new(CustomConfiguration.new)
else
self.config = EnvironmentConfiguration.new(Configuration.new)
end
class Application < Rails::Application class Application < Rails::Application
# Use the responders controller from the responders gem # Use the responders controller from the responders gem
config.app_generators.scaffold_controller :responders_controller config.app_generators.scaffold_controller :responders_controller

View File

@@ -1,6 +1,4 @@
module Danbooru module Danbooru
module_function
class Configuration class Configuration
# A secret key used to encrypt session cookies, among other things. If this # A secret key used to encrypt session cookies, among other things. If this
# token is changed, existing login sessions will become invalid. If this # token is changed, existing login sessions will become invalid. If this
@@ -469,19 +467,11 @@ module Danbooru
end end
end end
class EnvironmentConfiguration EnvironmentConfiguration = Struct.new(:config) do
def custom_configuration
@custom_configuration ||= CustomConfiguration.new
end
def method_missing(method, *args) def method_missing(method, *args)
var = ENV["DANBOORU_#{method.to_s.upcase.chomp("?")}"] var = ENV["DANBOORU_#{method.to_s.upcase.chomp("?")}"]
var.presence || custom_configuration.send(method, *args) var.presence || config.send(method, *args)
end end
end end
def config
@config ||= EnvironmentConfiguration.new
end
end end