factory_bot-with 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.github/workflows/main.yml +28 -0
- data/.gitignore +11 -0
- data/.solargraph.yml +21 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +132 -0
- data/LICENSE.txt +21 -0
- data/README.md +186 -0
- data/lib/factory_bot/with/assoc_info.rb +103 -0
- data/lib/factory_bot/with/methods.rb +34 -0
- data/lib/factory_bot/with/proxy.rb +34 -0
- data/lib/factory_bot/with/version.rb +7 -0
- data/lib/factory_bot/with.rb +103 -0
- metadata +75 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: a279f1170ba696ac48614f1c95572bf4be5a9b3f5ac5e47a3574813deafd5623
|
|
4
|
+
data.tar.gz: dddb6e8f841108fd70a80d509ea847a9daf6b0613111bf9977c8b79a6e32bf20
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 2dc2904bb72bee299282e42f9daa0ba697e7c4eb7febd3475697ff0db2df89c03aa71c5bc55e2e97d2a786a3f3449b6666d68b643874a70bc32a5f6ef0a379ee
|
|
7
|
+
data.tar.gz: 3b322dbad72231b9e16e97113a3a300db5a88a90d2a67c06ef026711881c2de52f48ca0326810be0ab3e1ce7f57ca56d238a1070d6a78eca2ddf5d667dd226ea
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
name: Ruby
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches:
|
|
6
|
+
- main
|
|
7
|
+
|
|
8
|
+
pull_request:
|
|
9
|
+
|
|
10
|
+
jobs:
|
|
11
|
+
build:
|
|
12
|
+
runs-on: ubuntu-latest
|
|
13
|
+
name: Ruby ${{ matrix.ruby }}
|
|
14
|
+
strategy:
|
|
15
|
+
matrix:
|
|
16
|
+
ruby:
|
|
17
|
+
- '3.3.6'
|
|
18
|
+
- '3.2.6'
|
|
19
|
+
|
|
20
|
+
steps:
|
|
21
|
+
- uses: actions/checkout@v4
|
|
22
|
+
- name: Set up Ruby
|
|
23
|
+
uses: ruby/setup-ruby@v1
|
|
24
|
+
with:
|
|
25
|
+
ruby-version: ${{ matrix.ruby }}
|
|
26
|
+
bundler-cache: true
|
|
27
|
+
- name: Run the default task
|
|
28
|
+
run: bundle exec rake
|
data/.gitignore
ADDED
data/.solargraph.yml
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
---
|
|
2
|
+
include:
|
|
3
|
+
- "**/*.rb"
|
|
4
|
+
exclude:
|
|
5
|
+
- spec/**/*
|
|
6
|
+
- test/**/*
|
|
7
|
+
- vendor/**/*
|
|
8
|
+
- ".bundle/**/*"
|
|
9
|
+
require: []
|
|
10
|
+
domains: []
|
|
11
|
+
reporters:
|
|
12
|
+
- rubocop
|
|
13
|
+
formatter:
|
|
14
|
+
rubocop:
|
|
15
|
+
cops: safe
|
|
16
|
+
except: []
|
|
17
|
+
only: []
|
|
18
|
+
extra_args: []
|
|
19
|
+
require_paths: []
|
|
20
|
+
plugins: []
|
|
21
|
+
max_files: 5000
|
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
|
2
|
+
|
|
3
|
+
## Our Pledge
|
|
4
|
+
|
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our
|
|
6
|
+
community a harassment-free experience for everyone, regardless of age, body
|
|
7
|
+
size, visible or invisible disability, ethnicity, sex characteristics, gender
|
|
8
|
+
identity and expression, level of experience, education, socio-economic status,
|
|
9
|
+
nationality, personal appearance, race, caste, color, religion, or sexual
|
|
10
|
+
identity and orientation.
|
|
11
|
+
|
|
12
|
+
We pledge to act and interact in ways that contribute to an open, welcoming,
|
|
13
|
+
diverse, inclusive, and healthy community.
|
|
14
|
+
|
|
15
|
+
## Our Standards
|
|
16
|
+
|
|
17
|
+
Examples of behavior that contributes to a positive environment for our
|
|
18
|
+
community include:
|
|
19
|
+
|
|
20
|
+
* Demonstrating empathy and kindness toward other people
|
|
21
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
|
22
|
+
* Giving and gracefully accepting constructive feedback
|
|
23
|
+
* Accepting responsibility and apologizing to those affected by our mistakes,
|
|
24
|
+
and learning from the experience
|
|
25
|
+
* Focusing on what is best not just for us as individuals, but for the overall
|
|
26
|
+
community
|
|
27
|
+
|
|
28
|
+
Examples of unacceptable behavior include:
|
|
29
|
+
|
|
30
|
+
* The use of sexualized language or imagery, and sexual attention or advances of
|
|
31
|
+
any kind
|
|
32
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
|
33
|
+
* Public or private harassment
|
|
34
|
+
* Publishing others' private information, such as a physical or email address,
|
|
35
|
+
without their explicit permission
|
|
36
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
|
37
|
+
professional setting
|
|
38
|
+
|
|
39
|
+
## Enforcement Responsibilities
|
|
40
|
+
|
|
41
|
+
Community leaders are responsible for clarifying and enforcing our standards of
|
|
42
|
+
acceptable behavior and will take appropriate and fair corrective action in
|
|
43
|
+
response to any behavior that they deem inappropriate, threatening, offensive,
|
|
44
|
+
or harmful.
|
|
45
|
+
|
|
46
|
+
Community leaders have the right and responsibility to remove, edit, or reject
|
|
47
|
+
comments, commits, code, wiki edits, issues, and other contributions that are
|
|
48
|
+
not aligned to this Code of Conduct, and will communicate reasons for moderation
|
|
49
|
+
decisions when appropriate.
|
|
50
|
+
|
|
51
|
+
## Scope
|
|
52
|
+
|
|
53
|
+
This Code of Conduct applies within all community spaces, and also applies when
|
|
54
|
+
an individual is officially representing the community in public spaces.
|
|
55
|
+
Examples of representing our community include using an official email address,
|
|
56
|
+
posting via an official social media account, or acting as an appointed
|
|
57
|
+
representative at an online or offline event.
|
|
58
|
+
|
|
59
|
+
## Enforcement
|
|
60
|
+
|
|
61
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
|
62
|
+
reported to the community leaders responsible for enforcement at
|
|
63
|
+
[INSERT CONTACT METHOD].
|
|
64
|
+
All complaints will be reviewed and investigated promptly and fairly.
|
|
65
|
+
|
|
66
|
+
All community leaders are obligated to respect the privacy and security of the
|
|
67
|
+
reporter of any incident.
|
|
68
|
+
|
|
69
|
+
## Enforcement Guidelines
|
|
70
|
+
|
|
71
|
+
Community leaders will follow these Community Impact Guidelines in determining
|
|
72
|
+
the consequences for any action they deem in violation of this Code of Conduct:
|
|
73
|
+
|
|
74
|
+
### 1. Correction
|
|
75
|
+
|
|
76
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed
|
|
77
|
+
unprofessional or unwelcome in the community.
|
|
78
|
+
|
|
79
|
+
**Consequence**: A private, written warning from community leaders, providing
|
|
80
|
+
clarity around the nature of the violation and an explanation of why the
|
|
81
|
+
behavior was inappropriate. A public apology may be requested.
|
|
82
|
+
|
|
83
|
+
### 2. Warning
|
|
84
|
+
|
|
85
|
+
**Community Impact**: A violation through a single incident or series of
|
|
86
|
+
actions.
|
|
87
|
+
|
|
88
|
+
**Consequence**: A warning with consequences for continued behavior. No
|
|
89
|
+
interaction with the people involved, including unsolicited interaction with
|
|
90
|
+
those enforcing the Code of Conduct, for a specified period of time. This
|
|
91
|
+
includes avoiding interactions in community spaces as well as external channels
|
|
92
|
+
like social media. Violating these terms may lead to a temporary or permanent
|
|
93
|
+
ban.
|
|
94
|
+
|
|
95
|
+
### 3. Temporary Ban
|
|
96
|
+
|
|
97
|
+
**Community Impact**: A serious violation of community standards, including
|
|
98
|
+
sustained inappropriate behavior.
|
|
99
|
+
|
|
100
|
+
**Consequence**: A temporary ban from any sort of interaction or public
|
|
101
|
+
communication with the community for a specified period of time. No public or
|
|
102
|
+
private interaction with the people involved, including unsolicited interaction
|
|
103
|
+
with those enforcing the Code of Conduct, is allowed during this period.
|
|
104
|
+
Violating these terms may lead to a permanent ban.
|
|
105
|
+
|
|
106
|
+
### 4. Permanent Ban
|
|
107
|
+
|
|
108
|
+
**Community Impact**: Demonstrating a pattern of violation of community
|
|
109
|
+
standards, including sustained inappropriate behavior, harassment of an
|
|
110
|
+
individual, or aggression toward or disparagement of classes of individuals.
|
|
111
|
+
|
|
112
|
+
**Consequence**: A permanent ban from any sort of public interaction within the
|
|
113
|
+
community.
|
|
114
|
+
|
|
115
|
+
## Attribution
|
|
116
|
+
|
|
117
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
|
118
|
+
version 2.1, available at
|
|
119
|
+
[https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
|
|
120
|
+
|
|
121
|
+
Community Impact Guidelines were inspired by
|
|
122
|
+
[Mozilla's code of conduct enforcement ladder][Mozilla CoC].
|
|
123
|
+
|
|
124
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
|
125
|
+
[https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
|
|
126
|
+
[https://www.contributor-covenant.org/translations][translations].
|
|
127
|
+
|
|
128
|
+
[homepage]: https://www.contributor-covenant.org
|
|
129
|
+
[v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
|
|
130
|
+
[Mozilla CoC]: https://github.com/mozilla/diversity
|
|
131
|
+
[FAQ]: https://www.contributor-covenant.org/faq
|
|
132
|
+
[translations]: https://www.contributor-covenant.org/translations
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 yubrot
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
# FactoryBot::With
|
|
2
|
+
|
|
3
|
+
FactoryBot::With is a FactoryBot extension that wraps `FactoryBot::Syntax::Methods` to make it a little easier to use.
|
|
4
|
+
|
|
5
|
+
For example, with these factories:
|
|
6
|
+
|
|
7
|
+
```ruby
|
|
8
|
+
FactoryBot.define do
|
|
9
|
+
factory(:blog)
|
|
10
|
+
factory(:article) { blog }
|
|
11
|
+
factory(:comment) { article }
|
|
12
|
+
end
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Instead of writing like this:
|
|
16
|
+
|
|
17
|
+
```ruby
|
|
18
|
+
create(:blog) do |blog|
|
|
19
|
+
create(:article, blog:) { |article| create(:comment, article:) }
|
|
20
|
+
create(:article, blog:) { |article| create_list(:comment, 3, article:) }
|
|
21
|
+
end
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
FactoryBot::With allows you to write:
|
|
25
|
+
|
|
26
|
+
```ruby
|
|
27
|
+
create.blog(
|
|
28
|
+
with.article(with.comment),
|
|
29
|
+
with.article(with_list.comment(3)),
|
|
30
|
+
)
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Installation
|
|
34
|
+
|
|
35
|
+
On your Gemfile:
|
|
36
|
+
|
|
37
|
+
```ruby
|
|
38
|
+
gem "factory_bot-with"
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Then, instead of including `FactoryBot::Syntax::Methods`, include `FactoryBot::With::Methods`:
|
|
42
|
+
|
|
43
|
+
```ruby
|
|
44
|
+
# RSpec example
|
|
45
|
+
RSpec.configure do |config|
|
|
46
|
+
# ...
|
|
47
|
+
config.include FactoryBot::With::Methods
|
|
48
|
+
# ...
|
|
49
|
+
end
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## What differs from `FactoryBot::Syntax::Methods`?
|
|
53
|
+
|
|
54
|
+
### Method style syntax
|
|
55
|
+
|
|
56
|
+
FactoryBot::With overrides the behavior when factory methods are called without arguments.
|
|
57
|
+
|
|
58
|
+
```ruby
|
|
59
|
+
create(:foo, ...) # behaves in the same way as FactoryBot.create
|
|
60
|
+
create # returns a Proxy (an intermadiate) object
|
|
61
|
+
create.foo(...) # is equivalent to create(:foo, ...)
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
This applies to other factory methods such as `build_stubbed`, `create_list`, `with` (described later), etc. as well.
|
|
65
|
+
|
|
66
|
+
```ruby
|
|
67
|
+
build_stubbed.foo(...)
|
|
68
|
+
create_list.foo(10, ...)
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### `with`, `with_pair`, and `with_list` operator
|
|
72
|
+
|
|
73
|
+
FactoryBot::With provides a new operator `with` (and its family).
|
|
74
|
+
|
|
75
|
+
```ruby
|
|
76
|
+
with(:factory_name, ...)
|
|
77
|
+
with_pair(:factory_name, ...)
|
|
78
|
+
with_list(:factory_name, number_of_items, ...)
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
The result of this operator (`With` instance) can be passed as an argument to factory methods such as `build` or `create`, which can then create additional objects following the result of the factory.
|
|
82
|
+
|
|
83
|
+
```ruby
|
|
84
|
+
create.blog(with.article)
|
|
85
|
+
# is equivalent to ...
|
|
86
|
+
blog = create.blog
|
|
87
|
+
create.article(blog:)
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
The overridden factory methods collect these `with` arguments before delegating object creation to the actual factory methods.
|
|
91
|
+
|
|
92
|
+
<details>
|
|
93
|
+
<summary>Automatic association resolution</summary>
|
|
94
|
+
|
|
95
|
+
`with` automatically resolves references to ancestor objects based on the definition of the FactoryBot associations.
|
|
96
|
+
|
|
97
|
+
This automatic resolution takes into account any [traits](https://thoughtbot.github.io/factory_bot/traits/summary.html) in the factories, [aliases](https://thoughtbot.github.io/factory_bot/sequences/aliases.html) in the factories, and [factory specifications](https://thoughtbot.github.io/factory_bot/associations/specifying-the-factory.html) in the associations.
|
|
98
|
+
|
|
99
|
+
```ruby
|
|
100
|
+
FactoryBot.define do
|
|
101
|
+
factory(:video)
|
|
102
|
+
factory(:photo)
|
|
103
|
+
factory(:tag) do
|
|
104
|
+
trait(:for_video) { taggable factory: :video }
|
|
105
|
+
trait(:for_photo) { taggable factory: :photo }
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
create.video(with.tag(text: "latest")) # resolved as taggable: video
|
|
110
|
+
create.photo(with.tag(text: "latest")) # ...
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
Due to technical limitations, [inline associations](https://thoughtbot.github.io/factory_bot/associations/inline-definition.html) cannot be resolved.
|
|
114
|
+
|
|
115
|
+
</details>
|
|
116
|
+
|
|
117
|
+
<details>
|
|
118
|
+
<summary>Autocomplete fully-qualified factory name</summary>
|
|
119
|
+
|
|
120
|
+
For a factory name that is prefixed by the parent object's factory name, the prefix can be omitted.
|
|
121
|
+
|
|
122
|
+
```ruby
|
|
123
|
+
FactoryBot.define do
|
|
124
|
+
factory(:blog)
|
|
125
|
+
factory(:blog_article) { blog }
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
create.blog(with.article) # autocomplete to :blog_article
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
</details>
|
|
132
|
+
|
|
133
|
+
### `with` as a template
|
|
134
|
+
|
|
135
|
+
`with` can also be used stand-alone. It works as a template for factory method calls.
|
|
136
|
+
|
|
137
|
+
Instead of writing:
|
|
138
|
+
|
|
139
|
+
```ruby
|
|
140
|
+
let(:story) { create(:story, *story_args, **story_kwargs) }
|
|
141
|
+
let(:story_args) { [] }
|
|
142
|
+
let(:story_kwargs) { {} }
|
|
143
|
+
|
|
144
|
+
context "when published more than one year ago" do
|
|
145
|
+
let(:story_args) { [*super(), :published] }
|
|
146
|
+
let(:story_kwargs) { { **super(), start_at: 2.year.ago } }
|
|
147
|
+
|
|
148
|
+
# ...
|
|
149
|
+
end
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
You can write like this:
|
|
153
|
+
|
|
154
|
+
```ruby
|
|
155
|
+
# Factory methods accept a With instance as a first argument:
|
|
156
|
+
let(:story) { create(story_template) }
|
|
157
|
+
let(:story_template) { with.story }
|
|
158
|
+
|
|
159
|
+
context "when published more than one year ago" do
|
|
160
|
+
let(:story_template) { with(super(), :published, start_at: 2.year.ago) }
|
|
161
|
+
|
|
162
|
+
# ...
|
|
163
|
+
end
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
## Development
|
|
167
|
+
|
|
168
|
+
```bash
|
|
169
|
+
git clone https://github.com/yubrot/factory_bot-with
|
|
170
|
+
cd gems/factory_bot-with
|
|
171
|
+
bin/setup
|
|
172
|
+
bundle exec rake --tasks
|
|
173
|
+
bundle exec rake
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
## Contributing
|
|
177
|
+
|
|
178
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/yubrot/factory_bot-with. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/yubrot/factory_bot-with/blob/main/CODE_OF_CONDUCT.md).
|
|
179
|
+
|
|
180
|
+
## License
|
|
181
|
+
|
|
182
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
|
183
|
+
|
|
184
|
+
## Code of Conduct
|
|
185
|
+
|
|
186
|
+
Everyone interacting in the FactoryBot::With project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/yubrot/factory_bot-with/blob/main/CODE_OF_CONDUCT.md).
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module FactoryBot
|
|
4
|
+
class With
|
|
5
|
+
# An association information of a FactoryBot factory.
|
|
6
|
+
class AssocInfo
|
|
7
|
+
# @return [Set<Symbol>] List of factory names to be considered compatible with this factory
|
|
8
|
+
attr_reader :factory_names
|
|
9
|
+
# @return [{Symbol => Symbol}] a map from factory names to association names
|
|
10
|
+
attr_reader :map
|
|
11
|
+
|
|
12
|
+
# @!visibility private
|
|
13
|
+
def initialize(factory_names, map)
|
|
14
|
+
unless factory_names.is_a?(Set) && factory_names.all? { _1.is_a?(Symbol) }
|
|
15
|
+
raise ArgumentError, "factory_names must be a Set of Symbols"
|
|
16
|
+
end
|
|
17
|
+
unless map.is_a?(Hash) && map.all? { _1.is_a?(Symbol) && _2.is_a?(Symbol) }
|
|
18
|
+
raise ArgumentError, "map must be a Hash of Symbol"
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
@factory_names = factory_names
|
|
22
|
+
@map = map
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# @param ancestors [Array<Array(AssocInfo, Object)>]
|
|
26
|
+
# @param dest [{Symbol => Object}]
|
|
27
|
+
def perform_automatic_association_resolution(ancestors, dest)
|
|
28
|
+
priorities = {}
|
|
29
|
+
map.each do |factory_name, attribute|
|
|
30
|
+
# skip if this attribute is explicitly specified
|
|
31
|
+
next if dest.member?(attribute) && !priorities.member?(attribute)
|
|
32
|
+
|
|
33
|
+
# closer ancestors have higher (lower integer) priority
|
|
34
|
+
ancestor, priority = ancestors.each_with_index.find do |(ancestor_assoc_info, _), _|
|
|
35
|
+
ancestor_assoc_info.factory_names.include?(factory_name)
|
|
36
|
+
end
|
|
37
|
+
next if !ancestor || priorities.fetch(attribute, Float::INFINITY) <= priority
|
|
38
|
+
|
|
39
|
+
priorities[attribute] = priority
|
|
40
|
+
dest[attribute] = ancestor[1]
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
class << self
|
|
45
|
+
# @param ancestors [Array<Array(AssocInfo, Object)>]
|
|
46
|
+
# @param partial_factory_name [Symbol]
|
|
47
|
+
# @return [Symbol]
|
|
48
|
+
def autocomplete_fully_qualified_factory_name(ancestors, partial_factory_name)
|
|
49
|
+
return partial_factory_name if exists?(partial_factory_name)
|
|
50
|
+
|
|
51
|
+
ancestors.each do |(ancestor_assoc_info, _)|
|
|
52
|
+
ancestor_assoc_info.factory_names.each do |ancestor_factory_name|
|
|
53
|
+
factory_name = :"#{ancestor_factory_name}_#{partial_factory_name}"
|
|
54
|
+
return factory_name if exists?(factory_name)
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
raise ArgumentError, "FactoryBot factory #{partial_factory_name} is not defined"
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# @param factory_name [Symbol]
|
|
62
|
+
# @return [Boolean]
|
|
63
|
+
def exists?(factory_name)
|
|
64
|
+
!!cache.fetch(factory_name) { FactoryBot.factories.registered?(factory_name) }
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# @param factory_name [Symbol]
|
|
68
|
+
# @return [AssocInfo]
|
|
69
|
+
def get(factory_name)
|
|
70
|
+
cache.fetch(factory_name) { cache[factory_name] = from_factory_bot_factory(factory_name) }
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# @param factory_name [Symbol]
|
|
74
|
+
# @return [AssocInfo]
|
|
75
|
+
def from_factory_bot_factory(factory_name)
|
|
76
|
+
unless FactoryBot.factories.registered?(factory_name)
|
|
77
|
+
raise ArgumentError, "FactoryBot factory #{factory_name} is not defined"
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
factory = FactoryBot.factories.find(factory_name)
|
|
81
|
+
|
|
82
|
+
# NOTE: We consider aliases to be incompatible with each other
|
|
83
|
+
factory_names = Set[factory_name]
|
|
84
|
+
map = {}
|
|
85
|
+
while factory.is_a?(FactoryBot::Factory)
|
|
86
|
+
factory_names << factory.name
|
|
87
|
+
# Here, we use reverse_each to prioritize the upper association
|
|
88
|
+
factory.with_traits(factory.defined_traits.map(&:name)).associations.reverse_each do |assoc|
|
|
89
|
+
map[Array(assoc.factory)[0].to_sym] = assoc.name
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
factory = factory.__send__(:parent)
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
new(factory_names, map)
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
# @return [{Symbol => AssocInfo}]
|
|
99
|
+
def cache = @cache ||= {}
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
end
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module FactoryBot
|
|
4
|
+
class With
|
|
5
|
+
# A <code>FactoryBot::Syntax::Methods</code> replacement to enable <code>factory_bot-with</code> features.
|
|
6
|
+
module Methods
|
|
7
|
+
include FactoryBot::Syntax::Methods
|
|
8
|
+
|
|
9
|
+
BUILD_STRATEGIES = %i[build build_stubbed create attributes_for with].freeze
|
|
10
|
+
VARIATIONS = {
|
|
11
|
+
unit: BUILD_STRATEGIES.to_h { [_1, _1] }.freeze,
|
|
12
|
+
pair: BUILD_STRATEGIES.to_h { [_1, :"#{_1}_pair"] }.freeze,
|
|
13
|
+
list: BUILD_STRATEGIES.to_h { [_1, :"#{_1}_list"] }.freeze,
|
|
14
|
+
}.freeze
|
|
15
|
+
|
|
16
|
+
VARIATIONS.each do |variation, build_strategies|
|
|
17
|
+
build_strategies.each do |build_strategy, method_name|
|
|
18
|
+
define_method(method_name) do |factory = nil, *args, **kwargs, &block|
|
|
19
|
+
return Proxy.new(self, __method__) unless factory
|
|
20
|
+
|
|
21
|
+
with =
|
|
22
|
+
if factory.is_a? With
|
|
23
|
+
factory.merge(With.new(variation, factory.factory_name, *args, **kwargs, &block))
|
|
24
|
+
else
|
|
25
|
+
With.new(variation, factory, *args, **kwargs, &block)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
with.instantiate(build_strategy)
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module FactoryBot
|
|
4
|
+
class With
|
|
5
|
+
# An intermediate object to provide some notation combined with <code>method_missing</code>.
|
|
6
|
+
# @example
|
|
7
|
+
# class Foo
|
|
8
|
+
# def foo(name = nil)
|
|
9
|
+
# return FactoryBot::With::Proxy.new(self, __method__) unless name
|
|
10
|
+
#
|
|
11
|
+
# name
|
|
12
|
+
# end
|
|
13
|
+
# end
|
|
14
|
+
#
|
|
15
|
+
# Foo.new.foo.bar #=> :bar
|
|
16
|
+
class Proxy < BasicObject
|
|
17
|
+
# @param receiver [Object]
|
|
18
|
+
# @param method [Symbol]
|
|
19
|
+
# @param preargs [Array]
|
|
20
|
+
def initialize(receiver, method, *preargs)
|
|
21
|
+
@receiver = receiver
|
|
22
|
+
@method = method
|
|
23
|
+
@preargs = preargs
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# @!visibility private
|
|
27
|
+
def respond_to_missing?(_method_name, _) = true
|
|
28
|
+
|
|
29
|
+
def method_missing(method_name, ...)
|
|
30
|
+
@receiver.__send__(@method, *@preargs, method_name, ...)
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "factory_bot"
|
|
4
|
+
require_relative "with/version"
|
|
5
|
+
require_relative "with/proxy"
|
|
6
|
+
require_relative "with/assoc_info"
|
|
7
|
+
require_relative "with/methods"
|
|
8
|
+
|
|
9
|
+
module FactoryBot
|
|
10
|
+
# An intermediate state for <code>with</code> operator.
|
|
11
|
+
class With
|
|
12
|
+
# @return [:unit, :pair, :list]
|
|
13
|
+
attr_reader :variation
|
|
14
|
+
# @return [Symbol]
|
|
15
|
+
attr_reader :factory_name
|
|
16
|
+
# @return [Array<With>]
|
|
17
|
+
attr_reader :withes
|
|
18
|
+
# @return [Array<Object>]
|
|
19
|
+
attr_reader :args
|
|
20
|
+
# @return [{Symbol => Object}]
|
|
21
|
+
attr_reader :kwargs
|
|
22
|
+
# @return [Proc, nil]
|
|
23
|
+
attr_reader :block
|
|
24
|
+
|
|
25
|
+
# @!visibility private
|
|
26
|
+
def initialize(variation, factory_name, *args, **kwargs, &block)
|
|
27
|
+
@variation = variation
|
|
28
|
+
@factory_name = factory_name
|
|
29
|
+
@withes, @args = args.partition { _1.is_a? self.class }
|
|
30
|
+
@kwargs = kwargs
|
|
31
|
+
@block = block
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# @!visibility private
|
|
35
|
+
# @return [Boolean]
|
|
36
|
+
def plain? = withes.empty? && args.empty? && kwargs.empty? && block.nil?
|
|
37
|
+
|
|
38
|
+
# @!visibility private
|
|
39
|
+
# @param other [With]
|
|
40
|
+
# @return [With]
|
|
41
|
+
def merge(other)
|
|
42
|
+
raise TypeError, "oter must be an instance of #{self.class}" unless other.is_a? self.class
|
|
43
|
+
raise ArgumentError, "other must have the same variation" if other.variation != variation
|
|
44
|
+
raise ArgumentError, "other must have the same factory_name" if other.factory_name != factory_name
|
|
45
|
+
|
|
46
|
+
return self if other.plain?
|
|
47
|
+
return other if plain?
|
|
48
|
+
|
|
49
|
+
self.class.new(
|
|
50
|
+
variation,
|
|
51
|
+
factory_name,
|
|
52
|
+
*args, *other.args, *withes, *other.withes,
|
|
53
|
+
**kwargs, **other.kwargs,
|
|
54
|
+
&self.class.merge_block(block, other.block)
|
|
55
|
+
)
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# @!visibility private
|
|
59
|
+
# @param build_strategy [:build, :build_stubbed, :create, :attributes_for, :with]
|
|
60
|
+
# @param ancestors [Array<Array(AssocInfo, Object)>, nil]
|
|
61
|
+
# @return [Object]
|
|
62
|
+
def instantiate(build_strategy, ancestors = nil)
|
|
63
|
+
return self if build_strategy == :with
|
|
64
|
+
|
|
65
|
+
factory_bot_method = Methods::VARIATIONS[variation][build_strategy]
|
|
66
|
+
factory_name, kwargs =
|
|
67
|
+
if ancestors
|
|
68
|
+
kwargs = @kwargs.dup
|
|
69
|
+
factory_name = AssocInfo.autocomplete_fully_qualified_factory_name(ancestors, @factory_name)
|
|
70
|
+
AssocInfo.get(factory_name).perform_automatic_association_resolution(ancestors, kwargs)
|
|
71
|
+
[factory_name, kwargs]
|
|
72
|
+
else
|
|
73
|
+
[@factory_name, @kwargs]
|
|
74
|
+
end
|
|
75
|
+
result = FactoryBot.__send__(factory_bot_method, factory_name, *args, **kwargs, &block)
|
|
76
|
+
|
|
77
|
+
unless withes.empty?
|
|
78
|
+
parents = variation == :unit ? [result] : result
|
|
79
|
+
assoc_info = AssocInfo.get(@factory_name)
|
|
80
|
+
parents.each do |parent|
|
|
81
|
+
ancestors_for_children = [[assoc_info, parent], *ancestors || []]
|
|
82
|
+
withes.each { _1.instantiate(build_strategy, ancestors_for_children) }
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
result
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
# @!visibility private
|
|
90
|
+
# @param first [Proc, nil]
|
|
91
|
+
# @param second [Proc, nil]
|
|
92
|
+
# @return [Proc, nil]
|
|
93
|
+
def self.merge_block(first, second)
|
|
94
|
+
return first unless second
|
|
95
|
+
return second unless first
|
|
96
|
+
|
|
97
|
+
lambda do |*args|
|
|
98
|
+
first.call(*args)
|
|
99
|
+
second.call(*args)
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: factory_bot-with
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- yubrot
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2024-11-27 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: factory_bot
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '6.0'
|
|
20
|
+
type: :runtime
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '6.0'
|
|
27
|
+
description: FactoryBot extension that wraps FactoryBot::Syntax::Methods to make it
|
|
28
|
+
a little easier to use
|
|
29
|
+
email:
|
|
30
|
+
- yubrot@gmail.com
|
|
31
|
+
executables: []
|
|
32
|
+
extensions: []
|
|
33
|
+
extra_rdoc_files: []
|
|
34
|
+
files:
|
|
35
|
+
- ".github/workflows/main.yml"
|
|
36
|
+
- ".gitignore"
|
|
37
|
+
- ".solargraph.yml"
|
|
38
|
+
- CHANGELOG.md
|
|
39
|
+
- CODE_OF_CONDUCT.md
|
|
40
|
+
- LICENSE.txt
|
|
41
|
+
- README.md
|
|
42
|
+
- lib/factory_bot/with.rb
|
|
43
|
+
- lib/factory_bot/with/assoc_info.rb
|
|
44
|
+
- lib/factory_bot/with/methods.rb
|
|
45
|
+
- lib/factory_bot/with/proxy.rb
|
|
46
|
+
- lib/factory_bot/with/version.rb
|
|
47
|
+
homepage: https://github.com/yubrot/factory_bot-with
|
|
48
|
+
licenses:
|
|
49
|
+
- MIT
|
|
50
|
+
metadata:
|
|
51
|
+
homepage_uri: https://github.com/yubrot/factory_bot-with
|
|
52
|
+
source_code_uri: https://github.com/yubrot/factory_bot-with
|
|
53
|
+
changelog_uri: https://github.com/yubrot/factory_bot-with/blob/main/CHANGELOG.md
|
|
54
|
+
rubygems_mfa_required: 'true'
|
|
55
|
+
post_install_message:
|
|
56
|
+
rdoc_options: []
|
|
57
|
+
require_paths:
|
|
58
|
+
- lib
|
|
59
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
60
|
+
requirements:
|
|
61
|
+
- - ">="
|
|
62
|
+
- !ruby/object:Gem::Version
|
|
63
|
+
version: 3.2.0
|
|
64
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - ">="
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: '0'
|
|
69
|
+
requirements: []
|
|
70
|
+
rubygems_version: 3.5.23
|
|
71
|
+
signing_key:
|
|
72
|
+
specification_version: 4
|
|
73
|
+
summary: FactoryBot extension that wraps FactoryBot::Syntax::Methods to make it a
|
|
74
|
+
little easier to use
|
|
75
|
+
test_files: []
|