Sha256: 8c7dd2462f04136af39fd57f36e97d2ae71ae10507c591b1187d38567f564c9b

Contents?: true

Size: 1.42 KB

Versions: 87

Compression:

Stored size: 1.42 KB

Contents

# frozen_string_literal: true

require_dependency "renalware/letters"

# Represents an Electronic CC - a reference to a letter sent to a recipient (another user on the
# system) as a 'CC'. We use the concept of a 'receipt' which, while mainly being a cross table
# between letter and recipient user, is also a well-named place to store related information
# for example whether the message has been read (e.g. the letter viewed).
module Renalware
  module Letters
    class ElectronicReceipt < ApplicationRecord
      belongs_to :letter, touch: true
      belongs_to :recipient, class_name: "Renalware::User"
      validates :letter, presence: true
      validates :recipient_id, presence: true

      # Merge scope here to make sure we only get approved letters
      scope :sent_by, lambda { |user_id|
        joins(:letter)
          .merge(Letter::Approved.all)
          .where(letter_letters: { author_id: user_id })
      }
      scope :unread, lambda {
        where(read_at: nil)
          .joins(:letter)
          .merge(Letter.approved_or_completed)
      }
      scope :read, lambda {
        where.not(read_at: nil)
          .joins(:letter)
          .merge(Letter.approved_or_completed)
      }
      scope :for_recipient, ->(user_id) { where(recipient_id: user_id) }
      scope :ordered, -> { order(created_at: :desc) }

      def read?
        read_at.present?
      end

      def unread?
        !read?
      end
    end
  end
end

Version data entries

87 entries across 87 versions & 1 rubygems

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