mirror of
https://github.com/thoughtbot/factory_bot.git
synced 2022-11-09 11:43:51 -05:00
975fc4ff29
## Enum traits
Given a Rails model with an enum attribute:
```rb
class Task < ActiveRecord::Base
enum status: {queued: 0, started: 1, finished: 2}
end
```
It is common for people to define traits for each possible value of the enum:
```rb
FactoryBot.define do
factory :task do
trait :queued do
status { :queued }
end
trait :started do
status { :started }
end
trait :finished do
status { :finished }
end
end
end
```
With this commit, those trait definitions are no longer necessary—they are defined automatically by factory_bot.
If automatically defining traits for enum attributes on every factory is not desired, it is possible to disable the feature by setting `FactoryBot.automatically_define_enum_traits = false` (see commit: [Allow opting out of automatically defining traits](5a20017351
)).
In that case, it is still possible to explicitly define traits for an enum attribute in a particular factory:
```rb
FactoryBot.automatically_define_enum_traits = false
FactoryBot.define do
factory :task do
traits_for_enum(:status)
end
end
```
It is also possible to use this feature for other enumerable values, not specifically tied to ActiveRecord enum attributes:
```rb
class Task
attr_accessor :status
end
FactoryBot.define do
factory :task do
traits_for_enum(:status, ["queued", "started", "finished"])
end
end
```
The second argument here can be an enumerable object, including a Hash or Array.
Closes #1049
Co-authored-by: Lance Johnson <lancejjohnson@gmail.com>
Co-authored-by: PoTa <pota@mosfet.hu>
Co-authored-by: Frida Casas <fridacasas.fc@gmail.com>
Co-authored-by: Daniel Colson <danieljamescolson@gmail.com>
163 lines
4 KiB
Ruby
163 lines
4 KiB
Ruby
require "active_support/core_ext/hash/keys"
|
|
require "active_support/inflector"
|
|
|
|
module FactoryBot
|
|
# @api private
|
|
class Factory
|
|
attr_reader :name, :definition
|
|
|
|
def initialize(name, options = {})
|
|
assert_valid_options(options)
|
|
@name = name.respond_to?(:to_sym) ? name.to_sym : name.to_s.underscore.to_sym
|
|
@parent = options[:parent]
|
|
@aliases = options[:aliases] || []
|
|
@class_name = options[:class]
|
|
@definition = Definition.new(@name, options[:traits] || [])
|
|
@compiled = false
|
|
end
|
|
|
|
delegate :add_callback, :declare_attribute, :to_create, :define_trait, :constructor,
|
|
:defined_traits, :inherit_traits, :append_traits, to: :@definition
|
|
|
|
def build_class
|
|
@build_class ||= if class_name.is_a? Class
|
|
class_name
|
|
else
|
|
class_name.to_s.camelize.constantize
|
|
end
|
|
end
|
|
|
|
def run(build_strategy, overrides, &block)
|
|
block ||= ->(result) { result }
|
|
compile
|
|
|
|
strategy = StrategyCalculator.new(build_strategy).strategy.new
|
|
|
|
evaluator = evaluator_class.new(strategy, overrides.symbolize_keys)
|
|
attribute_assigner = AttributeAssigner.new(evaluator, build_class, &compiled_constructor)
|
|
|
|
evaluation =
|
|
Evaluation.new(evaluator, attribute_assigner, compiled_to_create)
|
|
evaluation.add_observer(CallbacksObserver.new(callbacks, evaluator))
|
|
|
|
strategy.result(evaluation).tap(&block)
|
|
end
|
|
|
|
def human_names
|
|
names.map { |name| name.to_s.humanize.downcase }
|
|
end
|
|
|
|
def associations
|
|
evaluator_class.attribute_list.associations
|
|
end
|
|
|
|
# Names for this factory, including aliases.
|
|
#
|
|
# Example:
|
|
#
|
|
# factory :user, aliases: [:author] do
|
|
# # ...
|
|
# end
|
|
#
|
|
# FactoryBot.create(:author).class
|
|
# # => User
|
|
#
|
|
# Because an attribute defined without a value or block will build an
|
|
# association with the same name, this allows associations to be defined
|
|
# without factories, such as:
|
|
#
|
|
# factory :user, aliases: [:author] do
|
|
# # ...
|
|
# end
|
|
#
|
|
# factory :post do
|
|
# author
|
|
# end
|
|
#
|
|
# FactoryBot.create(:post).author.class
|
|
# # => User
|
|
def names
|
|
[name] + @aliases
|
|
end
|
|
|
|
def compile
|
|
unless @compiled
|
|
parent.compile
|
|
parent.defined_traits.each { |trait| define_trait(trait) }
|
|
@definition.compile(build_class)
|
|
build_hierarchy
|
|
@compiled = true
|
|
end
|
|
end
|
|
|
|
def with_traits(traits)
|
|
clone.tap do |factory_with_traits|
|
|
factory_with_traits.append_traits traits
|
|
end
|
|
end
|
|
|
|
protected
|
|
|
|
def class_name
|
|
@class_name || parent.class_name || name
|
|
end
|
|
|
|
def evaluator_class
|
|
@evaluator_class ||= EvaluatorClassDefiner.new(attributes, parent.evaluator_class).evaluator_class
|
|
end
|
|
|
|
def attributes
|
|
compile
|
|
AttributeList.new(@name).tap do |list|
|
|
list.apply_attributes definition.attributes
|
|
end
|
|
end
|
|
|
|
def hierarchy_class
|
|
@hierarchy_class ||= Class.new(parent.hierarchy_class)
|
|
end
|
|
|
|
def hierarchy_instance
|
|
@hierarchy_instance ||= hierarchy_class.new
|
|
end
|
|
|
|
def build_hierarchy
|
|
hierarchy_class.build_from_definition definition
|
|
end
|
|
|
|
def callbacks
|
|
hierarchy_instance.callbacks
|
|
end
|
|
|
|
def compiled_to_create
|
|
hierarchy_instance.to_create
|
|
end
|
|
|
|
def compiled_constructor
|
|
hierarchy_instance.constructor
|
|
end
|
|
|
|
private
|
|
|
|
def assert_valid_options(options)
|
|
options.assert_valid_keys(:class, :parent, :aliases, :traits)
|
|
end
|
|
|
|
def parent
|
|
if @parent
|
|
FactoryBot::Internal.factory_by_name(@parent)
|
|
else
|
|
NullFactory.new
|
|
end
|
|
end
|
|
|
|
def initialize_copy(source)
|
|
super
|
|
@definition = @definition.clone
|
|
@evaluator_class = nil
|
|
@hierarchy_class = nil
|
|
@hierarchy_instance = nil
|
|
@compiled = false
|
|
end
|
|
end
|
|
end
|