optimize queries

This commit is contained in:
Zach Kemp 2018-04-11 15:03:05 -07:00 committed by James Mason
parent 8a422412d6
commit 7d5d935a65
8 changed files with 101 additions and 43 deletions

View file

@ -3,9 +3,14 @@
module Admin
class BaseController < ApplicationController
before_action :verify_user_admin
before_action :load_all_conferences
private
def load_all_conferences
@conferences = Conference.all
end
def current_ability
@current_ability ||= AdminAbility.new(current_user)
end

View file

@ -5,7 +5,6 @@ class ApplicationController < ActionController::Base
include ApplicationHelper
add_flash_types :error
protect_from_forgery with: :exception, prepend: true
before_action :get_conferences
before_action :store_location
# Ensure every controller authorizes resource or skips authorization (skip_authorization_check)
check_authorization unless: :devise_controller?
@ -37,10 +36,6 @@ class ApplicationController < ActionController::Base
end
end
def get_conferences
@conferences = Conference.all
end
def current_ability
@current_ability ||= Ability.new(current_user)
end

View file

@ -6,8 +6,9 @@ class ConferencesController < ApplicationController
load_and_authorize_resource find_by: :short_title, except: :show
def index
@current = Conference.where('end_date >= ?', Date.current).reorder(start_date: :asc)
@antiquated = @conferences - @current
@current, @antiquated = Conference.reorder(start_date: :asc).all.partition do |conference|
conference.end_date >= Date.current
end
end
def show

View file

@ -6,39 +6,52 @@ class SchedulesController < ApplicationController
before_action :respond_to_options
load_resource :conference, find_by: :short_title
load_resource :program, through: :conference, singleton: true, except: :index
before_action :load_withdrawn_event_schedules, only: [:show, :events]
def show
@rooms = @conference.venue.rooms if @conference.venue
schedules = @program.selected_event_schedules
unless schedules
@event_schedules = @program.selected_event_schedules(
includes: [{ event: %i[event_type speakers submitter] }]
)
unless @event_schedules
redirect_to events_conference_schedule_path(@conference.short_title)
return
end
@events_xml = schedules.map(&:event).group_by{ |event| event.time.to_date } if schedules
@dates = @conference.start_date..@conference.end_date
@step_minutes = @program.schedule_interval.minutes
@conf_start = @conference.start_hour
@conf_period = @conference.end_hour - @conf_start
respond_to do |format|
format.xml do
@events_xml = @event_schedules.map(&:event).group_by{ |event| event.time.to_date } if @event_schedules
end
# the schedule takes you to today if it is a date of the schedule
@current_day = @conference.current_conference_day
@day = @current_day.present? ? @current_day : @dates.first
unless @current_day
# the schedule takes you to the current time if it is beetween the start and the end time.
@hour_column = @conference.hours_from_start_time(@conf_start, @conference.end_hour)
format.html do
@rooms = @conference.venue.rooms if @conference.venue
@dates = @conference.start_date..@conference.end_date
@step_minutes = @program.schedule_interval.minutes
@conf_start = @conference.start_hour
@conf_period = @conference.end_hour - @conf_start
# the schedule takes you to today if it is a date of the schedule
@current_day = @conference.current_conference_day
@day = @current_day.present? ? @current_day : @dates.first
unless @current_day
# the schedule takes you to the current time if it is beetween the start and the end time.
@hour_column = @conference.hours_from_start_time(@conf_start, @conference.end_hour)
end
# Ids of the @event_schedules of confrmed self_organized tracks along with the selected_schedule_id
@selected_schedules_ids = [@conference.program.selected_schedule_id]
@conference.program.tracks.self_organized.confirmed.each do |track|
@selected_schedules_ids << track.selected_schedule_id
end
@selected_schedules_ids.compact!
end
end
# Ids of the schedules of confrmed self_organized tracks along with the selected_schedule_id
@selected_schedules_ids = [@conference.program.selected_schedule_id]
@conference.program.tracks.self_organized.confirmed.each do |track|
@selected_schedules_ids << track.selected_schedule_id
end
@selected_schedules_ids.compact!
end
def events
@dates = @conference.start_date..@conference.end_date
@events_schedules = @program.selected_event_schedules
@events_schedules = @program.selected_event_schedules(
includes: [:room, { event: %i[track event_type speakers submitter] }]
)
@events_schedules = [] unless @events_schedules
@unscheduled_events = @program.events.confirmed - @events_schedules.map(&:event)
@ -54,4 +67,10 @@ class SchedulesController < ApplicationController
format.html { head :ok }
end if request.options?
end
def load_withdrawn_event_schedules
# Avoid making repetitive EXISTS queries for these later.
# See usage in EventsHelper#canceled_replacement_event_label
@withdrawn_event_schedules = EventSchedule.withdrawn_or_canceled_event_schedules(@program.schedule_ids)
end
end