Class: Ecrire::Application

Inherits:
Rails::Application
  • Object
show all
Defined in:
lib/ecrire/application.rb

Overview

Ecrire::Application is the entry point when running a blog.

The big difference between this application and a normal Rails application is that Ecrire will look for secrets.yml in the current working directory.

If it doesn’t find one, it will load the Onboarding process so the user can configure the database and the first user.

If the application finds secrets.yml, it will load the Theme which is located in the current working directory.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#pwd=(value) ⇒ Object (writeonly)

Sets the attribute pwd

Parameters:

  • value

    the value to set the attribute pwd to.



27
28
29
# File 'lib/ecrire/application.rb', line 27

def pwd=(value)
  @pwd = value
end

Class Method Details

.onboarding?Boolean

Returns true if Ecrire::Onboarding::Engine is loaded in the application runtime

Returns:

  • (Boolean)


94
95
96
# File 'lib/ecrire/application.rb', line 94

def onboarding?
  secrets.fetch(:onboarding, true)
end

Instance Method Details

#configObject



84
85
86
# File 'lib/ecrire/application.rb', line 84

def config
  @config ||= Ecrire::Configuration.new(Pathname.new(self.class.called_from))
end

#initialize!(group = :default) ⇒ Object

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 knowing which module it should load based on the current configuration.

Another issue happens with railties being memoized before Ecrire could figure out if the user needs to be onboarded.

For those reasons, this method is overloaded.



42
43
44
45
46
47
48
49
50
51
# File 'lib/ecrire/application.rb', line 42

def initialize!(group=:default) #:nodoc:
  raise "Application has been already initialized." if @initialized

  ActiveSupport.run_load_hooks(:before_initialize, self)
  @railties = Railties.new

  run_initializers(group, self)
  @initialized = true
  self
end

#secretsObject

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



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/ecrire/application.rb', line 59

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