~goofansu/rails-config-demo

2e7787010609bdf6c045f2e06b3f691012676f27 — Yejun Su 2 years ago c4fcf48
gem with classic mode loads class unexpectedly

Output:

before_configuration
application.rb loaded
before_initialize
default_protect_from_forgery is true, expected: false
config/initializers loaded
ActiveSupport.on_load(:before_initialize) runs at the end of before_initialize
after_initialize
default_protect_from_forgery is false, expected: false
ActiveSupport.on_load(:after_initialize) runs at the end of after_initialize
4 files changed, 74 insertions(+), 0 deletions(-)

M Gemfile
M Gemfile.lock
M config/application.rb
A config/initializers/custom_initializer.rb
M Gemfile => Gemfile +3 -0
@@ 25,3 25,6 @@ end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

gem 'wechat', '~> 0.12.0' # classic mode
# gem 'wechat', '~> 0.13.0' # zeitwerk mode

M Gemfile.lock => Gemfile.lock +29 -0
@@ 60,14 60,31 @@ GEM
      minitest (>= 5.1)
      tzinfo (~> 2.0)
      zeitwerk (~> 2.3)
    addressable (2.7.0)
      public_suffix (>= 2.0.2, < 5.0)
    builder (3.2.4)
    byebug (11.1.3)
    concurrent-ruby (1.1.8)
    crass (1.0.6)
    domain_name (0.5.20190701)
      unf (>= 0.0.5, < 1.0.0)
    erubi (1.10.0)
    ffi (1.14.2)
    ffi-compiler (1.0.1)
      ffi (>= 1.0.0)
      rake
    globalid (0.4.2)
      activesupport (>= 4.2.0)
    http (4.4.1)
      addressable (~> 2.3)
      http-cookie (~> 1.0)
      http-form_data (~> 2.2)
      http-parser (~> 1.2.0)
    http-cookie (1.0.3)
      domain_name (~> 0.5)
    http-form_data (2.3.0)
    http-parser (1.2.3)
      ffi-compiler (>= 1.0, < 2.0)
    i18n (1.8.9)
      concurrent-ruby (~> 1.0)
    listen (3.4.1)


@@ 89,6 106,7 @@ GEM
    nokogiri (1.11.1)
      mini_portile2 (~> 2.5.0)
      racc (~> 1.4)
    public_suffix (4.0.6)
    puma (5.2.2)
      nio4r (~> 2.0)
    racc (1.5.2)


@@ 125,6 143,7 @@ GEM
    rb-fsevent (0.10.4)
    rb-inotify (0.10.1)
      ffi (~> 1.0)
    rexml (3.2.4)
    sass-rails (6.0.0)
      sassc-rails (~> 2.1, >= 2.1.1)
    sassc (2.4.0)


@@ 147,9 166,18 @@ GEM
    tilt (2.0.10)
    tzinfo (2.0.4)
      concurrent-ruby (~> 1.0)
    unf (0.1.4)
      unf_ext
    unf_ext (0.0.7.7)
    websocket-driver (0.7.3)
      websocket-extensions (>= 0.1.0)
    websocket-extensions (0.1.5)
    wechat (0.12.2)
      activesupport (>= 3.2, < 7)
      http (>= 1.0.4, < 5)
      nokogiri (>= 1.6.0)
      rexml
      thor
    zeitwerk (2.4.2)

PLATFORMS


@@ 163,6 191,7 @@ DEPENDENCIES
  sass-rails (>= 6)
  sqlite3 (~> 1.4)
  tzinfo-data
  wechat (~> 0.12.0)

RUBY VERSION
   ruby 2.6.5p114

M config/application.rb => config/application.rb +39 -0
@@ 7,6 7,19 @@ require "active_model/railtie"
require "active_record/railtie"
# require "active_storage/engine"
require "action_controller/railtie"

ActionController::Railtie.initializer "puts default_protect_from_forgery when ActionController::Base is loaded" do
  ActiveSupport.on_load(:action_controller_base) do
    puts "default_protect_from_forgery is #{Rails.application.config.action_controller.default_protect_from_forgery}, expected: false"
  end
end

ActionController::Railtie.initializer "puts default_protect_from_forgery when application is initialized" do
  ActiveSupport.on_load(:after_initialize) do
    puts "default_protect_from_forgery is #{Rails.application.config.action_controller.default_protect_from_forgery}, expected: false"
  end
end

# require "action_mailer/railtie"
# require "action_mailbox/engine"
# require "action_text/engine"


@@ 34,5 47,31 @@ module RailsConfig

    # Don't generate system test files.
    config.generators.system_tests = nil

    config.before_configuration do
      puts "before_configuration"
    end

    config.before_initialize do
      puts "before_initialize"
    end

    config.after_initialize do
      puts "after_initialize"
    end

    initializer "before_initialize hook" do |app|
      ActiveSupport.on_load(:before_initialize) do
        puts "ActiveSupport.on_load(:before_initialize) runs at the end of before_initialize"
      end
    end

    initializer "after_initialize hook" do |app|
      ActiveSupport.on_load(:after_initialize) do
        puts "ActiveSupport.on_load(:after_initialize) runs at the end of after_initialize"
      end
    end

    puts "application.rb loaded"
  end
end

A config/initializers/custom_initializer.rb => config/initializers/custom_initializer.rb +3 -0
@@ 0,0 1,3 @@
Rails.application.config.action_controller.default_protect_from_forgery = false

puts 'config/initializers loaded'