Refactored code to reduce number of database queries (#960)

This commit is contained in:
Ahmad Farhat
2020-02-24 12:31:30 -05:00
committed by GitHub
parent 92da1f6f87
commit 2cc1fdf281
14 changed files with 81 additions and 52 deletions

View File

@ -42,27 +42,21 @@ class Room < ApplicationRecord
search_param = "%#{string}%"
joins(:owner).where(search_query, search: search_param)
where(search_query, search: search_param)
end
def self.admins_order(column, direction)
# Include the owner of the table
table = joins(:owner)
return table.order(Arel.sql("#{column} #{direction}")) if table.column_names.include?(column) || column == "users.name"
return table.order(Arel.sql("rooms.#{column} #{direction}")) if table.column_names.include?(column) || column == "users.name"
table
end
# Determines if a user owns a room.
def owned_by?(user)
return false if user.nil?
user.rooms.include?(self)
end
# Determines whether room is a home room
def home_room?
owner.main_room == self
user_id == user&.id
end
def shared_users