Sha256: 7d15a32d9d982bad2c576e95ccf46114d3723b6e1bef00fe5b5b2ea672bcd0b0

Contents?: true

Size: 1.53 KB

Versions: 98

Compression:

Stored size: 1.53 KB

Contents

# frozen_string_literal: true

require_dependency "renalware/admissions"

module Renalware
  module Patients
    class BookmarksQuery
      attr_reader :default_relation, :query

      def initialize(default_relation:, params: nil)
        @default_relation = default_relation
        @query = params || {}
        @query[:s] ||= "created_at desc"
      end

      def call
        search.result
      end

      # Note we *MUST* join onto patients for PatientsRansackHelper.identity_match to work.
      # It might be better to refactor PatientsRansackHelper so we can include where required
      # eg below using .extending(PatientsRansackHelper) rather than relying on it being in
      # included in the model file.
      # note that adding .includes(:created_by) here creates an ambigous column
      # 'family_name' error
      def search
        @search ||= begin
          (default_relation || Bookmark)
            .extend(RansackScopes)
            .joins(:patient)
            .eager_load(patient: [current_modality: :description])
            .order(created_at: :desc)
            .ransack(query)
        end
      end

      module RansackScopes
        def self.extended(base)
          # Using a custom ransacker here in order to sort by modality description name
          # because using a predicate like  :patient_current_modality_description_name
          # results in an INNER JOIN onto modalities.
          base.ransacker :modality_desc do
            Arel.sql("modality_descriptions.name")
          end
        end
      end
    end
  end
end

Version data entries

98 entries across 98 versions & 1 rubygems

Version Path
renalware-core-2.1.1 app/models/renalware/patients/bookmarks_query.rb
renalware-core-2.1.0 app/models/renalware/patients/bookmarks_query.rb
renalware-core-2.0.167 app/models/renalware/patients/bookmarks_query.rb
renalware-core-2.0.166 app/models/renalware/patients/bookmarks_query.rb
renalware-core-2.0.165 app/models/renalware/patients/bookmarks_query.rb
renalware-core-2.0.164 app/models/renalware/patients/bookmarks_query.rb
renalware-core-2.0.163 app/models/renalware/patients/bookmarks_query.rb
renalware-core-2.0.162 app/models/renalware/patients/bookmarks_query.rb
renalware-core-2.0.161 app/models/renalware/patients/bookmarks_query.rb
renalware-core-2.0.160 app/models/renalware/patients/bookmarks_query.rb
renalware-core-2.0.159 app/models/renalware/patients/bookmarks_query.rb
renalware-core-2.0.158 app/models/renalware/patients/bookmarks_query.rb
renalware-core-2.0.157 app/models/renalware/patients/bookmarks_query.rb
renalware-core-2.0.156 app/models/renalware/patients/bookmarks_query.rb
renalware-core-2.0.155 app/models/renalware/patients/bookmarks_query.rb
renalware-core-2.0.153 app/models/renalware/patients/bookmarks_query.rb
renalware-core-2.0.152 app/models/renalware/patients/bookmarks_query.rb
renalware-core-2.0.151 app/models/renalware/patients/bookmarks_query.rb
renalware-core-2.0.149 app/models/renalware/patients/bookmarks_query.rb
renalware-core-2.0.148 app/models/renalware/patients/bookmarks_query.rb