Sha256: 1caf3209f9bae97f9efba0dc1f9c2f503919e08f023fb00b1ea9ec848b4a4e40

Contents?: true

Size: 1.45 KB

Versions: 15

Compression:

Stored size: 1.45 KB

Contents

# frozen_string_literal: true

require_dependency "renalware/messaging"

# Builds a new MessageForm form object. MessageForm is used behind the html form when displaying a
# `Send Message` Dialog the first time. Note that we don't use this builder again on e.g.
# form submission - at that point all the required params are in the form payload - this is
# only for the initial MesageForm creation.
#
module Renalware
  module Messaging
    module Internal
      class MessageFormBuilder
        attr_reader :patient, :params

        def initialize(patient:, params:)
          @patient = patient
          @params = params
        end

        def call
          MessageForm.new(
            subject: build_subject,
            recipient_ids: build_recipient_ids,
            replying_to_message_id: replying_to_message_id,
            public: true
          )
        end

        private

        def build_subject
          replying? ? "Re: #{replying_to_message.subject}" : patient.to_s
        end

        def build_recipient_ids
          replying? ? Array(replying_to_message.author_id) : []
        end

        def replying_to_message
          return NullObject.instance unless replying?

          @replying_to_message ||= Message.find(replying_to_message_id)
        end

        def replying_to_message_id
          params[:replying_to_message_id]
        end

        def replying?
          replying_to_message_id.present?
        end
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
renalware-core-2.1.1 app/models/renalware/messaging/internal/message_form_builder.rb
renalware-core-2.1.0 app/models/renalware/messaging/internal/message_form_builder.rb
renalware-core-2.0.167 app/models/renalware/messaging/internal/message_form_builder.rb
renalware-core-2.0.166 app/models/renalware/messaging/internal/message_form_builder.rb
renalware-core-2.0.165 app/models/renalware/messaging/internal/message_form_builder.rb
renalware-core-2.0.164 app/models/renalware/messaging/internal/message_form_builder.rb
renalware-core-2.0.163 app/models/renalware/messaging/internal/message_form_builder.rb
renalware-core-2.0.162 app/models/renalware/messaging/internal/message_form_builder.rb
renalware-core-2.0.161 app/models/renalware/messaging/internal/message_form_builder.rb
renalware-core-2.0.160 app/models/renalware/messaging/internal/message_form_builder.rb
renalware-core-2.0.159 app/models/renalware/messaging/internal/message_form_builder.rb
renalware-core-2.0.158 app/models/renalware/messaging/internal/message_form_builder.rb
renalware-core-2.0.157 app/models/renalware/messaging/internal/message_form_builder.rb
renalware-core-2.0.156 app/models/renalware/messaging/internal/message_form_builder.rb
renalware-core-2.0.155 app/models/renalware/messaging/internal/message_form_builder.rb