diff --git a/app/models/alchemy/ingredient.rb b/app/models/alchemy/ingredient.rb index 5419f8fdbc..58ce87f7b0 100644 --- a/app/models/alchemy/ingredient.rb +++ b/app/models/alchemy/ingredient.rb @@ -80,7 +80,7 @@ def related_object_alias(name, class_name:) # Returns an ingredient class by type # - # Raises ArgumentError if there is no such class in the + # Raises NameError if there is no such class in the # +Alchemy::Ingredients+ module namespace. # # If you add custom ingredient class, @@ -89,7 +89,7 @@ def related_object_alias(name, class_name:) # @param [String] The ingredient class name to constantize # @return [Class] def ingredient_class_by_type(ingredient_type) - Alchemy::Ingredients.const_get(ingredient_type.to_s.classify.demodulize) + normalize_type(ingredient_type).constantize end # Modulize ingredient type diff --git a/spec/models/alchemy/ingredient_spec.rb b/spec/models/alchemy/ingredient_spec.rb index e921fa199b..507fe16612 100644 --- a/spec/models/alchemy/ingredient_spec.rb +++ b/spec/models/alchemy/ingredient_spec.rb @@ -105,6 +105,34 @@ end end + describe ".ingredient_class_by_type" do + subject { described_class.ingredient_class_by_type(ingredient_type) } + + context "with a known ingredient class" do + let(:ingredient_type) { "Text" } + + it "returns full ingredient constant" do + expect(subject).to eq(Alchemy::Ingredients::Text) + end + end + + context "with unkown ingredient class" do + let(:ingredient_type) { "Foo" } + + it do + expect { subject }.to raise_error(NameError) + end + end + end + + describe ".normalize_type" do + subject { described_class.normalize_type("Text") } + + it "returns full ingredient constant name" do + is_expected.to eq("Alchemy::Ingredients::Text") + end + end + describe "#settings" do let(:ingredient) { Alchemy::Ingredients::Text.build(role: "headline", element: element) }