Skip to content

Commit

Permalink
explicitly reset registries on reload
Browse files Browse the repository at this point in the history
  • Loading branch information
toy committed Jul 1, 2024
1 parent ddf0dc2 commit 73424a7
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
3 changes: 2 additions & 1 deletion config/initializers/acts_as_favorable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# as no eager loading takes place
Rails.application.config.to_prepare do
OpenProject::Acts::Favorable::Registry.add(
Project
Project,
reset: true
)
end
3 changes: 2 additions & 1 deletion config/initializers/acts_as_watchable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
News,
Wiki,
WikiPage,
WorkPackage
WorkPackage,
reset: true
)
end
4 changes: 3 additions & 1 deletion lib_static/open_project/acts/registry_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ def instance(model_name)
models[model_name.singularize.camelize]
end

def add(*models)
def add(*models, reset: false)
instance_methods_module = module_parent.const_get(:InstanceMethods)
acts_as_method_name = "acts_as_#{module_parent_name.demodulize.underscore}"

self.models.clear if reset

models.each do |model|
unless model.ancestors.include?(instance_methods_module)
raise ArgumentError.new("Model #{model} does not include #{acts_as_method_name}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,26 @@ def described_module = described_class
expect(registry.instance("models")).to eq(reloaded_model)
end
end

describe "after registry reset" do
let(:other_model) { Class.new }

before do
other_model.include instance_methods_module

allow(other_model).to receive(:name).and_return("OtherModel")

registry.add(model)
registry.add(other_model, reset: true)
end

it "doesn't return model registered before reset" do
expect(registry.instance("models")).to be_nil
end

it "returns model registered after reset" do
expect(registry.instance("other_models")).to eq(other_model)
end
end
end
end

0 comments on commit 73424a7

Please sign in to comment.