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

Commit

Permalink
v1.0.3: fix rails 6 reloader (#3)
Browse files Browse the repository at this point in the history
* v1.0.3: fix rails 6 reloader

* fix rubocop
  • Loading branch information
spape authored Apr 8, 2021
1 parent 9173aab commit 9f682ba
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 42 deletions.
6 changes: 4 additions & 2 deletions lib/dhs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,12 @@ module Nested
autoload :Unprocessable, 'dhs/unprocessable'

include Configuration
include AutoloadRecords if defined?(Rails)
include OptionBlocks

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

require 'dhs/railtie' if defined?(Rails)
if defined?(Rails)
include AutoloadRecords
require 'dhs/railtie'
end
end
31 changes: 17 additions & 14 deletions lib/dhs/concerns/autoload_records.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ class Engine < Rails::Engine
end

class Middleware

MODEL_FILES = 'app/models/**/*.rb'

def initialize(app)
@app = app
end
Expand All @@ -27,24 +30,24 @@ def call(env)
@app.call(env)
end

def self.model_files
Dir.glob(Rails.root.join('app', 'models', '**', '*.rb'))
end

def self.require_direct_inheritance
model_files.sort.map do |file|
next unless File.read(file).match('DHS::Record')
require_dependency file
file.split('models/').last.gsub('.rb', '').classify
end.compact
Rails.application.reloader.to_prepare do
Dir.glob(Rails.root.join(MODEL_FILES)).each do |file|
next unless File.read(file).match('DHS::Record')
require_dependency file
file.split('models/').last.gsub('.rb', '').classify
end.compact
end
end

def self.require_inheriting_records(parents)
model_files.each do |file|
file_content = File.read(file)
next if parents.none? { |parent| file_content.match(/\b#{parent}\b/) }
next if file_content.match?('extend ActiveSupport::Concern')
require_dependency file
Rails.application.reloader.to_prepare do
Dir.glob(Rails.root.join(MODEL_FILES)).each do |file|
file_content = File.read(file)
next if parents.none? { |parent| file_content.match(/\b#{parent}\b/) }
next if file_content.match?('extend ActiveSupport::Concern')
require_dependency file
end
end
end

Expand Down
27 changes: 2 additions & 25 deletions lib/dhs/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,8 @@
module DHS
class Railtie < Rails::Railtie
initializer 'dhs.hook_into_controller_initialization' do
class ActionController::Base

def initialize
prepare_dhs_request_cycle_cache
reset_option_blocks
reset_extended_rollbar_request_logs
super
end

private

def prepare_dhs_request_cycle_cache
return unless DHS.config.request_cycle_cache_enabled
DHS::Interceptors::RequestCycleCache::ThreadRegistry.request_id = [Time.now.to_f, request.object_id].join('#')
end

def reset_option_blocks
DHS::OptionBlocks::CurrentOptionBlock.options = nil
end

def reset_extended_rollbar_request_logs
return unless defined?(::Rollbar)
return unless DHC.config.interceptors.include?(DHS::Interceptors::ExtendedRollbar::Interceptor)
DHS::Interceptors::ExtendedRollbar::ThreadRegistry.log = []
end
Rails.application.reloader.to_prepare do
require_relative 'railtie/action_controller_extension'
end
end
end
Expand Down
34 changes: 34 additions & 0 deletions lib/dhs/railtie/action_controller_extension.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# frozen_string_literal: true

module DHS
class Railtie < Rails::Railtie

class ::ActionController::Base

def initialize
prepare_dhs_request_cycle_cache
reset_option_blocks
reset_extended_rollbar_request_logs
super
end

private

def prepare_dhs_request_cycle_cache
return unless DHS.config.request_cycle_cache_enabled
DHS::Interceptors::RequestCycleCache::ThreadRegistry.request_id = [Time.now.to_f, request.object_id].join('#')
end

def reset_option_blocks
DHS::OptionBlocks::CurrentOptionBlock.options = nil
end

def reset_extended_rollbar_request_logs
return unless defined?(::Rollbar)
return unless DHC.config.interceptors.include?(DHS::Interceptors::ExtendedRollbar::Interceptor)
DHS::Interceptors::ExtendedRollbar::ThreadRegistry.log = []
end
end

end
end
2 changes: 1 addition & 1 deletion lib/dhs/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module DHS
VERSION = '1.0.2'
VERSION = '1.0.3'
end

0 comments on commit 9f682ba

Please sign in to comment.