2018-10-07 21:45:51 -04:00
|
|
|
require "set"
|
|
|
|
require "active_support/core_ext/module/delegation"
|
2019-01-02 21:16:23 -05:00
|
|
|
require "active_support/core_ext/module/attribute_accessors"
|
2018-10-07 21:45:51 -04:00
|
|
|
require "active_support/deprecation"
|
|
|
|
require "active_support/notifications"
|
2017-10-20 15:20:28 -04:00
|
|
|
|
2020-04-23 19:14:02 -04:00
|
|
|
require "factory_bot/internal"
|
2018-10-07 21:45:51 -04:00
|
|
|
require "factory_bot/definition_hierarchy"
|
|
|
|
require "factory_bot/configuration"
|
|
|
|
require "factory_bot/errors"
|
|
|
|
require "factory_bot/factory_runner"
|
|
|
|
require "factory_bot/strategy_syntax_method_registrar"
|
|
|
|
require "factory_bot/strategy_calculator"
|
|
|
|
require "factory_bot/strategy/build"
|
|
|
|
require "factory_bot/strategy/create"
|
|
|
|
require "factory_bot/strategy/attributes_for"
|
|
|
|
require "factory_bot/strategy/stub"
|
|
|
|
require "factory_bot/strategy/null"
|
|
|
|
require "factory_bot/registry"
|
|
|
|
require "factory_bot/null_factory"
|
|
|
|
require "factory_bot/null_object"
|
|
|
|
require "factory_bot/evaluation"
|
|
|
|
require "factory_bot/factory"
|
|
|
|
require "factory_bot/attribute_assigner"
|
|
|
|
require "factory_bot/evaluator"
|
|
|
|
require "factory_bot/evaluator_class_definer"
|
|
|
|
require "factory_bot/attribute"
|
|
|
|
require "factory_bot/callback"
|
|
|
|
require "factory_bot/callbacks_observer"
|
|
|
|
require "factory_bot/declaration_list"
|
|
|
|
require "factory_bot/declaration"
|
|
|
|
require "factory_bot/sequence"
|
|
|
|
require "factory_bot/attribute_list"
|
|
|
|
require "factory_bot/trait"
|
Add functionality for enum traits (#1380)
## 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](https://github.com/thoughtbot/factory_bot/pull/1380/commits/5a20017351b08ce2ec9918d799e187e9eaa3ec32)).
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>
2020-05-01 17:50:51 -04:00
|
|
|
require "factory_bot/enum"
|
2018-10-07 21:45:51 -04:00
|
|
|
require "factory_bot/aliases"
|
|
|
|
require "factory_bot/definition"
|
|
|
|
require "factory_bot/definition_proxy"
|
|
|
|
require "factory_bot/syntax"
|
|
|
|
require "factory_bot/syntax_runner"
|
|
|
|
require "factory_bot/find_definitions"
|
|
|
|
require "factory_bot/reload"
|
|
|
|
require "factory_bot/decorator"
|
|
|
|
require "factory_bot/decorator/attribute_hash"
|
|
|
|
require "factory_bot/decorator/disallows_duplicates_registry"
|
|
|
|
require "factory_bot/decorator/invocation_tracker"
|
|
|
|
require "factory_bot/decorator/new_constructor"
|
|
|
|
require "factory_bot/linter"
|
|
|
|
require "factory_bot/version"
|
2017-10-20 15:20:28 -04:00
|
|
|
|
|
|
|
module FactoryBot
|
2020-04-24 17:11:12 -04:00
|
|
|
Deprecation = ActiveSupport::Deprecation.new("7.0", "factory_bot")
|
2019-01-05 21:19:04 -05:00
|
|
|
|
2019-01-11 01:39:39 -05:00
|
|
|
mattr_accessor :use_parent_strategy, instance_accessor: false
|
|
|
|
self.use_parent_strategy = true
|
2019-01-02 21:16:23 -05:00
|
|
|
|
Add functionality for enum traits (#1380)
## 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](https://github.com/thoughtbot/factory_bot/pull/1380/commits/5a20017351b08ce2ec9918d799e187e9eaa3ec32)).
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>
2020-05-01 17:50:51 -04:00
|
|
|
mattr_accessor :automatically_define_enum_traits, instance_accessor: false
|
|
|
|
self.automatically_define_enum_traits = true
|
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
# Look for errors in factories and (optionally) their traits.
|
|
|
|
# Parameters:
|
|
|
|
# factories - which factories to lint; omit for all factories
|
|
|
|
# options:
|
|
|
|
# traits: true - to lint traits as well as factories
|
|
|
|
# strategy: :create - to specify the strategy for linting
|
2018-11-21 15:28:28 -05:00
|
|
|
# verbose: true - to include full backtraces for each linting error
|
2017-10-20 15:20:28 -04:00
|
|
|
def self.lint(*args)
|
|
|
|
options = args.extract_options!
|
|
|
|
factories_to_lint = args[0] || FactoryBot.factories
|
2020-01-10 17:25:27 -05:00
|
|
|
Linter.new(factories_to_lint, **options).lint!
|
2017-10-20 15:20:28 -04:00
|
|
|
end
|
|
|
|
|
2020-04-26 15:33:43 -04:00
|
|
|
# Set the starting value for ids when using the build_stubbed strategy
|
|
|
|
#
|
|
|
|
# Arguments:
|
|
|
|
# * starting_id +Integer+
|
|
|
|
# The new starting id value.
|
|
|
|
def self.build_stubbed_starting_id=(starting_id)
|
|
|
|
Strategy::Stub.next_id = starting_id - 1
|
|
|
|
end
|
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
class << self
|
2020-04-24 17:11:12 -04:00
|
|
|
delegate :factories,
|
2020-06-05 15:15:18 -04:00
|
|
|
:register_strategy,
|
|
|
|
:rewind_sequences,
|
|
|
|
:strategy_by_name,
|
|
|
|
to: Internal
|
2017-10-20 15:20:28 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-26 15:48:37 -04:00
|
|
|
FactoryBot::Internal.register_default_strategies
|