Skip to content
This repository was archived by the owner on Oct 6, 2024. It is now read-only.

Commit 9f682ba

Browse files
authored
v1.0.3: fix rails 6 reloader (#3)
* v1.0.3: fix rails 6 reloader * fix rubocop
1 parent 9173aab commit 9f682ba

File tree

5 files changed

+58
-42
lines changed

5 files changed

+58
-42
lines changed

lib/dhs.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,12 @@ module Nested
5858
autoload :Unprocessable, 'dhs/unprocessable'
5959

6060
include Configuration
61-
include AutoloadRecords if defined?(Rails)
6261
include OptionBlocks
6362

6463
require 'dhs/record' # as dhs records in an application are directly inheriting it
6564

66-
require 'dhs/railtie' if defined?(Rails)
65+
if defined?(Rails)
66+
include AutoloadRecords
67+
require 'dhs/railtie'
68+
end
6769
end

lib/dhs/concerns/autoload_records.rb

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ class Engine < Rails::Engine
1818
end
1919

2020
class Middleware
21+
22+
MODEL_FILES = 'app/models/**/*.rb'
23+
2124
def initialize(app)
2225
@app = app
2326
end
@@ -27,24 +30,24 @@ def call(env)
2730
@app.call(env)
2831
end
2932

30-
def self.model_files
31-
Dir.glob(Rails.root.join('app', 'models', '**', '*.rb'))
32-
end
33-
3433
def self.require_direct_inheritance
35-
model_files.sort.map do |file|
36-
next unless File.read(file).match('DHS::Record')
37-
require_dependency file
38-
file.split('models/').last.gsub('.rb', '').classify
39-
end.compact
34+
Rails.application.reloader.to_prepare do
35+
Dir.glob(Rails.root.join(MODEL_FILES)).each do |file|
36+
next unless File.read(file).match('DHS::Record')
37+
require_dependency file
38+
file.split('models/').last.gsub('.rb', '').classify
39+
end.compact
40+
end
4041
end
4142

4243
def self.require_inheriting_records(parents)
43-
model_files.each do |file|
44-
file_content = File.read(file)
45-
next if parents.none? { |parent| file_content.match(/\b#{parent}\b/) }
46-
next if file_content.match?('extend ActiveSupport::Concern')
47-
require_dependency file
44+
Rails.application.reloader.to_prepare do
45+
Dir.glob(Rails.root.join(MODEL_FILES)).each do |file|
46+
file_content = File.read(file)
47+
next if parents.none? { |parent| file_content.match(/\b#{parent}\b/) }
48+
next if file_content.match?('extend ActiveSupport::Concern')
49+
require_dependency file
50+
end
4851
end
4952
end
5053

lib/dhs/railtie.rb

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,8 @@
33
module DHS
44
class Railtie < Rails::Railtie
55
initializer 'dhs.hook_into_controller_initialization' do
6-
class ActionController::Base
7-
8-
def initialize
9-
prepare_dhs_request_cycle_cache
10-
reset_option_blocks
11-
reset_extended_rollbar_request_logs
12-
super
13-
end
14-
15-
private
16-
17-
def prepare_dhs_request_cycle_cache
18-
return unless DHS.config.request_cycle_cache_enabled
19-
DHS::Interceptors::RequestCycleCache::ThreadRegistry.request_id = [Time.now.to_f, request.object_id].join('#')
20-
end
21-
22-
def reset_option_blocks
23-
DHS::OptionBlocks::CurrentOptionBlock.options = nil
24-
end
25-
26-
def reset_extended_rollbar_request_logs
27-
return unless defined?(::Rollbar)
28-
return unless DHC.config.interceptors.include?(DHS::Interceptors::ExtendedRollbar::Interceptor)
29-
DHS::Interceptors::ExtendedRollbar::ThreadRegistry.log = []
30-
end
6+
Rails.application.reloader.to_prepare do
7+
require_relative 'railtie/action_controller_extension'
318
end
329
end
3310
end
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# frozen_string_literal: true
2+
3+
module DHS
4+
class Railtie < Rails::Railtie
5+
6+
class ::ActionController::Base
7+
8+
def initialize
9+
prepare_dhs_request_cycle_cache
10+
reset_option_blocks
11+
reset_extended_rollbar_request_logs
12+
super
13+
end
14+
15+
private
16+
17+
def prepare_dhs_request_cycle_cache
18+
return unless DHS.config.request_cycle_cache_enabled
19+
DHS::Interceptors::RequestCycleCache::ThreadRegistry.request_id = [Time.now.to_f, request.object_id].join('#')
20+
end
21+
22+
def reset_option_blocks
23+
DHS::OptionBlocks::CurrentOptionBlock.options = nil
24+
end
25+
26+
def reset_extended_rollbar_request_logs
27+
return unless defined?(::Rollbar)
28+
return unless DHC.config.interceptors.include?(DHS::Interceptors::ExtendedRollbar::Interceptor)
29+
DHS::Interceptors::ExtendedRollbar::ThreadRegistry.log = []
30+
end
31+
end
32+
33+
end
34+
end

lib/dhs/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module DHS
4-
VERSION = '1.0.2'
4+
VERSION = '1.0.3'
55
end

0 commit comments

Comments
 (0)