From 79e14e764fd5c7e09e1a319f751fc24cb6c19a3b Mon Sep 17 00:00:00 2001 From: Andrew Brown Date: Mon, 24 Sep 2018 01:32:26 -0500 Subject: [PATCH] cut contributor queries in half --- app/controllers/application_controller.rb | 16 +++++++++++----- app/models/user.rb | 5 ++--- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index be2a9ba4..5cbba2b1 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,6 +1,9 @@ class ApplicationController < ActionController::Base protect_from_forgery + before_action :cache_most_used_page_information + + # todo name all these methods before_action do if params[:universe].present? && user_signed_in? if params[:universe] == 'all' @@ -28,14 +31,17 @@ class ApplicationController < ActionController::Base @page_description ||= 'Notebook.ai is a set of tools for writers, game designers, and roleplayers to create magnificent universes — and everything within them.' end - before_action do + + def content_type_from_controller(content_controller_name) + content_controller_name.to_s.chomp('Controller').singularize.constantize + end + + private + + def cache_most_used_page_information @activated_content_types = ( Rails.application.config.content_types[:all].map(&:name) & # Use config to dictate order current_user.user_content_type_activators.pluck(:content_type) ) end - - def content_type_from_controller(content_controller_name) - content_controller_name.to_s.chomp('Controller').singularize.constantize - end end diff --git a/app/models/user.rb b/app/models/user.rb index 85cfb619..2ffb11ba 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -38,10 +38,9 @@ class User < ApplicationRecord def contributable_universes @user_contributable_universes ||= begin # todo email confirmation needs to happy for data safety / privacy (only verified emails) - contributor_by_email = Contributor.where(email: self.email).pluck(:universe_id) - contributor_by_user = Contributor.where(user: self).pluck(:universe_id) + contributor_ids = Contributor.where('email = ? OR user_id = ?', self.email, self.id).pluck(:universe_id) - Universe.where(id: contributor_by_email + contributor_by_user) + Universe.where(id: contributor_ids) end end #TODO: rename this to #{content_type}_shared_with_me and only return contributable content that others own