AppConfig in heroku can now read array variables
This commit is contained in:
parent
a7d59ce115
commit
43090d38eb
4 changed files with 47 additions and 4 deletions
|
|
@ -2,12 +2,14 @@
|
||||||
# licensed under the Affero General Public License version 3 or later. See
|
# licensed under the Affero General Public License version 3 or later. See
|
||||||
# the COPYRIGHT file.
|
# the COPYRIGHT file.
|
||||||
require 'uri'
|
require 'uri'
|
||||||
|
require File.join(Rails.root, 'lib', 'enviroment_configuration')
|
||||||
|
|
||||||
class AppConfig < Settingslogic
|
class AppConfig < Settingslogic
|
||||||
|
ARRAY_VARS = [:community_spotlight, :admins]
|
||||||
|
|
||||||
def self.source_file_name
|
def self.source_file_name
|
||||||
config_file = File.join(Rails.root, "config", "application.yml")
|
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")
|
config_file = File.join(Rails.root, "config", "application.yml.example")
|
||||||
end
|
end
|
||||||
config_file
|
config_file
|
||||||
|
|
@ -117,10 +119,18 @@ HELP
|
||||||
|
|
||||||
def self.[] (key)
|
def self.[] (key)
|
||||||
return self.pod_uri if key == :pod_uri
|
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
|
super
|
||||||
end
|
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)
|
def self.[]= (key, value)
|
||||||
super
|
super
|
||||||
if key.to_sym == :pod_url
|
if key.to_sym == :pod_url
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
module EnviromentConfiguration
|
module EnviromentConfiguration
|
||||||
|
ARRAY_SEPERATOR = '%|%'
|
||||||
def self.heroku?
|
def self.heroku?
|
||||||
ENV['HEROKU']
|
ENV['HEROKU']
|
||||||
end
|
end
|
||||||
|
|
@ -35,4 +36,4 @@ module EnviromentConfiguration
|
||||||
AppConfig[:ca_file]
|
AppConfig[:ca_file]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -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
|
namespace :heroku do
|
||||||
HEROKU_CONFIG_ADD_COMMAND = "heroku config:add HEROKU=true"
|
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 = YAML.load_file('config/application.yml')['production'] rescue {}
|
||||||
application_config.delete_if { |k, v| v.blank? }
|
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"
|
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
|
token = ActiveSupport::SecureRandom.hex(40)#reloads secret token every time you reload vars.... this expires cookies, and kinda sucks
|
||||||
|
|
|
||||||
|
|
@ -141,6 +141,28 @@ describe AppConfig do
|
||||||
end
|
end
|
||||||
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
|
describe ".pod_uri" do
|
||||||
it "properly parses the pod_url" do
|
it "properly parses the pod_url" do
|
||||||
AppConfig.pod_uri = nil
|
AppConfig.pod_uri = nil
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue