Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Write a little middleware if you want to keep the knowledge of the per-country configuration out of the main application.</p> <p>A middleware is extremely simple. A do-nothing middleware looks like this:</p> <pre><code>class DoesNothing def initialize(app, *args) @app = app end def call(env) @app.call(env) end end </code></pre> <p>Rack powers applications through chaining a series of middlewares together... each one is given a reference to @app, which is the next link in the chain, and it must invoke <code>#call</code> on that application. The one at the end of the chain runs the app.</p> <p>So in your case, you can do some additional configuration in here.</p> <pre><code>class PerCountryConfiguration def initialize(app) @app = app end def call(env) case env["COUNTRY"] when "AU" Rails.application.config.s3_buckets = { ... } when "US" Rails.application.config.s3_buckets = { ... } ... etc end @app.call(env) end end </code></pre> <p>There are several ways to use the middleware, but since it depends on access to the Rails environment, you'll want to do it from inside Rails. Put it in your application.rb:</p> <pre><code>config.middleware.use PerCountryConfiguration </code></pre> <p>If you want to pass additional arguments to the constructor of your middleware, just list them after the class name:</p> <pre><code>config.middleware.use PerCountryConfiguration, :some_argument </code></pre> <p>You can also mount the middleware from inside of ApplicationController, which means all of the initializers and everything will have already been executed, so it may be too far along the chain.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload