Skip to content

Commit

Permalink
Merge pull request #2824 from tvdeyen/file-watcher
Browse files Browse the repository at this point in the history
Clear definitions cache after file change
  • Loading branch information
tvdeyen authored Apr 10, 2024
2 parents 0c76560 + 0105c6e commit 17908f9
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!
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.
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 17908f9

Please sign in to comment.