Use cleverer patterns to fetch/iterate AR objects when checking presence (#5833) [deploy]

This commit is contained in:
rhymes 2020-02-05 14:21:20 +01:00 committed by GitHub
parent be6aaf750c
commit a95877e8ff
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 40 additions and 26 deletions

View file

@ -19,7 +19,9 @@ module Notifications
notifiable_type: notifiable.class.name,
action: action,
)
return if notifications.blank?
# as we only select the first notification right after, there is no need
# to load all of them in memory with `.blank?`, thus we choose `.none?`
return if notifications.none?
new_json_data = notifications.first.json_data || {}
new_json_data[notifiable.class.name.downcase] = public_send("#{notifiable.class.name.downcase}_data", notifiable)

View file

@ -3,7 +3,7 @@ module Users
module_function
def call(user, cache_buster = CacheBuster)
return unless user.articles.any?
return if user.articles.blank?
virtual_articles = user.articles.map { |article| Article.new(article.attributes) }
user.articles.find_each do |article|

View file

@ -6,7 +6,8 @@
follow_cue: @boosted_article.organization&.tag_line || @boosted_article.organization&.tag_line %>
<% end %>
<% if @suggested_articles.any? %>
<%# the pattern .present?/.each has the advantage of issuing only a single SQL query to load objects in memory %>
<% if @suggested_articles.present? %>
<% @suggested_articles.each do |article| %>
<%= render "additional_content_boxes/article_box",
article: article,

View file

@ -11,7 +11,8 @@
</span>
</header>
<% @sponsorships = Sponsorship.gold.live.includes(:organization).order(featured_number: :asc) %>
<% if @sponsorships.any? %>
<%# the pattern .present?/.each has the advantage of issuing only a single SQL query to load objects in memory %>
<% if @sponsorships.present? %>
<div class="widget-body">
<% @sponsorships.each do |sponsorship| %>
<%= render "articles/single_sponsor", sponsorship: sponsorship %>

View file

@ -161,15 +161,15 @@
<% end %>
<% unless user_signed_in? %>
<% cache("seo-boostable-posts-homepage-#{params[:timeframe]}-xoxo", expires_in: 18.hours) do %>
<% @boostable_posts = Article.seo_boostable(nil, Timeframer.new(params[:timeframe]).datetime) %>
<% if @boostable_posts.any? %>
<% boostable_posts = Article.seo_boostable(nil, Timeframer.new(params[:timeframe]).datetime) %>
<% if boostable_posts.present? %>
<div class="widget">
<header>
<h4>trending guides/resources</h4>
</header>
<div class="widget-body">
<div class="widget-link-list">
<% @boostable_posts.each do |plucked_article| %>
<% boostable_posts.each do |plucked_article| %>
<%= render "articles/widget_list_item", plucked_article: plucked_article, show_comment_count: false %>
<% end %>
</div>

View file

@ -104,7 +104,9 @@
</div>
<div class="substories" id="substories">
<% if @stories.any? %>
<%# articles/tags/main_feed will iterate on stories with .each_with_index,
thus by using .present? here we preload the items %>
<% if @stories.present? %>
<%= render "articles/tags/main_feed" %>
<% end %>
</div>

View file

@ -1,14 +1,15 @@
<div id="sidebar-wrapper-right" class="sidebar-wrapper sidebar-wrapper-right">
<div class="sidebar-bg" id="sidebar-bg-right"></div>
<div class="side-bar sidebar-additional showing" id="sidebar-additional">
<% if Article.active_threads([@tag, "discuss"], Timeframer.new(params[:timeframe]).datetime).any? %>
<% active_threads = Article.active_threads([@tag, "discuss"], Timeframer.new(params[:timeframe]).datetime) %>
<% if active_threads.present? %>
<div class="widget">
<header>
<h4>#discuss</h4>
</header>
<div class="widget-body">
<div class="widget-link-list">
<% Article.active_threads([@tag, "discuss"], Timeframer.new(params[:timeframe]).datetime).each do |plucked_article| %>
<% active_threads.each do |plucked_article| %>
<%= render "articles/widget_list_item", plucked_article: plucked_article, show_comment_count: true %>
<% end %>
</div>
@ -21,15 +22,15 @@
</div>
<% else %>
<% cache("seo-boostable-posts-for-tag-#{@tag}-#{params[:timeframe]}-xoxo", expires_in: 18.hours) do %>
<% @boostable_posts = Article.seo_boostable(@tag, Timeframer.new(params[:timeframe]).datetime) %>
<% if @boostable_posts.any? %>
<% boostable_posts = Article.seo_boostable(@tag, Timeframer.new(params[:timeframe]).datetime) %>
<% if boostable_posts.present? %>
<div class="widget">
<header>
<h4>trending guides/resources</h4>
</header>
<div class="widget-body">
<div class="widget-link-list">
<% @boostable_posts.each do |plucked_article| %>
<% boostable_posts.each do |plucked_article| %>
<%= render "articles/widget_list_item", plucked_article: plucked_article, show_comment_count: false %>
<% end %>
</div>
@ -39,4 +40,4 @@
<% end %>
<% end %>
</div>
</div>
</div>

