Fixed multi coop routing and changed config accessors.

This commit is contained in:
benni 2012-08-24 19:52:38 +02:00
parent 2860a3ca44
commit ec2e761e7f
28 changed files with 124 additions and 88 deletions

42
lib/foodsoft_config.rb Normal file
View file

@ -0,0 +1,42 @@
class FoodsoftConfig
mattr_accessor :scope, :config
APP_CONFIG = YAML.load(File.read(File.join(Rails.root, "/config/app_config.yml")))
class << self
def init
# Load initial config from development or production
set_config Rails.env
# Overwrite scope to have a better namescope than 'production'
self.scope = config[:default_scope] or raise "No default_scope is set"
end
# Set config and database connection for specific foodcoop
# Only needed in multi coop mode
def select_foodcoop(foodcoop)
set_config foodcoop
setup_database
end
# Provides a nice accessor for config values
# FoodsoftConfig[:name] # => 'FC Test'
def [](key)
config[key]
end
private
def set_config(foodcoop)
raise "No config for this environment (#{foodcoop}) available!" if APP_CONFIG[foodcoop].nil?
self.config = APP_CONFIG[foodcoop].symbolize_keys
self.scope = foodcoop
end
def setup_database
database_config = ActiveRecord::Base.configurations[Rails.env]
database_config.merge!(config[:database]) if config[:database].present?
ActiveRecord::Base.establish_connection(database_config)
end
end
end

View file

@ -3,8 +3,8 @@ class Wikilink < WikiCloth::WikiLinkHandler
def link_attributes_for(page)
permalink = Page.permalink(page)
url_options = {:host => Foodsoft.config[:host], :protocol => Foodsoft.config[:protocol]}
url_options.merge!({:port => Foodsoft.config[:port]}) if Foodsoft.config[:port]
url_options = {:host => FoodsoftConfig[:host], :protocol => FoodsoftConfig[:protocol]}
url_options.merge!({:port => FoodsoftConfig[:port]}) if FoodsoftConfig[:port]
if Page.exists?(:permalink => permalink)
{ :href => url_for(url_options.merge({:controller => "pages", :action => "show",