diff --git a/app/models/alchemy/ingredient.rb b/app/models/alchemy/ingredient.rb index 7228abe8e0..6ef01ae1bf 100644 --- a/app/models/alchemy/ingredient.rb +++ b/app/models/alchemy/ingredient.rb @@ -90,6 +90,12 @@ def allowed_settings end end + # This method can be overwritten in individual ingredients to fetch objects that belong to the related object in some form. + # This takes an Array of things that can be passed to the Rails preloader. + def preload_relations + [] + end + # The value or the related object if present def value related_object || self[:value] diff --git a/app/models/alchemy/ingredients/picture.rb b/app/models/alchemy/ingredients/picture.rb index 378dd694f5..9bbdbd7773 100644 --- a/app/models/alchemy/ingredients/picture.rb +++ b/app/models/alchemy/ingredients/picture.rb @@ -38,6 +38,10 @@ class Picture < Alchemy::Ingredient upsample ] + def preload_relations + [:thumbs] + end + # The first 30 characters of the pictures name # # Used by the Element#preview_text method. diff --git a/spec/models/alchemy/ingredient_spec.rb b/spec/models/alchemy/ingredient_spec.rb index 364b04a1ad..5b883fe370 100644 --- a/spec/models/alchemy/ingredient_spec.rb +++ b/spec/models/alchemy/ingredient_spec.rb @@ -213,4 +213,12 @@ ) end end + + describe "#preload_relations" do + let(:ingredient) { Alchemy::Ingredients::Text.new(role: "intro", element: element) } + + subject { ingredient.preload_relations } + + it { is_expected.to eq([]) } + end end diff --git a/spec/models/alchemy/ingredients/picture_spec.rb b/spec/models/alchemy/ingredients/picture_spec.rb index da1b4182b0..98b63bb318 100644 --- a/spec/models/alchemy/ingredients/picture_spec.rb +++ b/spec/models/alchemy/ingredients/picture_spec.rb @@ -125,4 +125,12 @@ ) end end + + describe "#preload_relations" do + let(:ingredient) { described_class.new } + + subject { ingredient.preload_relations } + + it { is_expected.to eq([:thumbs]) } + end end