Initializer doesn't seem to be loading

282 views Asked by At

I have an API wrapper with the following code:

module MyApi
  mattr_accessor :app_id
end

I'm trying to set the value of app_id in an initializer like this:

# config/initializers/my_api.rb

MyApi.app_id = Rails.application.secrets.my_api["app_id"]

In my secrets file I have:

# secrets.yml

development:
  my_api:
    app_id: foo

But when I open my console or run tests I get this:

master ✗ $ rails c
Loading development environment (Rails 4.1.4)
2.2.0 :001 > MyApi.app_id
 => nil
2.2.0 :002 > MyApi.app_id = Rails.application.secrets.my_api["app_id"]
 => "foo"
2.2.0 :003 > MyApi.app_id
 => "foo"

I've followed gem readme's about using initializers but have never implemented one myself. Is there something I'm missing here? Using Rails 4.1.4.

1

There are 1 answers

0
RustComet On

Try adding this inside your module:

module MyApi

  ### Your code here


  private

  def self.setup
    yield self
  end
end

And in the initializer change it to have a setup block:

MyApi.setup do |config|
  config.app_id = Rails.application.secrets.my_api["app_id"]
end

I have only built a few gems with initializers, but this worked for me.