Where to place my environment dependent gem config in Rails 4?

Hommer Smith

I am using a gem (RTURK) and right now I have the following code on the class that wraps the gem:

if Rails.env.production?
  RTurk.setup(ENV['AMAZON_KEY_ID'], ENV['AMAZON_KEY'], :sandbox => false)
else
  RTurk.setup(ENV['AMAZON_KEY_ID'], ENV['AMAZON_KEY'], :sandbox => true)
end

I want to move that to a configuration/environments/production and configuration/environments/development and test

But I am unsure at which point I should place the configuration of the gem. Does it need to be in a config.after_initialize?

Jay Mitchell

The environment files are loaded before any initializers (see Rails Initalization Guide). This means that you can set a configuration variable in your environment file, and then use it in an initializer.

In your case, it looks like the only thing that differs is whether or not RTurk will be sandboxed. You can add config.rturk_sandbox = true to application.rb, and config.rturk_sandbox = false to production.rb file. Then create config/initializers/rturk.rb, which uses that value.

RTurk.setup(ENV['AMAZON_KEY_ID'], ENV['AMAZON_KEY'], :sandbox => YourApp::Application.config.rturk_sandbox)

(I'm suggesting adding config.rturk_sandbox = true to application.rb so that it does the safe thing if you forget to set that in an individual environment file.)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Rails 4: where to place Struct?

From Dev

Rails 4: where to place Struct?

From Dev

Where to place Grape custom validations in Rails 4

From Dev

Rails 4 - best_in_place gem and many-to-many association

From Dev

Rails 4 - best_in_place gem and many-to-many association

From Dev

Where should I place a config.txt in my python project

From Dev

Angular, Bootstrap 4, angular-cli - where to place dependent scss and import it?

From Dev

How to use the rails config gem?

From Dev

What may be preventing in-place editing in my rails 4 app?

From Dev

Rails 4 Auditing Gem

From Dev

Are there similar gem for Rails 4?

From Dev

Rails 4: Masonry gem

From Dev

best_in_place gem checkbox issue with Rails 4 (why is string not converted to boolean?)

From Dev

Rails 4 best_in_place gem display issues with iterating through list

From Dev

can I get a data from file only one time in my gem similar to config/initializers in rails?

From Dev

can I get a data from file only one time in my gem similar to config/initializers in rails?

From Dev

best place to configure a global setting for a gem Rails

From Dev

Rails best_in_place gem not updating data

From Dev

Rails best_in_place gem not updating data

From Dev

Environment variables cached in Rails config?

From Dev

Where to place generic methods in Rails?

From Dev

Where to place my button for intent?

From Dev

Asset pipeline Configuration for Rails 4. Can someone explain why my Rails 4 has no sprocket-rails gem?

From Dev

Where to place connection string in web.config

From Dev

How does ruby gem works in a rails environment

From Dev

Use gem as library in Rails 4

From Dev

Where should I put my gem in my developing gem?

From Dev

Rails 4 - Displaying a dependent model in a table

From Dev

Where Are My Config Files In Nagios?

Related Related

  1. 1

    Rails 4: where to place Struct?

  2. 2

    Rails 4: where to place Struct?

  3. 3

    Where to place Grape custom validations in Rails 4

  4. 4

    Rails 4 - best_in_place gem and many-to-many association

  5. 5

    Rails 4 - best_in_place gem and many-to-many association

  6. 6

    Where should I place a config.txt in my python project

  7. 7

    Angular, Bootstrap 4, angular-cli - where to place dependent scss and import it?

  8. 8

    How to use the rails config gem?

  9. 9

    What may be preventing in-place editing in my rails 4 app?

  10. 10

    Rails 4 Auditing Gem

  11. 11

    Are there similar gem for Rails 4?

  12. 12

    Rails 4: Masonry gem

  13. 13

    best_in_place gem checkbox issue with Rails 4 (why is string not converted to boolean?)

  14. 14

    Rails 4 best_in_place gem display issues with iterating through list

  15. 15

    can I get a data from file only one time in my gem similar to config/initializers in rails?

  16. 16

    can I get a data from file only one time in my gem similar to config/initializers in rails?

  17. 17

    best place to configure a global setting for a gem Rails

  18. 18

    Rails best_in_place gem not updating data

  19. 19

    Rails best_in_place gem not updating data

  20. 20

    Environment variables cached in Rails config?

  21. 21

    Where to place generic methods in Rails?

  22. 22

    Where to place my button for intent?

  23. 23

    Asset pipeline Configuration for Rails 4. Can someone explain why my Rails 4 has no sprocket-rails gem?

  24. 24

    Where to place connection string in web.config

  25. 25

    How does ruby gem works in a rails environment

  26. 26

    Use gem as library in Rails 4

  27. 27

    Where should I put my gem in my developing gem?

  28. 28

    Rails 4 - Displaying a dependent model in a table

  29. 29

    Where Are My Config Files In Nagios?

HotTag

Archive