Replies: 3 comments
-
|
Hey! config = MyConfig.new # loads data from wherever
config_env = config.to_env #=> { 'MY_CONFIG_X' => ... }
ENV.merge!(config_env)WDYT? We can add |
Beta Was this translation helpful? Give feedback.
-
|
Thank you for entertaining this idea. Here is the module I've written so far. # frozen_string_literal: true
module EnvPromotable
extend ActiveSupport::Concern
included do
cattr_accessor :promoted_attrs
self.promoted_attrs = []
end
class_methods do
def promote_to_env(*attrs)
self.promoted_attrs = attrs
attrs_to_env
end
def attrs_to_env
promoted_attrs.each do |attr|
value = send(attr)
key = [env_prefix,attr].join("_").upcase
ENV[key] = value.to_s unless value.nil?
end
end
end
end
class GoogleConfig < ApplicationConfig
include EnvPromotable
attr_config :account_type, :client_id, :client_email, :private_key
promote_to_env :client_id, :private_key
end
I'd like to only promote some of the keys, but it's not really a big deal. The obstacle with this approach is initialization and potentially reloading. The attribute is not promoted until the config/class is loaded which doesn't work without eager loading. Is there a way to force eager loading of config classes in the gem? I can write an initializer or block in application.rb, not sure which is best, but was hoping to have it all contained in a module. W.R.T. reloading I want to make sure setting the attr and a new instance/reload would update the ENV. These two aren't strictly my use case but it makes sense to have them in sync, although could get weird working with multiple instances for testing or wonky use-cases. Plus ENV reloads are understood a restart is required so maybe reloading isn't good idea? I tried to use a proc for the ENV value but it didnt' seem to like that (e.g. |
Beta Was this translation helpful? Give feedback.
-
|
This is the solution I'm running with for now. module EnvPromotable
extend ActiveSupport::Concern
included do
cattr_accessor :promoted_attrs
self.promoted_attrs = []
def attrs_to_env
puts "Promoting #{promoted_attrs}"
promoted_attrs.each do |attr|
value = send(attr)
key = [env_prefix,attr].join("_").upcase
ENV[key] = value.to_s unless value.nil?
end
end
on_load :attrs_to_env
end
class_methods do
def promote_to_env(*attrs)
self.promoted_attrs = attrs
attrs_to_env
end
end
endIt works with rspec but in order for envs to be populated in a fresh rails console I added this to unless Rails.application.config.eager_load
ActiveSupport::Reloader.to_prepare do
[
"config/configs/*.rb",
...
].each do |eager_with_reload_directory|
Dir[eager_with_reload_directory].each do |f|
require_dependency("#{Dir.pwd}/#{f}")
rescue StandardError => e
puts e.backtrace.take(10)
puts "\nError requiring local file\n\ #{f}\n#{e}\n\n"
nil
end
end
end
endNot pretty but we already had this block for some other STI classes and it does seem to work. It also runs all validations when starting the console so you know if things are wonky before using them. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I have a gem that depends on some things set in ENV. For local development I want them to come from secrets.yml, but I have to set ENV.
It kinda feels like adding everything in the configuration to ENV would be useful, but heavy handed. I've considered adding an
on_loadto myApplicationConfigbut would want to expose a macro for which things to expose. Also want to add them to env in the way thatAnywayConfigwould respect (e.g. env_prefix, underscore rules) but havn't found a helper method for generating them based on config_attributes.This is what I'd like to see and will probably write for my app, but figured I'd ask in case I missed a feature that already exists.
Beta Was this translation helpful? Give feedback.
All reactions