Sha256: 49cee5321ae894cb6df309f3ee04b1376f1f30d640b0cc381fe373d862168d3b

Contents?: true

Size: 529 Bytes

Versions: 7

Compression:

Stored size: 529 Bytes

Contents

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

7 entries across 7 versions & 1 rubygems

Version Path
active_mocker-2.1.3 lib/active_mocker/mock/belongs_to.rb
active_mocker-2.1.2 lib/active_mocker/mock/belongs_to.rb
active_mocker-2.1.1 lib/active_mocker/mock/belongs_to.rb
active_mocker-2.1.0 lib/active_mocker/mock/belongs_to.rb
active_mocker-2.0.0 lib/active_mocker/mock/belongs_to.rb
active_mocker-2.0.0.rc1 lib/active_mocker/mock/belongs_to.rb
active_mocker-2.0.0.pre1 lib/active_mocker/mock/belongs_to.rb