clowne-rb/clowne

View on GitHub
lib/clowne/adapters/active_record/associations/has_one.rb

Summary

Maintainability
A
0 mins
Test Coverage
# frozen_string_literal: true

module Clowne
  module Adapters # :nodoc: all
    class ActiveRecord
      module Associations
        class HasOne < Base
          def call(record)
            child = association
            return record unless child

            unless declaration.scope.nil?
              warn(
                "[Clowne] Has one association does not support scopes " \
                "(#{@association_name} for #{@source.class})"
              )
            end

            child_clone = clone_one(child)
            child_clone[:"#{reflection.foreign_key}"] = nil
            record.__send__(:"#{association_name}=", child_clone)

            record
          end
          # rubocop: enable Metrics/MethodLength
        end
      end
    end
  end
end