lib/ecrire/application.rb in ecrire-0.31.1 vs lib/ecrire/application.rb in ecrire-0.31.2

- old
+ new

@@ -3,10 +3,11 @@ require 's3' require 'kaminari' require 'observejs' require 'written' require 'pg_search' +require 'autoprefixer-rails' module Ecrire ## # Ecrire::Application is the entry point when running # a blog. @@ -21,20 +22,12 @@ # If the application finds +secrets.yml+, it will load the Theme which is located # in the current working directory. # # class Application < Rails::Application - require 'ecrire/config/environment' + attr_writer :pwd - config.before_initialize do - if paths['config/database'].existent.any? - require 'ecrire/theme/engine' - else - require 'ecrire/onboarding/engine' - end - end - ## # There seems to be a crack between when the configuration # is loaded and when the initializer collection is built. # # Ecrire requires the configuration to be loaded before @@ -56,37 +49,68 @@ @initialized = true self end ## - # Return paths based off Rails default plus some customization. + # Override Rails secrets management as + # it doesn't allow us to do what we want. + # Then, Ecrire will merge anything that is + # through environment variables # - # These paths are Ecrire's, not the users's theme. - # - # For the user's paths, look at Ecrire::Theme::Engine.paths - # - def paths - @paths ||= begin - paths = super - paths.add 'config/secrets', with: Dir.pwd + '/secrets.yml' - paths.add 'config/database', with: Dir.pwd + '/secrets.yml' - paths.add 'config/routes.rb', with: 'routes.rb' - paths.add 'config/locales', with: 'locales', glob: "**/*.{rb,yml}" - paths - end + def secrets + @secrets ||= begin + secrets = ActiveSupport::OrderedOptions.new + yaml = config.paths["config/secrets"].first + + if File.exist?(yaml) + require "erb" + content = YAML.load(ERB.new(IO.read(yaml)).result) || {} + secrets.merge!(content.deep_symbolize_keys) + end + + if ENV.has_key?(Ecrire::SECRET_ENVIRONMENT_KEY) + require 'json' + secrets.merge!(JSON.parse(ENV[Ecrire::SECRET_ENVIRONMENT_KEY]).deep_symbolize_keys) + end + + # Fallback to config.secret_key_base if secrets.secret_key_base isn't set + secrets.secret_key_base ||= config.secret_key_base + # Fallback to config.secret_token if secrets.secret_token isn't set + secrets.secret_token ||= config.secret_token + + secrets + end end - ## - # Returns true if Ecrire::Onboarding::Engine is loaded - # in the application runtime - # - def self.onboarding? - defined?(Ecrire::Onboarding::Engine) + def config + @config ||= Ecrire::Configuration.new(Pathname.new(self.class.called_from)) end + class << self + + ## + # Returns true if Ecrire::Onboarding::Engine is loaded + # in the application runtime + # + def onboarding? + secrets.fetch(:onboarding, true) + end + + end + Rails::Application::Bootstrap.initializers.select do |initializer| initializer.name.eql? :bootstrap_hook end.first.instance_variable_set :@block, Proc.new {} + + config.before_initialize do + require 'ecrire/config/environment' + if onboarding? + require 'ecrire/onboarding/engine' + else + require 'ecrire/theme/engine' + end + end + end end