What is the best way to create environment files for a Rails project?

I have some settings that I need in the Javascript file - the servers to connect - that change depending on my environment. For development

, test

and staging

I want to use staging servers; for production

, production servers. I already have settings in Ruby (configured in my files environment/xyz.rb

). So far, I have dynamically generated JS files at the time of request with a file application.js.erb

and a custom route. This is quite slow, but it means creating an additional controller and browsing the directory for that file only.

I would rather have a template file and a rake task that generates the correct version from the template and puts the static file in the directory public/javascripts

. Has anyone tried something like this? What did you use for rendering? Where did you put your template file and rendering code?

Or is it better to just keep the dynamic version and cache it during production?

+1


source to share


2 answers


Can you do something like this?

<% javascript_include_file "#{RAILS_ENV}.js" %>

      

I am doing something similar, although it is not with a JS file, but with some RB files ... and I am doing it for the same reason, determining which servers to connect to.



For me, I have an rb file in my directory "lib"

named "constants.rb"

. There are several if statements in this file that switch based on environment (development, test, staging, or production) that cause the ip values ​​to which I have to connect. This is difficult enough for my purposes on the behemoth of the machine that the application is running on.

Here's an example:

if ENV['RAILS_ENV'] != "production" ## if the rails environment is development or staging then use the test server
  @@IP_ADDRESS = "10.16.121.173" ## the test server
else ## if the Rails environment is production, then use the live server.
  @@IP_ADDRESS = "10.16.8.44" ## The is the shared IP address
end

      

+3


source


Using the rake command to create these javascript files is indeed an option. I am using rake tasks to generate i18n translation files for JS frontend from YAML translation files. One option is to use ERB for templates. I would put rakefile under lib/tasks/foo.rake

and templates under lib/tasks/foo/foo.erb

. If your JS template is not very complex, then I would suggest using a simple method Hash.to_json

to create javascript content:

namespace :js do
  task :generate_config => :environment do
    File.open("#{RAILS_ROOT}/public/javascripts/configuration.js", 'w') do |out|
      config = {:option_1 => 'Value 1', :option_2 => 'Value 2'}
      out << "var CONFIG = #{config.to_json}"
    end
  end
end

      

But config files are something you just don't want to forget to be restored when your original config data was changed. I am using a minified JS file generation solution in a production environment to add a huge amount of javascript files together so that I can save HTTP requests. But you can use basically the same solution. It works great if you are using Capistrano to deploy your application.

In the file, app/helpers/application_helper.rb

create a new helper method that is globally available:



module ApplicationHelper
  def javascript_include_config_tag
    if ActionController::Base.perform_caching
      unless File.exists?("#{RAILS_ROOT}/public/javascripts/configuration.js")

        #
        # TODO: Generate configuration data in here
        #
        config = 'configuration file contents'

        File.open("#{RAILS_ROOT}/public/javascripts/configuration.js", 'w') do |out|
          out << config
        end
      end
      javascript_include_tag('configuration')
    else
      # Link to js generated dynamically on each request. Useful in development.
      javascript_include_tag('PATH_TO_DYNAMICALLY_GENERATED_JS')
    end
  end
end

      

And in your opinion you are just including javascript with

<%= javascript_include_config_tag %>

      

The idea here is that this JS config file is only generated the first time the page is loaded after a fresh Capistrano deployment. This also has a huge disadvantage that you cannot request the file configuration.js

until the first page is generated, but in my application it works fine so far.

0


source







All Articles