Initializing object within devolopment.rb envirorm

2019-08-29 05:52发布

问题:

Completely lost.

I'm following this sample app tutorial https://www.wepay.com/developer/resources/wefarm-tutorial

which seems like a simple tutorial to follow except I'm building it inside a rails engine. I'm currently attempting to follow the tutorial and initialize the new object.

initialize a new WePay object. add these variables to config/development.rb:

wefarm / config / environments / development.rb

App specific information

CLIENT_ID = 32636
CLIENT_SECRET = "180c800c62"
USE_STAGE = true
WEPAY = WePay.new(CLIENT_ID, CLIENT_SECRET, USE_STAGE)

The issue I think I'm having is the gem is within my core engine allong with user's and the rest of the application and I'm adding these lines of code into the empty shell app. How would I make sure my engine uses this in development I'm also assuming I will come across this issue again when I'm set for production.

In another question a user specified I'm putting this code into the wrong area if I'm using an engine it should be in the initializer folder, but within the documents they just specify putting the code within the config/environments file so where/how exactly do I translate this over to an engine. If it goes into the initializer folder how would I make that file to just include the code specified?

Any help would be amazingly helpful.

Ps. The client Id and secret our just test information

回答1:

In Rails 4 engine, define the app specific information in secrets.yml

  1. Specifying secrete keys and ids according to environment will solve environment related configuration for keys. Example : development:

    CLIENT_ID: 123

  2. Make engine configurable

Example in config/initializer/wefarm.rb

Weform.CLIENT_ID =  Rails.application.secrets.CLIENT_ID 
  1. In lib/engine.rb (engine)

    mattr_accessor :CLIENT_ID

Now you can access client_id in engine as WeForm.CLIENT_ID

For more information Rails Engine