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

Conversation

mamhoff
Copy link
Contributor

@mamhoff mamhoff commented Jul 19, 2023

What is this pull request for?

This allows code that renders pages or elements with ingredients to preload the object graphs below ingredient's related objects.

Notable changes (remove if none)

In order to be able to preload ingredients' related object graphs, we need to be able to tell the rails preloader how those graphs look.

This adds a method that can be used on for example an element like this:

def preload_ingredient_relations(element)
  element.ingredients.group_by(&:preload_relations).each do |preload_relations, ingredients|
    preload(records: ingredients.map(&:related_object).compact, associations: preload_relations)
  end
  element
end

def preload(records:, associations:)
  if Rails::VERSION::MAJOR >= 7
    ActiveRecord::Associations::Preloader.new(records: records, associations: associations).call
  else
    ActiveRecord::Associations::Preloader.new.preload(records, associations)
  end
end

Most notorious for n+1 problems is the picture ingredient that tends to have a lot of thumbs that we actually need.

Checklist

  • I have followed Pull Request guidelines
  • I have added a detailed description into each commit message
  • I have added tests to cover this change

In order to be able to preload ingredients' related object graphs, we
need to be able to tell the rails preloader how those graphs look.

This adds a method that can be used on for example an element like this:

```
def preload_ingredient_relations(element)
  element.ingredients.group_by(&:preload_relations).each do |preload_relations, ingredients|
    preload(records: ingredients.map(&:related_object).compact, associations: preload_relations)
  end
  element
end

def preload(records:, associations:)
  if Rails::VERSION::MAJOR >= 7
    ActiveRecord::Associations::Preloader.new(records: records, associations: associations).call
  else
    ActiveRecord::Associations::Preloader.new.preload(records, associations)
  end
end
```

Most notorious for n+1 problems is the picture ingredient that tends to
have a lot of thumbs that we actually need.
@mamhoff mamhoff requested a review from tvdeyen July 19, 2023 15:35
mamhoff added a commit to AlchemyCMS/alchemy-json_api that referenced this pull request Jul 19, 2023
This takes the capabilities from AlchemyCMS/alchemy_cms#2523 and allows
pre-loading object graphs on ingredient's related objects.
Copy link
Member

@tvdeyen tvdeyen left a comment

Choose a reason for hiding this comment

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

Great idea. I think we could also make this a static method.

@@ -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>]

@@ -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
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?

@mamhoff
Copy link
Contributor Author

mamhoff commented Jul 20, 2023

Closing in favor of AlchemyCMS/alchemy-json_api#72

@mamhoff mamhoff closed this Jul 20, 2023
@mamhoff mamhoff deleted the add-preload-relations-to-ingredients branch July 20, 2023 15:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants