diff --git a/config/initializers/acts_as_favorable.rb b/config/initializers/acts_as_favorable.rb index 6ec2f1d50b6d..79086042f211 100644 --- a/config/initializers/acts_as_favorable.rb +++ b/config/initializers/acts_as_favorable.rb @@ -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 diff --git a/config/initializers/acts_as_watchable.rb b/config/initializers/acts_as_watchable.rb index 7097c990dec1..7c7ef06abee3 100644 --- a/config/initializers/acts_as_watchable.rb +++ b/config/initializers/acts_as_watchable.rb @@ -10,6 +10,7 @@ News, Wiki, WikiPage, - WorkPackage + WorkPackage, + reset: true ) end diff --git a/lib_static/open_project/acts/registry_methods.rb b/lib_static/open_project/acts/registry_methods.rb index ae9632baba00..6fe3e6e36eba 100644 --- a/lib_static/open_project/acts/registry_methods.rb +++ b/lib_static/open_project/acts/registry_methods.rb @@ -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}") diff --git a/spec/lib/acts_as_watchable/lib/open_project/acts/registry_methods_spec.rb b/spec/lib/acts_as_watchable/lib/open_project/acts/registry_methods_spec.rb index da6caeaae627..ed300fa779c9 100644 --- a/spec/lib/acts_as_watchable/lib/open_project/acts/registry_methods_spec.rb +++ b/spec/lib/acts_as_watchable/lib/open_project/acts/registry_methods_spec.rb @@ -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