From 0105c6eac2160a21b101eab69661c1e2050baf23 Mon Sep 17 00:00:00 2001 From: Thomas von Deyen Date: Sun, 7 Apr 2024 23:06:16 +0200 Subject: [PATCH] Clear definitions cache after file change When the `elements.yml` or `page_layouts.yml` files change the memoiziation gets resetted, so that the next app reload includes the latest changes to these files. This removes the need to restart the Rails server after one changed an element definition or page layout. --- lib/alchemy/element_definition.rb | 16 ++++++++++------ lib/alchemy/engine.rb | 17 +++++++++++++++++ lib/alchemy/page_layout.rb | 16 ++++++++++------ spec/libraries/element_definition_spec.rb | 8 ++++++++ spec/libraries/page_layout_spec.rb | 8 ++++++++ 5 files changed, 53 insertions(+), 12 deletions(-) diff --git a/lib/alchemy/element_definition.rb b/lib/alchemy/element_definition.rb index 4b8b700039..dbb9e2983e 100644 --- a/lib/alchemy/element_definition.rb +++ b/lib/alchemy/element_definition.rb @@ -42,6 +42,16 @@ def get(name) all.detect { |a| a["name"] == name } end + def reset! + @definitions = nil + end + + # The absolute +elements.yml+ file path + # @return [Pathname] + def definitions_file_path + Rails.root.join("config", "alchemy", "elements.yml") + end + private # Reads the element definitions from +config/alchemy/elements.yml+. @@ -58,12 +68,6 @@ def read_definitions_file "Could not find elements.yml file! Please run `rails generate alchemy:install`" end end - - # Returns the elements.yml file path - # - def definitions_file_path - Rails.root.join "config/alchemy/elements.yml" - end end end end diff --git a/lib/alchemy/engine.rb b/lib/alchemy/engine.rb index 6a131e06a1..3a2d8ed9f6 100644 --- a/lib/alchemy/engine.rb +++ b/lib/alchemy/engine.rb @@ -35,6 +35,23 @@ class Engine < Rails::Engine end end + initializer "alchemy.watch_definition_changes" do |app| + elements_reloader = app.config.file_watcher.new([ElementDefinition.definitions_file_path]) do + Rails.logger.info "[#{engine_name}] Reloading Element Definitions." + ElementDefinition.reset! + end + page_layouts_reloader = app.config.file_watcher.new([PageLayout.layouts_file_path]) do + Rails.logger.info "[#{engine_name}] Reloading Page Layouts." + PageLayout.reset! + end + [elements_reloader, page_layouts_reloader].each do |reloader| + app.reloaders << reloader + app.reloader.to_run do + reloader.execute_if_updated + end + end + end + # Gutentag downcases all tags before save # and Gutentag validations are not case sensitive. # But we support having tags with uppercase characters. diff --git a/lib/alchemy/page_layout.rb b/lib/alchemy/page_layout.rb index 258484989a..c7cb9eb335 100644 --- a/lib/alchemy/page_layout.rb +++ b/lib/alchemy/page_layout.rb @@ -41,6 +41,16 @@ def get(name) all.detect { |a| a["name"].casecmp(name).zero? } end + def reset! + @definitions = nil + end + + # The absolute +page_layouts.yml+ file path + # @return [Pathname] + def layouts_file_path + Rails.root.join("config", "alchemy", "page_layouts.yml") + end + private # Reads the layout definitions from +config/alchemy/page_layouts.yml+. @@ -58,12 +68,6 @@ def read_definitions_file raise LoadError, "Could not find page_layouts.yml file! Please run `rails generate alchemy:install`" end end - - # Returns the page_layouts.yml file path - # - def layouts_file_path - Rails.root.join "config/alchemy/page_layouts.yml" - end end end end diff --git a/spec/libraries/element_definition_spec.rb b/spec/libraries/element_definition_spec.rb index 3f2b7fc6c1..948945c42e 100644 --- a/spec/libraries/element_definition_spec.rb +++ b/spec/libraries/element_definition_spec.rb @@ -35,5 +35,13 @@ module Alchemy expect(ElementDefinition.get("default")).to eq({"name" => "default"}) end end + + describe ".reset!" do + it "sets @definitions to nil" do + ElementDefinition.all + ElementDefinition.reset! + expect(ElementDefinition.instance_variable_get(:@definitions)).to be_nil + end + end end end diff --git a/spec/libraries/page_layout_spec.rb b/spec/libraries/page_layout_spec.rb index 6d142ac010..1a816c4a2f 100644 --- a/spec/libraries/page_layout_spec.rb +++ b/spec/libraries/page_layout_spec.rb @@ -67,5 +67,13 @@ module Alchemy expect(PageLayout.get("default")).to eq({"name" => "default"}) end end + + describe ".reset!" do + it "sets @definitions to nil" do + PageLayout.all + PageLayout.reset! + expect(PageLayout.instance_variable_get(:@definitions)).to be_nil + end + end end end