Skip to content

Commit 13890b2

Browse files
committed
Merge pull request #209 from bjfish/protip_page_performance
Improve page loading time for the Protips page #371
2 parents bf8ab92 + 4b21fc2 commit 13890b2

File tree

3 files changed

+6
-5
lines changed

3 files changed

+6
-5
lines changed

app/controllers/protips_controller.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,8 @@ def search_options
424424

425425
{
426426
page: (signed_in? && search_options_params[:page].try(:to_i)) || 1,
427-
per_page: [(signed_in? && search_options_params[:per_page].try(:to_i)) || 18, Protip::PAGESIZE].min
427+
per_page: [(signed_in? && search_options_params[:per_page].try(:to_i)) || 18, Protip::PAGESIZE].min,
428+
load: { include: [:user, :likes, :protip_links, :comments] }
428429
}
429430
end
430431

app/models/protip.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -551,15 +551,15 @@ def best_stat
551551
{
552552
views: self.total_views/COUNTABLE_VIEWS_CHUNK,
553553
upvotes: self.upvotes,
554-
comments: self.comments.count,
554+
comments: self.comments.size,
555555
hawt: self.hawt? ? 100 : 0
556556
}.sort_by do |k, v|
557557
-v
558558
end.first
559559
end
560560

561561
def upvotes
562-
@upvotes ||= likes.count
562+
likes.size
563563
end
564564

565565
def upvotes=(count)
@@ -782,7 +782,7 @@ def images
782782
if self.new_record?
783783
self.links.select { |link| ProtipLink.is_image? link }
784784
else
785-
protip_links.where('kind in (?)', ProtipLink::IMAGE_KINDS).map(&:url)
785+
protip_links.loaded? ? protip_links.select { |p| ProtipLink::IMAGE_KINDS.include?(p.kind.to_sym) }.map(&:url) : protip_links.where('kind in (?)', ProtipLink::IMAGE_KINDS).map(&:url)
786786
end
787787
end
788788

app/views/protips/_grid.html.haml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
- if protip == 'show-ad'
2222
= render(partial: 'opportunities/mini', locals: { opportunity: opportunity })
2323
-elsif protip.present?
24-
- if protip = protip.load rescue nil # HACK: User deleted, protip no longer exists. Won't be found.
24+
- if protip.is_a?(Protip) || protip = protip.load rescue nil # HACK: User deleted, protip no longer exists. Won't be found.
2525
%li{ class: (protip.kind == 'link' ? 'ext-link' : '') }
2626
= render(partial: 'protips/mini', locals: { protip: protip, mode: mode })
2727

0 commit comments

Comments
 (0)