2019-09-16 12:27:29 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-02-24 23:34:14 +00:00
|
|
|
class Trends::TagFilter
|
2020-01-20 14:55:03 +00:00
|
|
|
KEYS = %i(
|
2021-11-25 12:07:38 +00:00
|
|
|
trending
|
|
|
|
status
|
2020-01-20 14:55:03 +00:00
|
|
|
).freeze
|
|
|
|
|
2019-09-16 12:27:29 +00:00
|
|
|
attr_reader :params
|
|
|
|
|
|
|
|
def initialize(params)
|
|
|
|
@params = params
|
|
|
|
end
|
|
|
|
|
|
|
|
def results
|
2021-11-25 12:07:38 +00:00
|
|
|
scope = begin
|
|
|
|
if params[:status] == 'pending_review'
|
|
|
|
Tag.unscoped
|
|
|
|
else
|
|
|
|
trending_scope
|
|
|
|
end
|
|
|
|
end
|
2019-09-16 12:27:29 +00:00
|
|
|
|
|
|
|
params.each do |key, value|
|
|
|
|
next if key.to_s == 'page'
|
|
|
|
|
|
|
|
scope.merge!(scope_for(key, value.to_s.strip)) if value.present?
|
|
|
|
end
|
|
|
|
|
2021-11-25 12:07:38 +00:00
|
|
|
scope
|
2019-09-16 12:27:29 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def scope_for(key, value)
|
|
|
|
case key.to_s
|
2021-11-25 12:07:38 +00:00
|
|
|
when 'status'
|
|
|
|
status_scope(value)
|
2019-09-16 12:27:29 +00:00
|
|
|
else
|
|
|
|
raise "Unknown filter: #{key}"
|
|
|
|
end
|
|
|
|
end
|
2021-11-25 12:07:38 +00:00
|
|
|
|
|
|
|
def trending_scope
|
2022-02-24 23:34:14 +00:00
|
|
|
Trends.tags.query.to_arel
|
2021-11-25 12:07:38 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def status_scope(value)
|
|
|
|
case value.to_s
|
|
|
|
when 'approved'
|
|
|
|
Tag.trendable
|
|
|
|
when 'rejected'
|
|
|
|
Tag.not_trendable
|
|
|
|
when 'pending_review'
|
|
|
|
Tag.pending_review
|
|
|
|
else
|
|
|
|
raise "Unknown status: #{value}"
|
|
|
|
end
|
|
|
|
end
|
2019-09-16 12:27:29 +00:00
|
|
|
end
|