Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use trait in callback #946

Closed
ofetisov opened this issue Sep 19, 2016 · 1 comment
Closed

Use trait in callback #946

ofetisov opened this issue Sep 19, 2016 · 1 comment

Comments

@ofetisov
Copy link

Hello guys, I'trying DRY my code with traits but I can't understand how to use traits in callbacks. Code below represents what i'm trying to do and obviously - doesn't works. Or maybe i'm using wrong approach?

FactoryGirl.define do
  trait :abc_t do
    abc true
   #lots of vars goes here
  end

  trait :abc_f do
    abc false
   #lots of vars goes here
  end

  factory :test, :class => Test do
    transient do
      some_var false
    end

    #some traits here

    after(:build) do |obj, evaluator|
      if evaluator.some_var
        abc_t
      else
        abc_f
      end
    end
  end
end
@joshuaclayton
Copy link
Contributor

@ofetisov traits are unusable in callbacks - imagine it as a named block to refer to some attributes you'd assign in the factory itself.

If you're attempting to conditionally apply a trait, my recommendation would be to do so from the call site (where you call create(:thing, :trait_1)) instead of in the factory itself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants