Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

use hash mapping name to class instead of storing just names

Ivan Kuchinddf0dc2a532d
diff --git a/lib_static/open_project/acts/registry_methods.rb b/lib_static/open_project/acts/registry_methods.rb
index ff0f275d370..ae9632baba0 100644
--- a/lib_static/open_project/acts/registry_methods.rb
+++ b/lib_static/open_project/acts/registry_methods.rb
@@ -30,9 +30,7 @@ module OpenProject
module Acts
module RegistryMethods
def instance(model_name)
- class_name = model_name.singularize.camelize
-
- class_name.constantize if class_names.include?(class_name)
+ models[model_name.singularize.camelize]
end
def add(*models)
@@ -44,14 +42,14 @@ module OpenProject
raise ArgumentError.new("Model #{model} does not include #{acts_as_method_name}")
end
- class_names << model.name
+ self.models[model.name] = model
end
end
private
- def class_names
- @class_names ||= Set.new
+ def models
+ @models ||= Hash.new
end
end
end
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 94bb1fb687f..da6caeaae62 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
@@ -58,8 +58,6 @@ RSpec.describe OpenProject::Acts::RegistryMethods do
describe ".instance" do
before do
model.include instance_methods_module
-
- allow(ActiveSupport::Inflector).to receive(:constantize).with("Model").and_return(model)
end
it "returns nil for non registered model" do
@@ -71,5 +69,22 @@ RSpec.describe OpenProject::Acts::RegistryMethods do
expect(registry.instance("models")).to eq(model)
end
+
+ describe "after model reload" do
+ let(:reloaded_model) { Class.new }
+
+ before do
+ reloaded_model.include instance_methods_module
+
+ allow(reloaded_model).to receive(:name).and_return("Model")
+
+ registry.add(model)
+ registry.add(reloaded_model)
+ end
+
+ it "returns reloaded model class" do
+ expect(registry.instance("models")).to eq(reloaded_model)
+ end
+ end
end
end