Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configure session_store.rb to handle staging and production?

I have a staging and a production environment on my rails 3.1rc6 app which uses subdomains. I've bought and configured different domain names for these environments, because the default something-something.herokuapp.com doesn't play nicely with subdomains.

When I set session_store.rb to this for one environment, everything works fine:

AppName::Application.config.session_store :cookie_store, :key => '_sample_app_session' , :domain => '.mystagingdomain.co.uk' 

But I can't seem to add in a conditional to allow for the environment-specific domain names.

I've tried

AppName::Application.config.session_store :cookie_store, :key => '_sample_app_session' , :domain => '.mystagingdomain.co.uk' if Rails.env.staging?
AppName::Application.config.session_store :cookie_store, :key => '_sample_app_session' , :domain => '.myproductiondomain.com' if Rails.env.production?

which doesn't work.

like image 848
snowangel Avatar asked Aug 25 '11 08:08

snowangel


2 Answers

The following settings has been working fine for me:

config/environments/staging.rb

AppName::Application.config.session_store :cookie_store, :key => '_sample_app_session' , :domain => '.mystagingdomain.co.uk'

config/environments/production.rb

AppName::Application.config.session_store :cookie_store, :key => '_sample_app_session' , :domain => '.myproductiondomain.com'
like image 129
Arun Kumar Arjunan Avatar answered Nov 20 '22 17:11

Arun Kumar Arjunan


You can use the :domain => :all option. You can also provide a :tld_length, if different than 1.

AppName::Application.config.session_store :cookie_store, :key => '_sample_app_session' , :domain => :all

Here's the relevant Rails code

def handle_options(options) #:nodoc:
  options[:path] ||= "/"

  if options[:domain] == :all
    # if there is a provided tld length then we use it otherwise default domain regexp
    domain_regexp = options[:tld_length] ? /([^.]+\.?){#{options[:tld_length]}}$/ : DOMAIN_REGEXP

    # if host is not ip and matches domain regexp
    # (ip confirms to domain regexp so we explicitly check for ip)
    options[:domain] = if (@host !~ /^[\d.]+$/) && (@host =~ domain_regexp)
      ".#{$&}"
    end
  elsif options[:domain].is_a? Array
    # if host matches one of the supplied domains without a dot in front of it
    options[:domain] = options[:domain].find {|domain| @host.include? domain[/^\.?(.*)$/, 1] }
  end
end

Otherwise, you should also be able to override the settings in the config/environments/ENVIRONMENT.rb file on a per-environment basis.

like image 7
Simone Carletti Avatar answered Nov 20 '22 16:11

Simone Carletti