Sha256: 39b1707af5fd07657585e9319c4d730e78eb8825575862a7f208e836b3552afa

Contents?: true

Size: 1.22 KB

Versions: 149

Compression:

Stored size: 1.22 KB

Contents

# frozen_string_literal: true

require_dependency "renalware/hd"

module Renalware
  module HD
    class PatientsWithUnmetPreferencesQuery
      def initialize(query = {})
        @query = query
      end

      def call
        search.result
      end

      def search
        HD::Patient
          .extend(Scopes)
          .includes(hd_profile: :hospital_unit)
          .includes(hd_preference_set: :hospital_unit)
          .eager_load(:hd_profile)
          .eager_load(:hd_preference_set)
          .having_an_unmet_preference
          .ransack(query)
      end

      private

      attr_reader :query

      module Scopes
        def having_an_unmet_preference
          sql = <<-SQL.squish
            (hd_preference_sets.hospital_unit_id > 0 AND
              hd_profiles.hospital_unit_id != hd_preference_sets.hospital_unit_id) OR
            (coalesce(hd_preference_sets.schedule_definition_id, 0) != 0 AND
              hd_profiles.schedule_definition_id != hd_preference_sets.schedule_definition_id) OR
            (coalesce(hd_preference_sets.other_schedule, '') != '' AND
              hd_profiles.other_schedule != hd_preference_sets.other_schedule)
          SQL
          where(sql)
        end
      end
    end
  end
end

Version data entries

149 entries across 149 versions & 1 rubygems

Version Path
renalware-core-2.0.147 app/models/renalware/hd/patients_with_unmet_preferences_query.rb
renalware-core-2.0.146 app/models/renalware/hd/patients_with_unmet_preferences_query.rb
renalware-core-2.0.145 app/models/renalware/hd/patients_with_unmet_preferences_query.rb
renalware-core-2.0.144 app/models/renalware/hd/patients_with_unmet_preferences_query.rb
renalware-core-2.0.143 app/models/renalware/hd/patients_with_unmet_preferences_query.rb
renalware-core-2.0.142 app/models/renalware/hd/patients_with_unmet_preferences_query.rb
renalware-core-2.0.141 app/models/renalware/hd/patients_with_unmet_preferences_query.rb
renalware-core-2.0.140 app/models/renalware/hd/patients_with_unmet_preferences_query.rb
renalware-core-2.0.139 app/models/renalware/hd/patients_with_unmet_preferences_query.rb
renalware-core-2.0.138 app/models/renalware/hd/patients_with_unmet_preferences_query.rb
renalware-core-2.0.137 app/models/renalware/hd/patients_with_unmet_preferences_query.rb
renalware-core-2.0.136 app/models/renalware/hd/patients_with_unmet_preferences_query.rb
renalware-core-2.0.135 app/models/renalware/hd/patients_with_unmet_preferences_query.rb
renalware-core-2.0.134 app/models/renalware/hd/patients_with_unmet_preferences_query.rb
renalware-core-2.0.133 app/models/renalware/hd/patients_with_unmet_preferences_query.rb
renalware-core-2.0.132 app/models/renalware/hd/patients_with_unmet_preferences_query.rb
renalware-core-2.0.131 app/models/renalware/hd/patients_with_unmet_preferences_query.rb
renalware-core-2.0.130 app/models/renalware/hd/patients_with_unmet_preferences_query.rb
renalware-core-2.0.129 app/models/renalware/hd/patients_with_unmet_preferences_query.rb
renalware-core-2.0.128 app/models/renalware/hd/patients_with_unmet_preferences_query.rb