Skip to content

Commit

Permalink
Implement strict loading association (#189)
Browse files Browse the repository at this point in the history
Avoid N+1 query
  • Loading branch information
luciagirasoles authored Aug 19, 2023
1 parent 66db12b commit 7254eb5
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 7 deletions.
14 changes: 10 additions & 4 deletions app/controllers/admin/events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ class EventsController < AdminController
before_action :set_event, only: %w[edit update destroy]

def index
@events = Event.includes(:speakers)
.order(date: :desc)
@events = Event.includes(:speakers).order(date: :desc)
end

def new
Expand Down Expand Up @@ -54,11 +53,18 @@ def authorize_event
end

def set_event
@event = Event.find_by(id: params[:id])
@event = Event.includes(:talks).find_by(id: params[:id])
end

def event_params
params.require(:event).permit(:title, :location, :description, :date, :type, :panel_video_link)
params.require(:event).permit(
:title,
:location,
:description,
:date,
:type,
:panel_video_link,
)
end
end
end
8 changes: 5 additions & 3 deletions app/controllers/api/events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
module Api
class EventsController < ApplicationController
def past
events = Event.where('date < ?', DateTime.current).order(date: :desc)
events =
Event.includes(:talks, :speakers).where('date < ?', DateTime.current).order(date: :desc)
events_by_date =
events
.group_by { |event| event.date.year }
Expand All @@ -16,7 +17,7 @@ def past

def past_by_month_day
event_date = DateTime.new(params[:year].to_i, params[:month].to_i, params[:day].to_i)
event = Event.where(date: event_date..event_date.end_of_day).first
event = Event.includes(:talks, :speakers).where(date: event_date..event_date.end_of_day).first
if event.present?
render status: 200, json: { data: event.as_json(include: %i[talks speakers]) }
else
Expand All @@ -25,7 +26,8 @@ def past_by_month_day
end

def upcoming
events = Event.where('date > ?', DateTime.current).order(date: :asc)
events =
Event.includes(:talks, :speakers).where('date > ?', DateTime.current).order(date: :asc)
events_by_date =
events
.group_by { |event| event.date.year }
Expand Down
4 changes: 4 additions & 0 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,8 @@

# Uncomment if you wish to allow Action Cable access from any origin.
# config.action_cable.disable_request_forgery_protection = true

# Sets the returned relation to strict_loading mode.
# This will raise an error if the record tries to lazily load an association.
config.active_record.strict_loading_by_default = true
end
4 changes: 4 additions & 0 deletions config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,8 @@

# Do not dump schema after migrations.
config.active_record.dump_schema_after_migration = false

# Sets the returned relation to strict_loading mode.
# This will raise an error if the record tries to lazily load an association.
config.active_record.strict_loading_by_default = :log
end
4 changes: 4 additions & 0 deletions config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,8 @@

# Annotate rendered view with file names.
# config.action_view.annotate_rendered_view_with_filenames = true

# Sets the returned relation to strict_loading mode.
# This will raise an error if the record tries to lazily load an association.
config.active_record.strict_loading_by_default = true
end
6 changes: 6 additions & 0 deletions spec/controllers/admin/events_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
describe 'GET #new' do
it 'assigns a new event to @event' do
get :new

expect(response).to have_http_status(302)
end
end
Expand All @@ -113,6 +114,7 @@
context 'when user is not authenticated' do
it 'returns 302' do
post :create

expect(response).to have_http_status(302)
expect(response).to redirect_to(new_user_session_path)
end
Expand All @@ -124,6 +126,7 @@

it 'returns 401' do
post :create

expect(response).to have_http_status(401)
end
end
Expand Down Expand Up @@ -157,12 +160,14 @@

it 'redirects to index page when event exists' do
delete :destroy, params: { id: event.id }

expect(response).to redirect_to(admin_events_path)
expect(Event.exists?(event.id)).to be(false)
end

it 'returns 404 when the event does not exist' do
delete :destroy, params: { id: 'fakefake' }

expect(response).to have_http_status(404)
end
end
Expand All @@ -185,6 +190,7 @@

it 'redirects to login' do
delete :destroy, params: { id: event.id }

expect(response).to redirect_to(new_user_session_path)
expect(Event.exists?(event.id)).to be(true)
end
Expand Down

0 comments on commit 7254eb5

Please sign in to comment.