Browse Source

hot_constants

main
pvincent 5 months ago
parent
commit
4e99f65d6f
  1. 8
      .env.sample
  2. 15
      app/reload/live_constants.rb
  3. 2
      app/views/layouts/application.html.erb
  4. 5
      config/environments/development.rb
  5. 7
      config/initializers/rails_live_reload.rb
  6. 24
      lib/hot_constants/live_constants.rb
  7. 18
      lib/monkey_patches/rails_live_reload/watcher.rb

8
.env.sample

@ -1,3 +1,5 @@
# STATIC: restart server when changes!
#-------------------------------------
RAILS_PORT=7500
DATABASE_HOST=localhost
@ -5,3 +7,9 @@ DATABASE_NAME=easy-going-rails
DATABASE_USER=$DATABASE_NAME
DATABASE_PASSWORD=$DATABASE_NAME
# HOT CONSTANTS
#--------------
STIMULUS_DEBUG=false
LOG_ACTIVE_RECORD=false
LOG_ACTION_VIEW=false

15
app/reload/live_constants.rb

@ -1,15 +0,0 @@
# Development Constants
module LiveConstants
# constants
STIMULUS_DEBUG = false
LOG_ACTIVE_RECORD = false
LOG_ACTION_VIEW = false
def self.reload!
ActionView::Base.logger.level = LOG_ACTION_VIEW ? :debug : :fatal
ActiveRecord::Base.logger.level = LOG_ACTIVE_RECORD ? :debug : :fatal
SemanticLogger[to_s].warn 'reloaded!'
end
reload!
end

2
app/views/layouts/application.html.erb

@ -11,7 +11,7 @@
<%= tag :meta, name: :viewport, content: 'width=device-width,initial-scale=1' %>
<% if Rails.env.development? %>
<%= tag :meta, name: 'turbo-prefetch', content: false %>
<%= tag :meta, name: 'stimulus-debug', content: LiveConstants::STIMULUS_DEBUG %>
<%= tag :meta, name: 'stimulus-debug', content: LiveConstants.stimulus_debug %>
<% end%>
<!-- LINK-->

5
config/environments/development.rb

@ -95,4 +95,9 @@ Rails.application.configure do # rubocop:disable Metrics/BlockLength
routes.default_url_options[:host] = '127.0.0.1'
routes.default_url_options[:port] = ARGV[0] == '--port' ? ARGV[1] : 3000 # ie: Procfile.dev first two arguments
config.after_initialize do
require Rails.root.join('lib', 'hot_constants', 'live_constants')
LiveConstants.reload!
end
end

7
config/initializers/rails_live_reload.rb

@ -1,9 +1,7 @@
# frozen_string_literal: true
if defined?(RailsLiveReload)
if defined?(RailsLiveReload) && Rails.env.development?
RailsLiveReload.configure do |config|
# config.url = "/rails/live/reload"
# Default watched folders & files
config.watch %r{app/views/.+\.(erb|haml|slim)$}
config.watch %r{(app|vendor)/(assets|javascript)/\w+/(.+\.(css|js|html|png|jpg|ts|jsx)).*}, reload: :always
@ -11,6 +9,7 @@ if defined?(RailsLiveReload)
# USEFUL for tailwind changes!!!!
config.watch %r{app/assets/builds/tailwind.css}, reload: :always
config.enabled = Rails.env.development?
# HOT Constants
config.watch(%r{/\.env}, reload: :always)
end
end

24
lib/hot_constants/live_constants.rb

@ -0,0 +1,24 @@
require 'dotenv'
# Development Constants
module LiveConstants
HOT = {} # rubocop:disable Style/MutableConstant
LOGGER = SemanticLogger[to_s]
class << self
def stimulus_debug = load_boolean('STIMULUS_DEBUG', false)
def log_active_record = load_boolean('LOG_ACTIVE_RECORD', true)
def log_action_view = load_boolean('LOG_ACTION_VIEW', true)
def load_boolean(key, default) = HOT.fetch(key, default).to_s.downcase == 'true'
def reload!
HOT.replace Dotenv.parse
# LOGGER.warn 'reloaded!'
ActiveRecord::Base.logger.level = log_active_record ? :debug : :fatal
ActionView::Base.logger.level = log_action_view ? :debug : :fatal
end
end
reload!
end

18
lib/monkey_patches/rails_live_reload/watcher.rb

@ -0,0 +1,18 @@
module RailsLiveReload
# MonkeyPath Watcher
class Watcher
ENV_FILE = Rails.root.join('.env').to_s
def reload_all
data = {
event: RailsLiveReload::INTERNAL[:socket_events][:reload],
files:
}.to_json
LiveConstants.reload! if files.find { |change| change[0] == ENV_FILE }
@sockets.each do |socket, _|
socket.puts data
end
end
end
end
Loading…
Cancel
Save