1

I have configuration data (the host/post for a Redis server) that is managed by a class (RedisConfig) in my RAILS_HOME/lib folder. We decided to use redis-store as our cache.

This is what we have in RAILS_HOME/config/environments/production.rb:

config.cache_store = RedisStore.new "#{RedisConfig.host}:#{RedisConfig.port}"

I, of course, get a TypeError as RedisConfig is loaded AFTER production.rb is loaded, so it never exists in this context. Whats the best strategy for loading and using this configuration manager in our environment.rb or environments/#{RAILS_ENV}.rb files?

Paul Gibler
  • 276
  • 3
  • 13

1 Answers1

1

Consider taking this approach. The trick here is they get loaded during the initialisation stage - it's the same as including it in as an initialiser itself.

See this SO post on loading up your RedisConfig module. While this is about engines, it's got some interesting details on initialisation.

Update

Here's a better solution - update your application.rb as follows

# Custom directories with classes and modules you want to be autoloadable.
# config.autoload_paths += %W(#{config.root}/extras)
config.autoload_paths += Dir["#{config.root}/lib/**/"]

This will autoload all modules stored in /lib.

Community
  • 1
  • 1
Michael De Silva
  • 3,808
  • 1
  • 20
  • 24