Extract identical preload call

This commit is contained in:
Matt Jankowski 2024-09-17 15:25:17 -04:00
parent a72d8a6132
commit bdc9e2f230

View File

@ -116,18 +116,25 @@ class AccountSearchQuery
def search
Account.find_by_sql([BASIC_SEARCH_SQL, { limit: @limit, offset: @offset, tsquery: generate_query_for_search }]).tap do |records|
ActiveRecord::Associations::Preloader.new(records: records, associations: [:account_stat, { user: :role }]).call
preload(records)
end
end
def advanced_search(account, following: false)
Account.find_by_sql([advanced_sql_template(following), { id: account.id, limit: @limit, offset: @offset, tsquery: generate_query_for_search }]).tap do |records|
ActiveRecord::Associations::Preloader.new(records: records, associations: [:account_stat, { user: :role }]).call
preload(records)
end
end
private
def preload(records)
ActiveRecord::Associations::Preloader.new(
records: records,
associations: [:account_stat, { user: :role }]
).call
end
def generate_query_for_search
# The final ":*" is for prefix search.
# The trailing space does not seem to fit any purpose, but `to_tsquery`