Sha256: fb005b27c4ae2fd11fbdb8be5ea4896d9a42413bdd4c2ecd3ec5a20909527e25

Contents?: true

Size: 1.73 KB

Versions: 81

Compression:

Stored size: 1.73 KB

Contents

# frozen_string_literal: true

require_dependency "renalware/letters"

# This class is responsible for transforming the attributes
# of a recipient. The resulting attributes can then
# be mass assigned to an ActiveRecord recipient object.
module Renalware
  module Letters
    class RecipientParamsProcessor
      def initialize(patient)
        @patient = patient
      end

      def call(params)
        params =
          if can_have_contact_as_addressee?(params)
            params = put_contact_as_addressee(params)
            translate_keep_flag_to_nested_attributes_destroy_flag(params)
          else
            clear_addressee(params)
          end

        remove_addressee_id(params)
      end

      private

      def can_have_contact_as_addressee?(params)
        params[:person_role] == "contact"
      end

      def put_contact_as_addressee(params)
        params.merge(addressee: fetch_contact(params))
      end

      def remove_addressee_id(params)
        params.except(:addressee_id)
      end

      def clear_addressee(params)
        params.merge(addressee: nil)
      end

      def translate_keep_flag_to_nested_attributes_destroy_flag(params)
        # ActiveRecord checks "_destroy" attribute to delete an existing
        # record when mass assigning nested attributes.
        # In our form, we need to use to "check" the contacts to be
        # assigned as CC's (not to destroy them).  So we therefore convert
        # the "_keep" flag to a "_destroy" one.
        params[:_destroy] = params[:_keep] == "1" ? "0" : "1"
        params.except(:_keep)
      end

      def fetch_contact(params)
        return if params[:addressee_id].blank?

        @patient.contacts.find_by!(id: params[:addressee_id])
      end
    end
  end
end

Version data entries

81 entries across 81 versions & 1 rubygems

Version Path
renalware-core-2.0.153 app/models/renalware/letters/recipient_params_processor.rb
renalware-core-2.0.152 app/models/renalware/letters/recipient_params_processor.rb
renalware-core-2.0.151 app/models/renalware/letters/recipient_params_processor.rb
renalware-core-2.0.149 app/models/renalware/letters/recipient_params_processor.rb
renalware-core-2.0.148 app/models/renalware/letters/recipient_params_processor.rb
renalware-core-2.0.147 app/models/renalware/letters/recipient_params_processor.rb
renalware-core-2.0.146 app/models/renalware/letters/recipient_params_processor.rb
renalware-core-2.0.145 app/models/renalware/letters/recipient_params_processor.rb
renalware-core-2.0.144 app/models/renalware/letters/recipient_params_processor.rb
renalware-core-2.0.143 app/models/renalware/letters/recipient_params_processor.rb
renalware-core-2.0.142 app/models/renalware/letters/recipient_params_processor.rb
renalware-core-2.0.141 app/models/renalware/letters/recipient_params_processor.rb
renalware-core-2.0.140 app/models/renalware/letters/recipient_params_processor.rb
renalware-core-2.0.139 app/models/renalware/letters/recipient_params_processor.rb
renalware-core-2.0.138 app/models/renalware/letters/recipient_params_processor.rb
renalware-core-2.0.137 app/models/renalware/letters/recipient_params_processor.rb
renalware-core-2.0.136 app/models/renalware/letters/recipient_params_processor.rb
renalware-core-2.0.135 app/models/renalware/letters/recipient_params_processor.rb
renalware-core-2.0.134 app/models/renalware/letters/recipient_params_processor.rb
renalware-core-2.0.133 app/models/renalware/letters/recipient_params_processor.rb