Commit 7014a01daf00f84d0c03c4df423a76591c52919b

Authored by Nathan Broadbent
1 parent ea2965aa
Exists in master and in 1 other branch production

Allow a Rails engine to override config by defining it earlier.

app/models/user.rb
... ... @@ -3,15 +3,13 @@ class User
3 3 include Mongoid::Document
4 4 include Mongoid::Timestamps
5 5  
6   - devise :database_authenticatable,
7   - :recoverable, :rememberable, :trackable,
8   - :validatable, :token_authenticatable
  6 + devise *Errbit::Config.devise_modules
9 7  
10 8 field :email
11 9 field :name
12 10 field :admin, :type => Boolean, :default => false
13 11 field :per_page, :type => Fixnum, :default => PER_PAGE
14   - field :time_zone, :default => "UTC"
  12 + field :time_zone, :default => "UTC"
15 13  
16 14 after_destroy :destroy_watchers
17 15 before_save :ensure_authentication_token
... ...
config/initializers/_load_config.rb
1 1 require 'ostruct'
  2 +default_config_file = Rails.root.join("config", "config.example.yml")
2 3  
3   -Errbit::Config = OpenStruct.new
4   -
5   -# If Errbit is running on Heroku, config can be set from environment variables.
6   -if ENV['HEROKU']
7   - Errbit::Config.host = ENV['ERRBIT_HOST']
8   - Errbit::Config.email_from = ENV['ERRBIT_EMAIL_FROM']
9   - Errbit::Config.email_at_notices = [1,3,10] #ENV['ERRBIT_EMAIL_AT_NOTICES']
10   - Errbit::Config.confirm_resolve_err = ENV['ERRBIT_CONFIRM_RESOLVE_ERR']
11   - Errbit::Config.user_has_username = ENV['ERRBIT_USER_HAS_USERNAME']
12   - Errbit::Config.allow_comments_with_issue_tracker = ENV['ERRBIT_ALLOW_COMMENTS_WITH_ISSUE_TRACKER']
13   -
14   - Errbit::Config.smtp_settings = {
15   - :address => "smtp.sendgrid.net",
16   - :port => "25",
17   - :authentication => :plain,
18   - :user_name => ENV['SENDGRID_USERNAME'],
19   - :password => ENV['SENDGRID_PASSWORD'],
20   - :domain => ENV['SENDGRID_DOMAIN']
21   - }
22   -end
  4 +# Allow a Rails Engine to override config by defining it earlier
  5 +unless defined?(Errbit::Config)
  6 + Errbit::Config = OpenStruct.new
23 7  
24   -# Use example config for test environment.
25   -default_config_file = Rails.root.join("config", "config.example.yml")
26   -config_file = Rails.env == "test" ? default_config_file : Rails.root.join("config", "config.yml")
27   -
28   -# Load config if config file exists.
29   -if File.exists?(config_file)
30   - config = YAML.load_file(config_file)
31   - config.merge!(config.delete(Rails.env)) if config.has_key?(Rails.env)
32   - config.each do |k,v|
33   - Errbit::Config.send("#{k}=", v)
  8 + # If Errbit is running on Heroku, config can be set from environment variables.
  9 + if ENV['HEROKU']
  10 + Errbit::Config.host = ENV['ERRBIT_HOST']
  11 + Errbit::Config.email_from = ENV['ERRBIT_EMAIL_FROM']
  12 + Errbit::Config.email_at_notices = [1,3,10] #ENV['ERRBIT_EMAIL_AT_NOTICES']
  13 + Errbit::Config.confirm_resolve_err = ENV['ERRBIT_CONFIRM_RESOLVE_ERR']
  14 + Errbit::Config.user_has_username = ENV['ERRBIT_USER_HAS_USERNAME']
  15 + Errbit::Config.allow_comments_with_issue_tracker = ENV['ERRBIT_ALLOW_COMMENTS_WITH_ISSUE_TRACKER']
  16 +
  17 + Errbit::Config.smtp_settings = {
  18 + :address => "smtp.sendgrid.net",
  19 + :port => "25",
  20 + :authentication => :plain,
  21 + :user_name => ENV['SENDGRID_USERNAME'],
  22 + :password => ENV['SENDGRID_PASSWORD'],
  23 + :domain => ENV['SENDGRID_DOMAIN']
  24 + }
34 25 end
35   -# Raise an error if we are not running tests, not running on Heroku, and config.yml doesn't exist.
36   -elsif not ENV['HEROKU']
37   - raise "Please copy 'config/config.example.yml' to 'config/config.yml' and configure your settings."
  26 +
  27 + # Use example config for test environment.
  28 + config_file = Rails.env == "test" ? default_config_file : Rails.root.join("config", "config.yml")
  29 +
  30 + # Load config if config file exists.
  31 + if File.exists?(config_file)
  32 + config = YAML.load_file(config_file)
  33 + config.merge!(config.delete(Rails.env)) if config.has_key?(Rails.env)
  34 + config.each do |k,v|
  35 + Errbit::Config.send("#{k}=", v)
  36 + end
  37 + # Raise an error if we are not running tests, not running on Heroku, and config.yml doesn't exist.
  38 + elsif not ENV['HEROKU']
  39 + raise "Please copy 'config/config.example.yml' to 'config/config.yml' and configure your settings."
  40 + end
  41 +
  42 + # Set default devise modules
  43 + Errbit::Config.devise_modules = [:database_authenticatable,
  44 + :recoverable, :rememberable, :trackable,
  45 + :validatable, :token_authenticatable]
38 46 end
39 47  
40 48 # Set default settings from config.example.yml if key is missing from config.yml
... ...