From 86253448f0166b3c294e05eda99a9ec16423ae11 Mon Sep 17 00:00:00 2001 From: pvincent Date: Thu, 11 Jul 2024 16:17:30 +0400 Subject: [PATCH] hot_constants first attempt --- .env.sample | 13 ++++++++----- TODO.md | 3 ++- app/views/layouts/application.html.erb | 2 +- config/environments/development.rb | 4 ++-- doc/index.md | 6 ++++++ .../{live_constants.rb => hot_constants.rb} | 6 +++++- lib/monkey_patches/rails_live_reload/watcher.rb | 5 +++-- 7 files changed, 27 insertions(+), 12 deletions(-) create mode 100644 doc/index.md rename lib/hot_constants/{live_constants.rb => hot_constants.rb} (89%) diff --git a/.env.sample b/.env.sample index 1df2736..06fe8d1 100644 --- a/.env.sample +++ b/.env.sample @@ -1,11 +1,11 @@ # STATIC: restart server when changes! #------------------------------------- -RAILS_PORT=7500 +# RAILS_PORT=7500 -DATABASE_HOST=localhost -DATABASE_NAME=easy-going-rails -DATABASE_USER=$DATABASE_NAME -DATABASE_PASSWORD=$DATABASE_NAME +# DATABASE_HOST=localhost +# DATABASE_NAME=easy-going-rails +# DATABASE_USER=$DATABASE_NAME +# DATABASE_PASSWORD=$DATABASE_NAME # HOT CONSTANTS #-------------- @@ -13,3 +13,6 @@ STIMULUS_DEBUG=false LOG_ACTIVE_RECORD=false LOG_ACTION_VIEW=false +INTEGER=1 +STRING="" +BOOLEAN=true diff --git a/TODO.md b/TODO.md index 19fdeb9..938417e 100644 --- a/TODO.md +++ b/TODO.md @@ -32,8 +32,9 @@ New Notes * [x] Use hotwire_livereload instead of rails_live_reload * [x] nope, because rails_live_reload is more clever (and sooner) than hotwire_livereload -* [x] LiveConstants +* [x] HotConstants * [x] monkeypatch rails-live-reload + * [ ] meta-programming * [x] Flash message * [x] https://reinteractive.com/articles/how-to-create-flash-messages-in-Rails-7 * [x] Minitest diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 6d2a3e2..037af26 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -14,7 +14,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: HotConstants.stimulus_debug %> <% end%> diff --git a/config/environments/development.rb b/config/environments/development.rb index 7e93d2a..12d3909 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -78,8 +78,8 @@ Rails.application.configure do # rubocop:disable Metrics/BlockLength routes.default_url_options[:host] = '127.0.0.1' config.after_initialize do - require Rails.root.join('lib', 'hot_constants', 'live_constants') - LiveConstants.reload! + require Rails.root.join('lib', 'hot_constants', 'hot_constants') + HotConstants.reload! end require Rails.root.join('lib', 'formatters', 'ansi_formatter') diff --git a/doc/index.md b/doc/index.md new file mode 100644 index 0000000..038e250 --- /dev/null +++ b/doc/index.md @@ -0,0 +1,6 @@ +Developer documentation +====================== + +Hint +---- +* Do not use AlpineJs bind:class onto a fontawesome i element, it hangs due to the SVG convert of the i element happening concurrently \ No newline at end of file diff --git a/lib/hot_constants/live_constants.rb b/lib/hot_constants/hot_constants.rb similarity index 89% rename from lib/hot_constants/live_constants.rb rename to lib/hot_constants/hot_constants.rb index 0be0b5c..f7ff5ea 100644 --- a/lib/hot_constants/live_constants.rb +++ b/lib/hot_constants/hot_constants.rb @@ -1,11 +1,15 @@ require 'dotenv' # Hot Live Constants -module LiveConstants +module HotConstants HOTENV = {} # rubocop:disable Style/MutableConstant LOGGER = SemanticLogger[to_s] class << self + def initialize + LOGGER.info('initializing') + end + 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) diff --git a/lib/monkey_patches/rails_live_reload/watcher.rb b/lib/monkey_patches/rails_live_reload/watcher.rb index 60621d5..d7b858b 100644 --- a/lib/monkey_patches/rails_live_reload/watcher.rb +++ b/lib/monkey_patches/rails_live_reload/watcher.rb @@ -1,7 +1,6 @@ module RailsLiveReload # MonkeyPath Watcher class Watcher - ENV_FILE = Rails.root.join('.env').to_s def reload_all before_reload(files) data = { event: RailsLiveReload::INTERNAL[:socket_events][:reload], files: }.to_json @@ -10,8 +9,10 @@ module RailsLiveReload private + ENV_FILE = Rails.root.join('.env').to_s + def before_reload(files) - LiveConstants.reload! if files.find { |change| change[0] == ENV_FILE } + HotConstants.reload! if files.find { |change| change[0] == ENV_FILE } end end end