factory_bot icon indicating copy to clipboard operation
factory_bot copied to clipboard

Context helpers for callbacks:

Open CodeMeister opened this issue 1 year ago • 11 comments

Fixes: #1694 Fixes: #1649

Context is provided as a block parameter in callbacks. This pull requests adds some helper methods to context, making it easier to detect the strategy used and identify which attributes were defined by the user / factory.

Methods included are:

  • :stategy
  • :defined_attributes
  • :user_defined_attributes
  • :factory_defined_attributes

All can be queried:

context.strategy         #=> 'build'
context.strategy.build?  #=> true

context.user_defined_attributes           #=> [:age, :name]
context.user_defined_attributes.age?      #=> true
context.user_defined_attributes.height?   #=> false

Although the Evaluator class is marked @api private, being included as a callback parameter means users are already accessing it. Making it easier to use can only be a good thing.

Tests and documentation added.

CodeMeister avatar Sep 08 '24 16:09 CodeMeister