Skip to content

Commit

Permalink
chore: improve app query performance
Browse files Browse the repository at this point in the history
- Reduce N+1 queries
- Cache where possible/logical
  • Loading branch information
ColinW520 committed Dec 1, 2019
1 parent d72b440 commit 7c7d481
Show file tree
Hide file tree
Showing 10 changed files with 21 additions and 13 deletions.
2 changes: 1 addition & 1 deletion app/controllers/admin/events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class Admin::EventsController < Admin::BaseController
before_action :set_event, only: [:show, :edit, :update, :destroy]

def index
@events = Event.with_attachments.by_starts_at.decorate
@events = Event.includes(:location, :races, :logo_attachment).by_starts_at.decorate
end

def show
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/admin/participants_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class Admin::ParticipantsController < Admin::BaseController
before_action :set_participant, only: [:edit, :update, :destroy, :show]

def index
@q = Participant.includes(:registration, :race).ransack(params[:q])
@q = Participant.includes(registration: [:payment, :race, :event]).ransack(params[:q])
@participants = @q.result.page(params[:page])

respond_to do |format|
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/admin/races_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class Admin::RacesController < Admin::BaseController
before_action :set_race, except: [:index, :new, :create]

def index
@races = Race.by_starts_at
@races = Race.includes(:event, :participants).by_starts_at
end

def show
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def set_time_zone(&block)
end

def set_menu_items
@menu_events = Event.with_attachments.is_active.order(:starts_at)
@menu_events = Event.is_active.order(:starts_at)
end

def set_drift_key
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ def index
end

def show
if @event.present? && @event.is_active?
if @event.is_active?
@location = @event.location
@races = @event.races.active.not_archived.order(starts_at: :asc)
@races = @event.races.includes(:course_map_attachment, :elevation_profile_attachment).active.not_archived.order(starts_at: :asc)
else
redirect_to(
root_path,
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/static_pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ def danger
end

def home
@events = Event.with_attachments.is_active.by_starts_at.decorate
@sponsors = Sponsor.with_attachments.order(:name).decorate
@events = Event.includes(:location, :cover_photo_attachment).is_active.by_starts_at.decorate
@sponsors = Sponsor.includes(:logo_attachment).order(:name).decorate
@promo_photos = @events.map { |event| event.promo_photos.first(4) }.flatten!
end

Expand Down
2 changes: 1 addition & 1 deletion app/models/participant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Participant < ApplicationRecord
scope :with_payment, -> { joins(:payment) }
scope :created_after, ->(time) { after(time) }
scope :not_cancelled, -> { joins(:registration).where(registrations: { cancelled_at: nil }) }
scope :for_start_list, -> { with_payment.not_cancelled }
scope :for_start_list, -> { includes(:event).with_payment.not_cancelled }

def full_name
"#{first_name} #{last_name}".titleize
Expand Down
3 changes: 2 additions & 1 deletion app/views/admin/races/_table.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
%th{scope: "col"}
%tbody
- @races.each do |race|
= render "race_row", race: race.decorate
- cache race do
= render "race_row", race: race.decorate
7 changes: 4 additions & 3 deletions app/views/shared/sections/_promo_photos_grid.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
.row
.card-columns
- @promo_photos.each do |promo_photo|
.card
%a{"data-gallery" => "example-gallery", "data-toggle" => "lightbox", href: url_for(promo_photo)}
= image_tag promo_photo, class: "img img-fluid rounded"
- cache promo_photo do
.card
%a{"data-gallery" => "example-gallery", "data-toggle" => "lightbox", href: url_for(promo_photo)}
= image_tag promo_photo, class: "img img-fluid rounded"
6 changes: 6 additions & 0 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,10 @@
# Use an evented file watcher to asynchronously detect changes in source code,
# routes, locales, etc. This feature depends on the listen gem.
config.file_watcher = ActiveSupport::EventedFileUpdateChecker

config.after_initialize do
Bullet.enable = true
Bullet.rails_logger = true
Bullet.add_footer = true
end
end

0 comments on commit 7c7d481

Please sign in to comment.