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