2022-02-25 00:34:14 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class Trends::Query
|
|
|
|
include Enumerable
|
|
|
|
|
2024-12-04 12:59:17 +01:00
|
|
|
attr_reader :klass, :loaded
|
2022-02-25 00:34:14 +01:00
|
|
|
|
|
|
|
alias loaded? loaded
|
|
|
|
|
2024-12-04 12:59:17 +01:00
|
|
|
def initialize(_prefix, klass)
|
2022-02-25 00:34:14 +01:00
|
|
|
@klass = klass
|
|
|
|
@records = []
|
|
|
|
@loaded = false
|
|
|
|
@allowed = false
|
2024-12-04 12:59:17 +01:00
|
|
|
@account = nil
|
2022-04-08 17:10:53 +02:00
|
|
|
@limit = nil
|
|
|
|
@offset = nil
|
2022-02-25 00:34:14 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def allowed!
|
|
|
|
@allowed = true
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
def allowed
|
|
|
|
clone.allowed!
|
|
|
|
end
|
|
|
|
|
2024-12-04 12:59:17 +01:00
|
|
|
def filtered_for!(account)
|
|
|
|
@account = account
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
def filtered_for(account)
|
|
|
|
clone.filtered_for!(account)
|
|
|
|
end
|
|
|
|
|
2022-02-25 00:34:14 +01:00
|
|
|
def in_locale!(value)
|
|
|
|
@locale = value
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
def in_locale(value)
|
|
|
|
clone.in_locale!(value)
|
|
|
|
end
|
|
|
|
|
|
|
|
def offset!(value)
|
2022-03-26 00:26:50 +01:00
|
|
|
@offset = value.to_i
|
2022-02-25 00:34:14 +01:00
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
def offset(value)
|
|
|
|
clone.offset!(value)
|
|
|
|
end
|
|
|
|
|
|
|
|
def limit!(value)
|
2022-03-26 00:26:50 +01:00
|
|
|
@limit = value.to_i
|
2022-02-25 00:34:14 +01:00
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
def limit(value)
|
|
|
|
clone.limit!(value)
|
|
|
|
end
|
|
|
|
|
|
|
|
def records
|
|
|
|
load
|
|
|
|
@records
|
|
|
|
end
|
|
|
|
|
2022-04-07 18:06:15 +02:00
|
|
|
delegate :each, :empty?, :first, :last, :size, to: :records
|
2022-02-25 00:34:14 +01:00
|
|
|
|
|
|
|
def to_ary
|
|
|
|
records.dup
|
|
|
|
end
|
|
|
|
|
|
|
|
alias to_a to_ary
|
|
|
|
|
|
|
|
def to_arel
|
2024-12-04 12:59:17 +01:00
|
|
|
raise NotImplementedError
|
2022-02-25 00:34:14 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def load
|
|
|
|
unless loaded?
|
|
|
|
@records = perform_queries
|
|
|
|
@loaded = true
|
|
|
|
end
|
|
|
|
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
def perform_queries
|
2024-12-04 12:59:17 +01:00
|
|
|
to_arel.to_a
|
2022-02-25 00:34:14 +01:00
|
|
|
end
|
|
|
|
|
2024-12-04 12:59:17 +01:00
|
|
|
def preferred_languages
|
|
|
|
if @account&.chosen_languages.present?
|
|
|
|
@account.chosen_languages
|
|
|
|
else
|
|
|
|
@locale
|
|
|
|
end
|
2022-02-25 00:34:14 +01:00
|
|
|
end
|
|
|
|
end
|