Merge pull request #250 from Retrospring/eager-loading
Eager load relationships for questions, answers and users
This commit is contained in:
commit
2a2bd6ad14
|
@ -1,6 +1,6 @@
|
|||
class AnswerController < ApplicationController
|
||||
def show
|
||||
@answer = Answer.find(params[:id])
|
||||
@answer = Answer.includes(comments: [:user, :smiles], question: [:user], smiles: [:user]).find(params[:id])
|
||||
@display_all = true
|
||||
|
||||
if user_signed_in?
|
||||
|
|
|
@ -8,10 +8,10 @@ class DiscoverController < ApplicationController
|
|||
|
||||
top_x = 10 # only display the top X items
|
||||
|
||||
@popular_answers = Answer.where("created_at > ?", Time.now.ago(1.week)).order(:smile_count).reverse_order.limit(top_x)
|
||||
@most_discussed = Answer.where("created_at > ?", Time.now.ago(1.week)).order(:comment_count).reverse_order.limit(top_x)
|
||||
@popular_questions = Question.where("created_at > ?", Time.now.ago(1.week)).order(:answer_count).reverse_order.limit(top_x)
|
||||
@new_users = User.where("asked_count > 0").order(:id).reverse_order.limit(top_x)
|
||||
@popular_answers = Answer.where("created_at > ?", Time.now.ago(1.week)).order(:smile_count).reverse_order.limit(top_x).includes(:question, :user, :comments)
|
||||
@most_discussed = Answer.where("created_at > ?", Time.now.ago(1.week)).order(:comment_count).reverse_order.limit(top_x).includes(:question, :user, :comments)
|
||||
@popular_questions = Question.where("created_at > ?", Time.now.ago(1.week)).order(:answer_count).reverse_order.limit(top_x).includes(:user)
|
||||
@new_users = User.where("asked_count > 0").order(:id).reverse_order.limit(top_x).includes(:profile)
|
||||
|
||||
# .user = the user
|
||||
# .question_count = how many questions did the user ask
|
||||
|
|
|
@ -4,7 +4,7 @@ class UserController < ApplicationController
|
|||
before_action :authenticate_user!, only: %w(edit update edit_privacy update_privacy edit_theme update_theme preview_theme delete_theme data export begin_export edit_security update_2fa destroy_2fa reset_user_recovery_codes)
|
||||
|
||||
def show
|
||||
@user = User.where('LOWER(screen_name) = ?', params[:username].downcase).first!
|
||||
@user = User.where('LOWER(screen_name) = ?', params[:username].downcase).includes(:profile).first!
|
||||
@answers = @user.cursored_answers(last_id: params[:last_id])
|
||||
@answers_last_id = @answers.map(&:id).min
|
||||
@more_data_available = !@user.cursored_answers(last_id: @answers_last_id, size: 1).count.zero?
|
||||
|
@ -84,7 +84,7 @@ class UserController < ApplicationController
|
|||
|
||||
def followers
|
||||
@title = 'Followers'
|
||||
@user = User.where('LOWER(screen_name) = ?', params[:username].downcase).first!
|
||||
@user = User.where('LOWER(screen_name) = ?', params[:username].downcase).includes(:profile).first!
|
||||
@users = @user.cursored_followers(last_id: params[:last_id])
|
||||
@users_last_id = @users.map(&:id).min
|
||||
@more_data_available = !@user.cursored_followers(last_id: @users_last_id, size: 1).count.zero?
|
||||
|
@ -98,7 +98,7 @@ class UserController < ApplicationController
|
|||
|
||||
def friends
|
||||
@title = 'Following'
|
||||
@user = User.where('LOWER(screen_name) = ?', params[:username].downcase).first!
|
||||
@user = User.where('LOWER(screen_name) = ?', params[:username].downcase).includes(:profile).first!
|
||||
@users = @user.cursored_friends(last_id: params[:last_id])
|
||||
@users_last_id = @users.map(&:id).min
|
||||
@more_data_available = !@user.cursored_friends(last_id: @users_last_id, size: 1).count.zero?
|
||||
|
@ -112,7 +112,7 @@ class UserController < ApplicationController
|
|||
|
||||
def questions
|
||||
@title = 'Questions'
|
||||
@user = User.where('LOWER(screen_name) = ?', params[:username].downcase).first!
|
||||
@user = User.where('LOWER(screen_name) = ?', params[:username].downcase).includes(:profile).first!
|
||||
@questions = @user.cursored_questions(author_is_anonymous: false, last_id: params[:last_id])
|
||||
@questions_last_id = @questions.map(&:id).min
|
||||
@more_data_available = !@user.cursored_questions(author_is_anonymous: false, last_id: @questions_last_id, size: 1).count.zero?
|
||||
|
|
|
@ -9,5 +9,6 @@ module User::AnswerMethods
|
|||
answers
|
||||
.order(:created_at)
|
||||
.reverse_order
|
||||
.includes(comments: [:user, :smiles], question: [:user], smiles: [:user])
|
||||
end
|
||||
end
|
||||
|
|
|
@ -7,10 +7,10 @@ module User::RelationshipMethods
|
|||
define_cursor_paginator :cursored_followers, :ordered_followers
|
||||
|
||||
def ordered_friends
|
||||
friends.reverse_order
|
||||
friends.reverse_order.includes(:profile)
|
||||
end
|
||||
|
||||
def ordered_followers
|
||||
followers.reverse_order
|
||||
followers.reverse_order.includes(:profile)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -7,6 +7,6 @@ module User::TimelineMethods
|
|||
|
||||
# @return [Array] the users' timeline
|
||||
def timeline
|
||||
Answer.where('user_id in (?) OR user_id = ?', friend_ids, id).order(:created_at).reverse_order
|
||||
Answer.where('user_id in (?) OR user_id = ?', friend_ids, id).order(:created_at).reverse_order.includes(comments: [:user, :smiles], question: [:user], user: [:profile], smiles: [:user])
|
||||
end
|
||||
end
|
||||
|
|
|
@ -0,0 +1,22 @@
|
|||
require 'rails_helper'
|
||||
|
||||
describe AnswerController do
|
||||
let(:user) { FactoryBot.create :user,
|
||||
otp_module: :disabled,
|
||||
otp_secret_key: 'AAAAAAAA'}
|
||||
let(:answer) { FactoryBot.create :answer, user: user }
|
||||
|
||||
describe "#show" do
|
||||
subject { get :show, params: { username: user.screen_name, id: answer.id } }
|
||||
|
||||
context "user signed in" do
|
||||
before(:each) { sign_in user }
|
||||
|
||||
it "renders the answer/show template" do
|
||||
subject
|
||||
expect(assigns(:answer)).to eq(answer)
|
||||
expect(response).to render_template("answer/show")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -7,13 +7,69 @@ describe UserController, type: :controller do
|
|||
otp_module: :disabled,
|
||||
otp_secret_key: 'EJFNIJPYXXTCQSRTQY6AG7XQLAT2IDG5H7NGLJE3'}
|
||||
|
||||
describe "#show" do
|
||||
subject { get :show, params: { username: user.screen_name } }
|
||||
|
||||
context "user signed in" do
|
||||
before(:each) { sign_in user }
|
||||
|
||||
it "renders the user/show template" do
|
||||
subject
|
||||
expect(assigns(:user)).to eq(user)
|
||||
expect(response).to render_template("user/show")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "#followers" do
|
||||
subject { get :followers, params: { username: user.screen_name } }
|
||||
|
||||
context "user signed in" do
|
||||
before(:each) { sign_in user }
|
||||
|
||||
it "renders the user/show_follow template" do
|
||||
subject
|
||||
expect(assigns(:user)).to eq(user)
|
||||
expect(response).to render_template("user/show_follow")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "#friends" do
|
||||
subject { get :friends, params: { username: user.screen_name } }
|
||||
|
||||
context "user signed in" do
|
||||
before(:each) { sign_in user }
|
||||
|
||||
it "renders the user/show_follow template" do
|
||||
subject
|
||||
expect(assigns(:user)).to eq(user)
|
||||
expect(response).to render_template("user/show_follow")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "#questions" do
|
||||
subject { get :questions, params: { username: user.screen_name } }
|
||||
|
||||
context "user signed in" do
|
||||
before(:each) { sign_in user }
|
||||
|
||||
it "renders the user/questions template" do
|
||||
subject
|
||||
expect(assigns(:user)).to eq(user)
|
||||
expect(response).to render_template("user/questions")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "#edit" do
|
||||
subject { get :edit }
|
||||
|
||||
context "user signed in" do
|
||||
before(:each) { sign_in user }
|
||||
|
||||
it "renders the edit_profile_picture template" do
|
||||
it "renders the user/edit template" do
|
||||
subject
|
||||
expect(response).to render_template("user/edit")
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue