2023-03-02 19:46:21 -06:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class TopicsFilter
|
2023-03-21 21:04:57 -05:00
|
|
|
def initialize(guardian:, scope: Topic, category_id: nil)
|
|
|
|
@guardian = guardian
|
|
|
|
@scope = scope
|
|
|
|
@category = category_id.present? ? Category.find_by(id: category_id) : nil
|
2023-03-02 19:46:21 -06:00
|
|
|
end
|
|
|
|
|
2023-03-21 21:04:57 -05:00
|
|
|
def filter(status: nil)
|
|
|
|
filter_status(@scope, status) if status
|
|
|
|
@scope
|
2023-03-02 19:46:21 -06:00
|
|
|
end
|
|
|
|
|
2023-03-21 21:04:57 -05:00
|
|
|
private
|
|
|
|
|
|
|
|
def filter_status(scope, status)
|
|
|
|
case status
|
2023-03-02 19:46:21 -06:00
|
|
|
when "open"
|
2023-03-21 21:04:57 -05:00
|
|
|
@scope = @scope.where("NOT topics.closed AND NOT topics.archived")
|
2023-03-02 19:46:21 -06:00
|
|
|
when "closed"
|
2023-03-21 21:04:57 -05:00
|
|
|
@scope = @scope.where("topics.closed")
|
2023-03-02 19:46:21 -06:00
|
|
|
when "archived"
|
2023-03-21 21:04:57 -05:00
|
|
|
@scope = @scope.where("topics.archived")
|
2023-03-02 19:46:21 -06:00
|
|
|
when "deleted"
|
|
|
|
if @guardian.can_see_deleted_topics?(@category)
|
2023-03-21 21:04:57 -05:00
|
|
|
@scope = @scope.unscope(where: :deleted_at).where("topics.deleted_at IS NOT NULL")
|
2023-03-02 19:46:21 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|