AppConfig in heroku can now read array variables

This commit is contained in:
Maxwell Salzberg 2012-01-03 15:58:04 -08:00
parent a7d59ce115
commit 43090d38eb
4 changed files with 47 additions and 4 deletions

View file

@ -2,12 +2,14 @@
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.
require 'uri'
require File.join(Rails.root, 'lib', 'enviroment_configuration')
class AppConfig < Settingslogic
ARRAY_VARS = [:community_spotlight, :admins]
def self.source_file_name
config_file = File.join(Rails.root, "config", "application.yml")
if !File.exists?(config_file) && (Rails.env == 'test' || Rails.env.include?("integration") || ENV["HEROKU"])
if !File.exists?(config_file) && (Rails.env == 'test' || Rails.env.include?("integration") || EnviromentConfiguration.heroku?)
config_file = File.join(Rails.root, "config", "application.yml.example")
end
config_file
@ -117,10 +119,18 @@ HELP
def self.[] (key)
return self.pod_uri if key == :pod_uri
return ENV[key.to_s] if ENV[key.to_s] && ENV["HEROKU"]
return fetch_from_env(key.to_s) if EnviromentConfiguration.heroku?
super
end
def fetch_from_env(key)
if ARRAY_VARS.include?(key.to_sym)
ENV[key].split(EnviromentConfiguration::ARRAY_SEPERATOR)
else
ENV[key] if ENV[key]
end
end
def self.[]= (key, value)
super
if key.to_sym == :pod_url

View file

@ -1,4 +1,5 @@
module EnviromentConfiguration
ARRAY_SEPERATOR = '%|%'
def self.heroku?
ENV['HEROKU']
end

View file

@ -1,3 +1,9 @@
# Copyright (c) 2012, Diaspora Inc. This file is
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.
#
require File.join(Rails.root, 'lib', 'enviroment_configuration')
namespace :heroku do
HEROKU_CONFIG_ADD_COMMAND = "heroku config:add HEROKU=true"
@ -6,7 +12,11 @@ namespace :heroku do
application_config = YAML.load_file('config/application.yml')['production'] rescue {}
application_config.delete_if { |k, v| v.blank? }
heroku_env = application_config.map{|k, v| "#{k}=#{v}"}.join(' ')
heroku_env = application_config.map do|key, value|
value =value.join(EnviromentConfiguration::ARRAY_SEPERATOR) if value.respond_to?(:join)
"#{key}=#{value}"
end.join(' ')
puts "Generating and setting a new secret token"
token = ActiveSupport::SecureRandom.hex(40)#reloads secret token every time you reload vars.... this expires cookies, and kinda sucks

View file

@ -141,6 +141,28 @@ describe AppConfig do
end
end
context 'configurations which are arrays' do
it 'should be set to be admins or community_spotlight' do
AppConfig::ARRAY_VARS.should =~ [:community_spotlight, :admins]
end
context 'on heroku' do
before do
ENV['admins'] = "maxwell#{EnviromentConfiguration::ARRAY_SEPERATOR}daniel"
EnviromentConfiguration.stub(:heroku?).and_return(true)
end
after do
EnviromentConfiguration.stub(:heroku?).and_return(false)
end
it 'converts a string with ARRAY_SEPERATOR to an array' do
AppConfig[:admins].should be_a Array
end
end
end
describe ".pod_uri" do
it "properly parses the pod_url" do
AppConfig.pod_uri = nil