Skip to content

Commit

Permalink
Add application config file.
Browse files Browse the repository at this point in the history
  • Loading branch information
FooBarWidget committed Aug 16, 2011
1 parent 1c7be9d commit 238d2e5
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 11 deletions.
14 changes: 3 additions & 11 deletions app/controllers/application_controller.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,25 +1,20 @@
class ApplicationController < ActionController::Base class ApplicationController < ActionController::Base
protect_from_forgery protect_from_forgery


around_filter :set_active_base_url
before_filter :authenticate_user! before_filter :authenticate_user!


private private
### before filters ### 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! def require_admin!
if !current_user.admin? if !current_user.admin?
render :template => 'shared/admin_required' render :template => 'shared/admin_required'
end end
end end



### helpers

def save_return_to_url def save_return_to_url
session[:return_to] ||= begin session[:return_to] ||= begin
path = params[:return_to] path = params[:return_to]
Expand All @@ -31,9 +26,6 @@ def save_return_to_url
end end
end end



### helpers

def redirect_back(default_url = nil) def redirect_back(default_url = nil)
redirect_to(session.delete(:return_to) || :back) redirect_to(session.delete(:return_to) || :back)
rescue RedirectBackError rescue RedirectBackError
Expand Down
18 changes: 18 additions & 0 deletions config/application.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,7 +1,10 @@
# -*- Mode: Ruby; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-

require File.expand_path('../boot', __FILE__) require File.expand_path('../boot', __FILE__)


require 'rails/all' require 'rails/all'
require 'securerandom' require 'securerandom'
require 'uri'


# If you have a Gemfile, require the default gems, the ones in the # If you have a Gemfile, require the default gems, the ones in the
# current environment and also include :assets gems if in development # current environment and also include :assets gems if in development
Expand All @@ -10,6 +13,10 @@


module Juvia module Juvia
class Application < Rails::Application 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. # Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers # Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded. # -- all .rb files in that directory are automatically loaded.
Expand Down Expand Up @@ -40,5 +47,16 @@ class Application < Rails::Application


# Enable the asset pipeline # Enable the asset pipeline
config.assets.enabled = true 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
end end
10 changes: 10 additions & 0 deletions config/application.yml.example
Original file line number Original file line Diff line number Diff line change
@@ -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

14 changes: 14 additions & 0 deletions config/database.yml.example
Original file line number Original file line Diff line number Diff line change
@@ -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:
75 changes: 75 additions & 0 deletions lib/app_config.rb
Original file line number Original file line Diff line number Diff line change
@@ -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.