Permalink
Browse files

Add application config file.

  • Loading branch information...
1 parent 1c7be9d commit 238d2e55f3b9bc7bfb77d3e314875cf16a6fdb83 @FooBarWidget FooBarWidget committed Aug 16, 2011
Showing with 120 additions and 11 deletions.
  1. +3 −11 app/controllers/application_controller.rb
  2. +18 −0 config/application.rb
  3. +10 −0 config/application.yml.example
  4. +14 −0 config/database.yml.example
  5. +75 −0 lib/app_config.rb
@@ -1,25 +1,20 @@
class ApplicationController < ActionController::Base
protect_from_forgery
- around_filter :set_active_base_url
before_filter :authenticate_user!
private
### before filters
- def set_active_base_url
- Thread.current[:base_url] = @base_url = request.protocol + request.host_with_port
- yield
- ensure
- Thread.current[:base_url] = nil
- end
-
def require_admin!
if !current_user.admin?
render :template => 'shared/admin_required'
end
end
+
+ ### helpers
+
def save_return_to_url
session[:return_to] ||= begin
path = params[:return_to]
@@ -31,9 +26,6 @@ def save_return_to_url
end
end
-
- ### helpers
-
def redirect_back(default_url = nil)
redirect_to(session.delete(:return_to) || :back)
rescue RedirectBackError
View
@@ -1,7 +1,10 @@
+# -*- Mode: Ruby; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
+
require File.expand_path('../boot', __FILE__)
require 'rails/all'
require 'securerandom'
+require 'uri'
# If you have a Gemfile, require the default gems, the ones in the
# current environment and also include :assets gems if in development
@@ -10,6 +13,10 @@
module Juvia
class Application < Rails::Application
+ require File.expand_path('../../lib/app_config', __FILE__)
+
+ config.required_app_config = [:base_url]
+
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
@@ -40,5 +47,16 @@ class Application < Rails::Application
# Enable the asset pipeline
config.assets.enabled = true
+
+ initializer "action_mailer.default_url_options", :after => "app_config" do
+ uri = URI.parse(config.base_url)
+ config.action_mailer.default_url_options = { :host => uri.host }
+ if uri.scheme != "http"
+ config.action_mailer.default_url_options[:protocol] = uri.scheme
+ end
+ if !(uri.scheme == "http" && uri.port == 80) && !(uri.scheme == "https" && uri.port == 443)
+ config.action_mailer.default_url_options[:port] = uri.port
+ end
+ end
end
end
@@ -0,0 +1,10 @@
+development:
+ # The base URL on which you want to install Juvia, without trailing slash.
+ base_url: http://juvia.local
+
+test:
+ base_url: http://juvia.yoursite.com
+
+production:
+ base_url: http://juvia.yoursite.com
+
@@ -0,0 +1,14 @@
+development:
+ adapter: sqlite3
+ database: db/development.sqlite3
+
+test:
+ adapter: sqlite3
+ database: db/test.sqlite3
+
+production:
+ adapter: mysql
+ database: juvia
+ host: localhost
+ username:
+ password:
View
@@ -0,0 +1,75 @@
+# -*- Mode: Ruby; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
+
+module AppConfig
+ class Railtie < Rails::Railtie
+ initializer "app_config" do |app|
+ require 'yaml'
+ require 'ostruct'
+
+ Rails.extend(AppConfig::RailsExtensions)
+
+ config_file = "#{app.root}/config/application.yml"
+ config = YAML.load_file(config_file)
+
+ if !config[Rails.env]
+ abort "#{config_file} must contain configuration for the '#{Rails.env}' environment."
+ end
+
+ config = recursively_make_indifferent_access(config[Rails.env])
+ Rails.config = config
+ install_into_app_config(config, app.config)
+ if !check_requirements(nil, config, app.config.required_app_config)
+ abort "Please edit #{config_file} and set the aforementioned settings."
+ end
+ end
+
+ private
+
+ def recursively_make_indifferent_access(hash)
+ hash = hash.with_indifferent_access
+ hash.each_pair do |key, value|
+ if value.is_a?(Hash)
+ hash[key] = recursively_make_indifferent_access(value)
+ end
+ end
+ hash
+ end
+
+ def install_into_app_config(config, app_config)
+ config.each_pair do |key, value|
+ if value.is_a?(Hash)
+ sub_app_config = OpenStruct.new
+ app_config.send("#{key}=", sub_app_config)
+ install_into_app_config(value, sub_app_config)
+ else
+ app_config.send("#{key}=", value)
+ end
+ end
+ end
+
+ def check_requirements(namespace, config, requirements)
+ result = true
+ requirements.each do |key|
+ if key.is_a?(Hash)
+ key.each_pair do |subkey, subrequirements|
+ if false && config[subkey].blank?
+ STDERR.puts "The application setting '#{namespace}#{subkey}' must be set."
+ result = false
+ else
+ subresult = check_requirements("#{namespace}#{subkey}.", config[subkey] || {}, subrequirements)
+ result = result && subresult
+ end
+ end
+ elsif config[key].blank?
+ STDERR.puts "The application setting '#{namespace}#{key}' must be set."
+ result = false
+ end
+ end
+ result
+ end
+ end
+
+ module RailsExtensions
+ attr_accessor :config
+ end
+end

0 comments on commit 238d2e5

Please sign in to comment.