Home > front end >  Delegated type with factory bot, mutually dependent foreign key issue
Delegated type with factory bot, mutually dependent foreign key issue

Time:05-04

I am trying to figure out how to create Factory objects in the right order so that the delegated class has access to it's parent model. Here are my models:

class Alert < ApplicationRecord
  delegated_type :alertable, types: %w[QuotaAlert]
  delegate :trigger_percent, :trigger_percent=, :quota, :quota_public_id, to: :quota_alert
  accepts_nested_attributes_for :alertable, update_only: true
module Alertable
  extend ActiveSupport::Concern

  included do
    has_one :alert, as: :alertable, touch: true
    delegate :company, :public_id, :channel, to: :alert
class QuotaAlert < ApplicationRecord
  validates_presence_of :trigger_percent
  validates_inclusion_of :trigger_percent, in: 0..1

  belongs_to :quota

  include Alertable
end

And my factory:

FactoryBot.define do
  factory :alert do
    company
    channel { 'email' }

    trait :quota_alert_always_trigger do
      before(:create) do |alert|
        alert.alertable = create :quota_alert, trigger_percent: 0
      end
    end

However, the delegation of company to alert.company is complaining because it says quota_alert.alert is nil. I think I need to change the ordering of how my factory is creating the objects, but I can't quite figure out the right order.

QuotaAlert requires alert to be already created to access alert.company, however, alert can't be created without QuotaAlert.

The actual error is: Module::DelegationError: QuotaAlert#company delegated to alert.company, but alert is nil

What's the right way to do this?

UPDATE:

# frozen_string_literal: true

FactoryBot.define do
  factory :quota_alert do
    quota { create :quota, :with_random_current_count }
    trigger_percent { rand }
  end
end

UPDATE 2: here's the only other factory that gets touched:

FactoryBot.define do
  factory :quota do
    limit_amount { rand(5000..10_000) }
    name { Faker::Coffee.blend_name }
    feature
    company
    product
    customer
    subscription

    trait :with_random_current_count do
      after(:create) do |quota|
        quota.set_current_count! rand(1..1000)
      end
    end
  end
end

CodePudding user response:

# app/models/*.rb

class Company < ApplicationRecord; end
class Quota < ApplicationRecord; end

class Alert < ApplicationRecord
  belongs_to :company

  delegated_type :alertable, types: %w[QuotaAlert]
  # should delegate to `alertable`; otherwise delegated type setup seems unnecessary
  delegate :trigger_percent, :trigger_percent=, :quota, :quota_public_id, to: :alertable

  accepts_nested_attributes_for :alertable, update_only: true
end

class QuotaAlert < ApplicationRecord
  # include Alertable
  has_one :alert, as: :alertable, touch: true
  delegate :company, :public_id, :channel, to: :alert

  belongs_to :quota

  validates :trigger_percent, presence: true, inclusion: { in: 0..1 }
end
# spec/factories/alerts.rb

FactoryBot.define do
  factory :quota
  factory :company

  factory :quota_alert do
    quota
    trigger_percent { 0.2 }
  end

  factory :alert do
    company
    channel { 'email' }
    association :alertable, factory: :quota_alert
  end
end
# spec/fatories_spec.rb

require 'rails_helper'
RSpec.describe "Factories" do
  it { p create(:alert) }
end

You're calling company somewhere in your factories or in the models before the records get created. Creating an alert does not trigger company delegation by itself.

$ rspec spec/factories_spec.rb

Factories
#<Alert id: 1, channel: "email", public_id: nil, company_id: 1, alertable_type: "QuotaAlert", alertable_id: 1>
  example at ./spec/factories_spec.rb:4

Finished in 0.07686 seconds (files took 0.88 seconds to load)
1 example, 0 failures

It might be simpler to create an alert in the console to make sure everything works and then break it down in the factories.

>> Alert.create!(alertable: QuotaAlert.create!(trigger_percent: 0.1, quota: Quota.create!), company: Company.create!)
# ...
=> #<Alert:0x00007ff608b5acd8 id: 1, channel: nil, public_id: nil, company_id: 1, alertable_type: "QuotaAlert", alertable_id: 1>

Let me know what's missing. I'll update the answer.

  • Related