Rails FactoryGirl Factory with optional model association
Asked Answered
T

2

0

In accordance with rails 4 model association left join validate id I had to update my factories. I am trying to create a condition where a coupon can be created, but has a job id only assigned when it is executed i.e. :

FactoryGirl.define do
  factory :coupon do
    code { rand(25**25) }
    percent_discount { rand(100**1) }
    start_at { Time.now }
    end_at { 30.day.from_now }

    trait :executed do |c|
      job
      c.executed_at { Time.now }
    end
  end
end

enter image description here

Thee answered 5/3, 2016 at 0:43 Comment(0)
C
0

You can use callbacks in factory. Here is an example of it :

FactoryGirl.define do
  factory :coupon do
    code { rand(25**25) }
    percent_discount { rand(100**1) }
    start_at { Time.now }
    end_at { 30.day.from_now }

    after(:create) do |c|
      #do job related stuff
    end
  end
end

Checkout its documentation for more information : https://github.com/thoughtbot/factory_girl/blob/master/GETTING_STARTED.md#callbacks

UPDATE

Based on your last comment, I believe trait will not be useful. Here is something I understand create coupon > do some process > execute coupon > assign job. So after creating coupon I think there is some delay / process logic of coupon. So when you execute coupon at this time you need to create object of job and associate with that coupon. I believe following would be suitable for that flow :

FactoryGirl.define do
  factory :coupon do
    code { rand(25**25) }
    percent_discount { rand(100**1) }
    start_at { Time.now }
    end_at { 30.day.from_now }

    #This trait is used in case, 
    #if you want job to be assigned to coupon
    #for example create(:coupon,:executed)
    trait :executed do |c|
      association :job, factory: [:job] 
      c.executed_at { Time.now }
    end

  end
end

#In Test Case
@coupon = create(:coupon)
#have some test for coupon before execution
#now executing coupon
@coupon.update_attributes(job_id: create(:job), executed_at: Time.now)
Cookout answered 5/3, 2016 at 10:5 Comment(13)
do you feel that is a better approach than the answer I posted below with traits?Thee
traits and callbacks are different. Trait are used to group attributes while callbacks are used to do something based on event. I believe your issue was do something on event and hence callback is suitable. github.com/thoughtbot/factory_girl/blob/master/…Cookout
in my case a job is only assigned when the coupon is executed, so wouldn't a trait be preferred in that scenario?Thee
when we say "coupon is executed" means object of coupon is created ? When coupon is created it would always have job assigned ? if so callback is good approach to use in my opinion. Trait is used to bundle attributes and used to created specific object with specific attributes.Cookout
The coupon is created without a job assigned. Only when the coupon is used is it paired with one job. I referred to that as executed. Traits?Thee
Based on my last comment, do you have thoughts? I would like to give you credit for this, but I think it approaches the problem differently than I need it to work. I am a crazy here?Thee
I updated my answer. I hope it matches with your explanation.Cookout
"Based on your last comment, I believe trait will not be useful. Here is something I understand create coupon > do some process > execute coupon > assign job. So after creating coupon I think there is some delay / process logic of coupon. So when you execute coupon at this time you need to create object of job and associate with that coupon. I believe following would be suitable for that flow" => True, however, I have another question. Let's say I wanted to be able to create the coupon with a valid job assigned for tests? Coupons can be created and never assigned as well.Thee
So I could use a trait and call FactoryGirl.create(:coupon, :executed) for example and that will build both a coupon and valid assigned job for tests.Thee
Yes, in that case it would be helpful. It is something as per your requirement.Cookout
if you want to update your answer I will delete mine and mark yours. this way others can have the full walk throughThee
Done. Can you please have a look and let me know if I am missing anything??Cookout
looks great! I just checked this answerThee
P
0

It seems that you should remove something like validates :job, presence: true or validates :job_id, presence: true from your coupon model

Prole answered 5/3, 2016 at 0:56 Comment(2)
I had that already on those classes, in this case the association was not properly setup.Thee
if you want to adjust your answer to match mine, I will delete mine and give you credit. Thoughts?Thee
C
0

You can use callbacks in factory. Here is an example of it :

FactoryGirl.define do
  factory :coupon do
    code { rand(25**25) }
    percent_discount { rand(100**1) }
    start_at { Time.now }
    end_at { 30.day.from_now }

    after(:create) do |c|
      #do job related stuff
    end
  end
end

Checkout its documentation for more information : https://github.com/thoughtbot/factory_girl/blob/master/GETTING_STARTED.md#callbacks

UPDATE

Based on your last comment, I believe trait will not be useful. Here is something I understand create coupon > do some process > execute coupon > assign job. So after creating coupon I think there is some delay / process logic of coupon. So when you execute coupon at this time you need to create object of job and associate with that coupon. I believe following would be suitable for that flow :

FactoryGirl.define do
  factory :coupon do
    code { rand(25**25) }
    percent_discount { rand(100**1) }
    start_at { Time.now }
    end_at { 30.day.from_now }

    #This trait is used in case, 
    #if you want job to be assigned to coupon
    #for example create(:coupon,:executed)
    trait :executed do |c|
      association :job, factory: [:job] 
      c.executed_at { Time.now }
    end

  end
end

#In Test Case
@coupon = create(:coupon)
#have some test for coupon before execution
#now executing coupon
@coupon.update_attributes(job_id: create(:job), executed_at: Time.now)
Cookout answered 5/3, 2016 at 10:5 Comment(13)
do you feel that is a better approach than the answer I posted below with traits?Thee
traits and callbacks are different. Trait are used to group attributes while callbacks are used to do something based on event. I believe your issue was do something on event and hence callback is suitable. github.com/thoughtbot/factory_girl/blob/master/…Cookout
in my case a job is only assigned when the coupon is executed, so wouldn't a trait be preferred in that scenario?Thee
when we say "coupon is executed" means object of coupon is created ? When coupon is created it would always have job assigned ? if so callback is good approach to use in my opinion. Trait is used to bundle attributes and used to created specific object with specific attributes.Cookout
The coupon is created without a job assigned. Only when the coupon is used is it paired with one job. I referred to that as executed. Traits?Thee
Based on my last comment, do you have thoughts? I would like to give you credit for this, but I think it approaches the problem differently than I need it to work. I am a crazy here?Thee
I updated my answer. I hope it matches with your explanation.Cookout
"Based on your last comment, I believe trait will not be useful. Here is something I understand create coupon > do some process > execute coupon > assign job. So after creating coupon I think there is some delay / process logic of coupon. So when you execute coupon at this time you need to create object of job and associate with that coupon. I believe following would be suitable for that flow" => True, however, I have another question. Let's say I wanted to be able to create the coupon with a valid job assigned for tests? Coupons can be created and never assigned as well.Thee
So I could use a trait and call FactoryGirl.create(:coupon, :executed) for example and that will build both a coupon and valid assigned job for tests.Thee
Yes, in that case it would be helpful. It is something as per your requirement.Cookout
if you want to update your answer I will delete mine and mark yours. this way others can have the full walk throughThee
Done. Can you please have a look and let me know if I am missing anything??Cookout
looks great! I just checked this answerThee

© 2022 - 2024 — McMap. All rights reserved.