Sha256: 0a1e95e50681471a529554d47cf73aecb60523a47cd03558d9902b0042c1d090

Contents?: true

Size: 1.52 KB

Versions: 2

Compression:

Stored size: 1.52 KB

Contents

# frozen_string_literal: true

module Lcms
  module Engine
    # Usage:
    #   @documents = AdminDocumentsQuery.call(query_params, page: params[:page])
    #
    class AdminDocumentsQuery < BaseQuery
      # Returns: ActiveRecord relation
      def call
        @scope = Document.includes(:materials, :resource).all # initial scope
        apply_filters

        if @pagination.present?
          sorted_scope.paginate(page: @pagination[:page])
        else
          sorted_scope
        end
      end

      private

      def apply_filters # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
        @scope = @scope.actives unless q.inactive == '1'
        @scope = @scope.failed if q.only_failed == '1'
        @scope = @scope.filter_by_term(q.search_term) if q.search_term.present?
        @scope = @scope.filter_by_subject(q.subject) if q.subject.present?
        @scope = @scope.filter_by_grade(q.grade) if q.grade.present?
        @scope = @scope.filter_by_module(q.module) if q.module.present?
        @scope = @scope.filter_by_unit(q.unit) if q.unit.present?
        @scope = @scope.with_broken_materials if q.broken_materials == '1'
        @scope = @scope.with_updated_materials if q.reimport_required == '1'
        @scope
      end

      def sorted_scope
        @scope = @scope.order_by_curriculum if q.sort_by.blank? || q.sort_by == 'curriculum'
        @scope = @scope.order(updated_at: :desc) if q.sort_by == 'last_update'
        @scope.distinct.order(active: :desc)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
lcms-engine-0.3.0 app/queries/lcms/engine/admin_documents_query.rb
lcms-engine-0.2.0 app/queries/lcms/engine/admin_documents_query.rb