Sha256: 80975b8e01db113262db3baaf6312cfcb72ee745c3df582f0568974f23bded5a

Contents?: true

Size: 558 Bytes

Versions: 31

Compression:

Stored size: 558 Bytes

Contents

# frozen_string_literal: true
module ActiveMocker
  class BelongsTo < SingleRelation
    attr_reader :item

    def initialize(item, child_self:, foreign_key:)
      save_item(item, child_self)
      assign_foreign_key(child_self, foreign_key, item.try(:id))
      super
    end

    private

    def assign_foreign_key(child_self, foreign_key, foreign_id)
      child_self.send(:write_attribute, foreign_key, foreign_id)
    end

    def save_item(item, child_self)
      return if item.nil?
      item.try(:save) if child_self.persisted?
    end
  end
end

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
active_mocker-2.4.0.pre1 lib/active_mocker/mock/belongs_to.rb
active_mocker-2.3.3 lib/active_mocker/mock/belongs_to.rb
active_mocker-2.3.2 lib/active_mocker/mock/belongs_to.rb
active_mocker-2.3.1 lib/active_mocker/mock/belongs_to.rb
active_mocker-2.3.0 lib/active_mocker/mock/belongs_to.rb
active_mocker-2.2.5 lib/active_mocker/mock/belongs_to.rb
active_mocker-2.2.4 lib/active_mocker/mock/belongs_to.rb
active_mocker-2.2.3 lib/active_mocker/mock/belongs_to.rb
active_mocker-2.2.2 lib/active_mocker/mock/belongs_to.rb
active_mocker-2.2.1 lib/active_mocker/mock/belongs_to.rb
active_mocker-2.2.0 lib/active_mocker/mock/belongs_to.rb