Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Define #preload_relations on Ingredients #2523

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions app/models/alchemy/ingredient.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# This takes an Array of things that can be passed to the Rails preloader.
# This takes an Array of relation names that can be passed to the Rails preloader.
# @return [Array<Symbol>]

def preload_relations
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wondering if this could be a static method of some kind, as this does not need the instance? I know it is read on the instance, but it could as well be the class instead, right?

[]
end

# The value or the related object if present
def value
related_object || self[:value]
Expand Down
4 changes: 4 additions & 0 deletions app/models/alchemy/ingredients/picture.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 8 additions & 0 deletions spec/models/alchemy/ingredient_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 8 additions & 0 deletions spec/models/alchemy/ingredients/picture_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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