Skip to content

Commit

Permalink
Clear definitions cache after file change
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tvdeyen committed Apr 10, 2024
1 parent 0c76560 commit 0105c6e
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 12 deletions.
16 changes: 10 additions & 6 deletions lib/alchemy/element_definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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+.
Expand All @@ -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
17 changes: 17 additions & 0 deletions lib/alchemy/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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!

Check warning on line 41 in lib/alchemy/engine.rb

View check run for this annotation

Codecov / codecov/patch

lib/alchemy/engine.rb#L40-L41

Added lines #L40 - L41 were not covered by tests
end
page_layouts_reloader = app.config.file_watcher.new([PageLayout.layouts_file_path]) do
Rails.logger.info "[#{engine_name}] Reloading Page Layouts."
PageLayout.reset!

Check warning on line 45 in lib/alchemy/engine.rb

View check run for this annotation

Codecov / codecov/patch

lib/alchemy/engine.rb#L44-L45

Added lines #L44 - L45 were not covered by tests
end
[elements_reloader, page_layouts_reloader].each do |reloader|
app.reloaders << reloader
app.reloader.to_run do
reloader.execute_if_updated

Check warning on line 50 in lib/alchemy/engine.rb

View check run for this annotation

Codecov / codecov/patch

lib/alchemy/engine.rb#L50

Added line #L50 was not covered by tests
end
end
end

# Gutentag downcases all tags before save
# and Gutentag validations are not case sensitive.
# But we support having tags with uppercase characters.
Expand Down
16 changes: 10 additions & 6 deletions lib/alchemy/page_layout.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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+.
Expand All @@ -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
8 changes: 8 additions & 0 deletions spec/libraries/element_definition_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 8 additions & 0 deletions spec/libraries/page_layout_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 0105c6e

Please sign in to comment.