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

require 'rails/all'

if defined?(Bundler)
  # If you precompile assets before deploying to production, use this line
  Bundler.require *Rails.groups(:assets => %w(development test))
  Bundler.require(:default, :assets) if ENV['COMPILING_ASSETS']
  # If you want your assets lazily compiled in production, use this line
  # Bundler.require(:default, :assets, Rails.env)
end

module Jobsworth
  class Application < Rails::Application

    # Load jobsworth specific configurations
    application_config_file = Rails.root.join 'config', 'application.yml'
    config.jobsworth = if File.exists? application_config_file
      Hashie::Mash.new YAML.load(ERB.new(File.read(application_config_file)).result)[Rails.env]
    else
      Hashie::Mash.new
    end

    config.action_mailer.smtp_settings = config.jobsworth.smtp.try(:to_hash, symbolize_keys: true )

    if config.jobsworth.exception_notifier
      config.middleware.use ExceptionNotification::Rack, config.jobsworth.exception_notifier.try(:to_hash, symbolize_keys: true )
    end

    # 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.

    # Add additional load paths for your own custom dirs
    # config.load_paths += %W( #{config.root}/extras )

    # Only load the plugins named here, in the order given (default is alphabetical).
    # :all can be used as a placeholder for all plugins not explicitly named
    # config.plugins = [ :exception_notification, :ssl_requirement, :all ]

    # Activate observers that should always be running
    # config.active_record.observers = :cacher, :garbage_collector

    # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
    # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
    # config.time_zone = 'Central Time (US & Canada)'

    # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
    # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
    # config.i18n.default_locale = :de

    # Configure generators values. Many other options are available, be sure to check the documentation.
    # config.generators do |g|
    #   g.orm             :active_record
    #   g.template_engine :erb
    #   g.test_framework  :test_unit, :fixture => true
    # end
    config.generators do |g|
      g.fixture_replacement :factory_girl
    end

    config.assets.initialize_on_precompile = false

    # Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added)
    config.assets.precompile += %w(excanvas.js)

    # Configure the default encoding used in templates for Ruby 1.9.
    config.encoding = 'utf-8'

    config.assets.enabled = true

    # Configure sensitive parameters which will be filtered from the log file.
    config.filter_parameters += [:password]

    # This code is for forcing spork to use 'load' when requiring model classes
    # this avoids a sort of 'cache effect'
    if Rails.env.test?
      initializer :after => :initialize_dependency_mechanism do
        ActiveSupport::Dependencies.mechanism = :load
      end
    end

    # https://github.com/rails/rails/issues/13164
    config.i18n.enforce_available_locales = false
  end
end
