Sha256: 5a6d169b9c82accf7c314e86411e7b3b1efcb5b8b06b64b9f5e69c851b30cae8

Contents?: true

Size: 1.73 KB

Versions: 1

Compression:

Stored size: 1.73 KB

Contents

# frozen_string_literal: true

module Lcms
  module Engine
    # Usage:
    #   @materials = AdminMaterialsQuery.call(query_params, page: params[:page])
    class AdminMaterialsQuery < BaseQuery
      STRICT_METADATA = %w(grade subject).freeze

      # Returns: ActiveRecord relation
      def call
        @scope = Material.all # initial scope
        search_by_identifier
        search_by_file_name

        filter_by_metadata

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

      private

      def filter_by_metadata
        metadata_keys.each do |key|
          next unless q[key].present?

          @scope =
            if STRICT_METADATA.include?(key.to_s)
              @scope.where_metadata(key => q[key])
            else
              @scope.where_metadata_like(key, q[key])
            end
        end
      end

      def metadata_keys
        DocTemplate.config.dig('metadata', 'service').constantize.materials_metadata.attribute_set.map(&:name)
      end

      def search_by_identifier
        # we need the `with_pg_search_rank` scope for this to work with DISTINCT
        # See more on: https://github.com/Casecommons/pg_search/issues/238
        @scope = @scope.search_identifier(q.search_term).with_pg_search_rank if q.search_term.present?
      end

      def search_by_file_name
        @scope = @scope.search_name(q.search_file_name).with_pg_search_rank if q.search_file_name.present?
      end

      def sorted_scope
        @scope = @scope.reorder(:identifier) if q.sort_by.blank? || q.sort_by == 'identifier'
        @scope = @scope.reorder(updated_at: :desc) if q.sort_by == 'last_update'
        @scope.distinct
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
lcms-engine-0.4.0 app/queries/lcms/engine/admin_materials_query.rb