diff --git a/app/models/app_config.rb b/app/models/app_config.rb index 409a58c64..152d6156b 100644 --- a/app/models/app_config.rb +++ b/app/models/app_config.rb @@ -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 diff --git a/lib/enviroment_configuration.rb b/lib/enviroment_configuration.rb index 7c0160277..6e35cad40 100644 --- a/lib/enviroment_configuration.rb +++ b/lib/enviroment_configuration.rb @@ -1,4 +1,5 @@ module EnviromentConfiguration + ARRAY_SEPERATOR = '%|%' def self.heroku? ENV['HEROKU'] end @@ -35,4 +36,4 @@ module EnviromentConfiguration AppConfig[:ca_file] end end -end \ No newline at end of file +end diff --git a/lib/tasks/heroku.rake b/lib/tasks/heroku.rake index d7d58e482..f6c34b9fd 100644 --- a/lib/tasks/heroku.rake +++ b/lib/tasks/heroku.rake @@ -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 diff --git a/spec/models/app_config_spec.rb b/spec/models/app_config_spec.rb index 044f5d84e..a993ca83c 100644 --- a/spec/models/app_config_spec.rb +++ b/spec/models/app_config_spec.rb @@ -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