View file

@ -86,7 +86,7 @@
</details>
</div>
<div class="col-12">
<% if @featured_articles && @featured_articles.any? %>
<% if @featured_articles.present? %>
<h2>Manually Featured Articles</h2>
<% @featured_articles.each do |article| %>
<%= render "individual_article", article: article %>

View file

@ -5,7 +5,7 @@
<h2 class="m-0"><%= link_to @podcast.title, "/#{@podcast.slug}" %></h2>
</div>
<div class="card-body">
<% if @podcast.admins.any? %>
<% if @podcast.admins.present? %>
<h3>Admins:</h3>
<ul class="list-group list-group-flush mb-3">
<% @podcast.admins.each do |admin| %>

View file

@ -72,7 +72,7 @@
<%= f.text_area :reason_for_adjustment, placeholder: "Reason for adjustment (Be super kind) - Only the reason is needed, the notification will take care of the rest.", required: true %>
<%= f.submit "Submit Tag Adjustment" %>
<% end %>
<% if @adjustments.any? %>
<% if @adjustments.present? %>
<% @adjustments.each do |adjustment| %>
<% if current_user.any_admin? || adjustment.user_id == current_user.id %>
<%= form_for(adjustment, url: "/tag_adjustments/#{adjustment.id}", html: { method: :delete, onsubmit: "return confirm('Are you sure you want to undo the #{adjustment.adjustment_type} of the #{adjustment.tag_name} tag?')" }) do |f| %>

View file

@ -7,7 +7,10 @@
</div>
<% end %>
<% if @organization.users.present? %>
<%# given the probability that organizations can have lots of users,
here we're using the any?/find_each pattern to avoid loading possibly
too many objects in memory %>
<% if @organization.users.any? %>
<div class="widget">
<div class="widget-suggested-follows-container">
<header><h4>meet the team</h4></header>

View file

@ -12,7 +12,9 @@
<%= render "organizations/sidebar" %>
<div class="articles-list" id="articles-list">
<div class="substories" id="substories">
<% if @stories.any? %>
<%# organizations/main_feed will iterate on stories with .each_with_index,
thus by using .present? here we preload the items %>
<% if @stories.present? %>
<%= render "organizations/main_feed" %>
<% end %>
</div>

View file

@ -1,4 +1,4 @@
<% if !organizations.any? %>
<% if organizations.none? %>
<div class="partner-credits-explainer">
<h4>What next?</h4>
<h3>Create an Organization</h3>
@ -46,7 +46,7 @@
<br /><br />Your placement is <em>pending</em>.
<% end %>
</div>
<% elsif sponsorships.any? %>
<% elsif sponsorships.present? %>
<div class="partner-explainer-notice">
<%# this should never happen but better safe than sorry %>
<% sponsorships.each do |sponsorship| %>

View file

@ -8,7 +8,7 @@
}
</style>
<div class="index-comments">
<% if @comments.any? %>
<% if @comments.present? %>
<% if params[:view] == "comments" %>
<div class="breadcrumb"><a href="<%= @user.path %>">View All Activity</a></div>
<header>

View file

@ -1,4 +1,4 @@
<% if @pinned_stories.any? %>
<% if @pinned_stories.present? %>
<div class="pinned-articles">
<div class="pinned-articles-indicator"><img src="<%= asset_path("pushpin.svg") %>" alt="pin" /> Pinned</div>
<% @pinned_stories.each do |story| %>
@ -8,11 +8,11 @@
<% end %>
<% @stories.each_with_index do |story, i| %>
<%= render "articles/single_story", story: story %>
<% if i == 0 && @comments.any? %>
<% if i == 0 && @comments.present? %>
<%= render "users/comments_section" %>
<% end %>
<% end %>
<% if !@stories.any? && @comments.any? %>
<% if @stories.none? && @comments.present? %>
<%= render "users/comments_section" %>
<% end %>
<% if @stories.size > 1 %>

View file

@ -1,9 +1,11 @@
<%# it's highly unlikely a single use will belong to so many organizations to slow down
this fetch, thus we can safely use the .present?/.each pattern to load them %>
<% if @user.organizations.present? %>
<div id="sidebar-organizations" class="widget">
<div class="widget-suggested-follows-container">
<header><h4>organizations</h4></header>
<div class="widget-body">
<% @user.organizations.find_each do |organization| %>
<% @user.organizations.each do |organization| %>
<div class="widget-user-pic">
<a href="/<%= organization.slug %>">
<img src="<%= ProfileImage.new(organization).get(90) %>" alt="<%= organization.name %> profile image">