factory_bot_rails 4.11.1 → 6.4.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 542cc50fa6c84e06f69ce225f5a2ccdd9b00e51e9406c8d0924e4a1a1283012a
4
- data.tar.gz: 9f9282517549bceb7dfab46f6b46190c715fec59827080981e31d7cae167d00f
3
+ metadata.gz: b4a9a114b3d5f63d0083ef6990ac113e593ff67c19866fce133ecfc0423dd653
4
+ data.tar.gz: 3e8a9c7450996d67fa4f45b7ee949348c261319c7195e01a3a08aaa9ae3bcdfa
5
5
  SHA512:
6
- metadata.gz: 72c7160bdcd59e1e76c19aba443cfbeca65343565aa6a811ae42586fd9ac40cc84d08b43f9666c7457861a84f04d6511ba94606d20573b40ccbf22004234bf3d
7
- data.tar.gz: 1b042672955ce17426a880cd3391fb21a43222478009bd4d0c35e83761c367c7f6fff538f24b047f985063f949d6c3f474bba32ab94c0af01007cac907609415
6
+ metadata.gz: 3c0a4cfe2745a02a9b2b1f8192286db492fc6e0bf850478c456f0b3863cdaecc9901dafc200201e75bc5dbb9bab429fdf3f9705ceb0d4ed7bd5a6758b4805f3f
7
+ data.tar.gz: 957f01135a351596d8221148bca75b61500518fc71f734454cf3051933ccd1f08f1b361de768bc73dd77105e09b747283617bc02f04e33944e02c97415f7beb3
data/CONTRIBUTING.md CHANGED
@@ -35,5 +35,5 @@ Some things that will increase the chance that your pull request is accepted:
35
35
  * Follow our [style guide][style].
36
36
  * Write a [good commit message][commit].
37
37
 
38
- [style]: https://github.com/thoughtbot/guides/tree/master/style
38
+ [style]: https://github.com/thoughtbot/guides/tree/main/general
39
39
  [commit]: http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html
data/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2008-2013 Joe Ferris and thoughtbot, inc.
1
+ Copyright (c) 2008-2020 Joe Ferris and thoughtbot, inc.
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining a copy
4
4
  of this software and associated documentation files (the "Software"), to deal
data/NEWS.md ADDED
@@ -0,0 +1,126 @@
1
+ factory\_bot\_rails versioning is synced with factory\_bot releases. For this reason
2
+ there might not be any notable changes in new versions of this project.
3
+
4
+ # NEWS
5
+
6
+ ## 6.4.2 (November 23, 2023)
7
+ * Fixed: Fix Rails 7.1.2 + monkey-patched ActiveRecord compatibility (Adif
8
+ Sgaid, Benoit Tigeot)
9
+ * Internal: Test against Rails 7.1 (y-yagi)
10
+ * Internal: Fix links to old files after renaming the main branch to `main`
11
+ (y-yagi)
12
+
13
+ ## 6.4.0 (November 17, 2023)
14
+
15
+ * Releasing this for consistency with the factory\_bot dependency.
16
+
17
+ ## 6.3.0 (November 17, 2023)
18
+
19
+ * Changed: reject sequence definitions for ActiveRecord primary keys (Sean
20
+ Doyle).
21
+ * Changed: factory\_bot dependency to ~> 6.4 (Mike Burns).
22
+ * Changed: upgrade dependencies (Daniel Colson).
23
+ * Add: `projections.json` for Rails.vim (Caleb Hearth).
24
+ * Docs: fix broken link (Edu Depetris).
25
+ * Docs: mention Rails generator in docs (Edu Depetris).
26
+ * Docs: fix typo (Yudai Takada).
27
+ * Internal: skip Spring version 2.1.1 due to a bug in that release (Christina
28
+ Entcheva, Daniel Colson).
29
+ * Internal: test against Rails 6.1 (Antonis Berkakis).
30
+ * Internal: test against Ruby 3 (Daniel Colson).
31
+ * Internal: fewer warnings in Cucumber tests (Daniel Colson).
32
+ * Internal: use GitHub Actions for CI (Mathieu Jobin).
33
+ * Internal: a whole bunch of cleanup (Daniel Colson).
34
+ * Internal: fix CI due to a Bundler output change (Mike Burns).
35
+
36
+ ## 6.2.0 (May 7, 2021)
37
+
38
+ * Changed: factory\_bot dependency to ~> 6.2.0
39
+
40
+ ## 6.1.0 (July 8, 2020)
41
+
42
+ * Changed: factory\_bot dependency to ~> 6.1.0
43
+
44
+ ## 6.0.0 (June 18, 2020)
45
+
46
+ * Fixed: generate a plural factory name when the --force-plural flag is provided
47
+ * Changed: factory\_bot dependency to ~> 6.0.0
48
+ * Removed: `"factory_bot.register_reloader"` initializer, now registering the
49
+ reloader after application initialization
50
+ * Removed: support for EOL versions of Ruby (2.3, 2.4) and Rails (4.2)
51
+
52
+ ## 5.2.0 (April 26, 2020)
53
+
54
+ * Changed: factory\_bot dependency to ~> 5.2.0
55
+
56
+ ## 5.1.1 (September 24, 2019)
57
+
58
+ * Fixed: Ensure definitions do not load before I18n is initialized
59
+
60
+ ## 5.1.0 (September 24, 2019)
61
+
62
+ * Changed: factory\_bot dependency to ~> 5.1.0
63
+
64
+ ## 5.0.2 (April 14, 2019)
65
+
66
+ * Bugfix: Reload factory\_bot whenever the application changes to avoid holding
67
+ onto stale object references
68
+ * Bugfix: Avoid watching project root when no factory definitions exist
69
+
70
+ ## 5.0.1 (February 9, 2019)
71
+
72
+ * Bugfix: Avoid watching files and directories that don't exist (to avoid a
73
+ file watching bug in Rails https://github.com/rails/rails/issues/32700)
74
+
75
+ ## 5.0.0 (February 1, 2019)
76
+
77
+ * Added: calling reload! in the Rails console will reload any factory definition files that have changed
78
+ * Added: support for custom generator templates
79
+ * Added: `definition_file_paths` configuration option, making it easier to place factories in custom locations
80
+ * Changed: namespaced models are now generated inside a directory matching the namespace
81
+ * Changed: added newline between factories generated into the same file
82
+ * Removed: support for EOL version of Ruby and Rails
83
+
84
+ ## 4.11.1 (September 7, 2018)
85
+
86
+ * Update generator to use dynamic attributes instead of deprecated static attributes
87
+
88
+ ## 4.11.0 (August 16, 2018)
89
+
90
+ * No notable changes
91
+
92
+ ## 4.10.0 (May 25, 2018)
93
+
94
+ * No notable changes
95
+
96
+ ## 4.8.2 (October 20, 2017)
97
+
98
+ * Rename factory\_girl\_rails to factory\_bot\_rails
99
+
100
+ ## 4.7.0 (April 1, 2016)
101
+
102
+ * No notable changes
103
+
104
+ ## 4.6.0 (February 1, 2016)
105
+
106
+ * No notable changes
107
+
108
+ ## 4.5.0 (October 17, 2014)
109
+
110
+ * Improved README
111
+
112
+ ## 4.4.1 (February 26, 2014)
113
+
114
+ * Support Spring
115
+
116
+ ## 4.2.1 (February 8, 2013)
117
+
118
+ * Fix bug when configuring FG and RSpec fixture directory
119
+ * Remove debugging
120
+ * Require factory\_girl\_rails explicitly in generator
121
+
122
+ ## 4.2.0 (January 25, 2013)
123
+
124
+ * Add appraisal and get test suite working reliably with turn gem
125
+ * Support MiniTest
126
+ * Allow a custom directory for factories to be specified
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # factory_bot_rails [![Build Status][ci-image]][ci] [![Code Climate][grade-image]][grade] [![Gem Version][version-image]][version] [![Reviewed by Hound][hound-image]][hound]
1
+ # factory_bot_rails [![Code Climate][grade-image]][grade] [![Gem Version][version-image]][version]
2
2
 
3
3
  [factory_bot][fb] is a fixtures replacement with a straightforward definition
4
4
  syntax, support for multiple build strategies (saved instances, unsaved
@@ -12,12 +12,10 @@ Check out the [guide](https://github.com/thoughtbot/factory_bot/blob/4-9-0-stabl
12
12
 
13
13
  ## Rails
14
14
 
15
- factory_bot_rails provides Rails integration for [factory_bot][fb].
16
-
17
- Currently, automatic factory definition loading is the only Rails-specific feature.
15
+ factory\_bot\_rails provides Rails integration for [factory_bot][fb].
18
16
 
19
17
  Supported Rails versions are listed in [`Appraisals`](Appraisals). Supported
20
- Ruby versions are listed in [`.travis.yml`](.travis.yml).
18
+ Ruby versions are listed in [`.github/workflows/build.yml`](.github/workflows/build.yml).
21
19
 
22
20
  ## Download
23
21
 
@@ -29,7 +27,7 @@ Gem:
29
27
 
30
28
  ## Configuration
31
29
 
32
- Add `factory_bot_rails` to your Gemfile:
30
+ Add `factory_bot_rails` to your Gemfile in both the test and development groups:
33
31
 
34
32
  ```ruby
35
33
  group :development, :test do
@@ -37,9 +35,61 @@ group :development, :test do
37
35
  end
38
36
  ```
39
37
 
40
- Generators for factories will automatically substitute fixture (and maybe any other
41
- `fixture_replacement` you set). If you want to disable this feature, add the
42
- following to your application.rb file:
38
+ You may want to configure your test suite to include factory\_bot methods; see
39
+ [configuration](https://github.com/thoughtbot/factory_bot/blob/main/GETTING_STARTED.md#configure-your-test-suite).
40
+
41
+ ### Automatic Factory Definition Loading
42
+
43
+ By default, factory\_bot\_rails will automatically load factories
44
+ defined in the following locations,
45
+ relative to the root of the Rails project:
46
+
47
+ ```
48
+ factories.rb
49
+ test/factories.rb
50
+ spec/factories.rb
51
+ factories/*.rb
52
+ test/factories/*.rb
53
+ spec/factories/*.rb
54
+ ```
55
+
56
+ You can configure by adding the following to `config/application.rb` or the
57
+ appropriate environment configuration in `config/environments`:
58
+
59
+ ```ruby
60
+ config.factory_bot.definition_file_paths = ["custom/factories"]
61
+ ```
62
+
63
+ This will cause factory\_bot\_rails to automatically load factories in
64
+ `custom/factories.rb` and `custom/factories/*.rb`.
65
+
66
+ It is possible to use this setting to share factories from a gem:
67
+
68
+ ```rb
69
+ begin
70
+ require 'factory_bot_rails'
71
+ rescue LoadError
72
+ end
73
+
74
+ class MyEngine < ::Rails::Engine
75
+ config.factory_bot.definition_file_paths +=
76
+ [File.expand_path('../factories', __FILE__)] if defined?(FactoryBotRails)
77
+ end
78
+ ```
79
+
80
+ You can also disable automatic factory definition loading entirely by
81
+ using an empty array:
82
+
83
+ ```rb
84
+ config.factory_bot.definition_file_paths = []
85
+ ```
86
+
87
+ ### Generators
88
+
89
+ Including factory\_bot\_rails in the development group of your Gemfile
90
+ will cause Rails to generate factories instead of fixtures.
91
+ If you want to disable this feature, you can either move factory\_bot\_rails out
92
+ of the development group of your Gemfile, or add the following configuration:
43
93
 
44
94
  ```ruby
45
95
  config.generators do |g|
@@ -47,8 +97,15 @@ config.generators do |g|
47
97
  end
48
98
  ```
49
99
 
50
- Default factories directory is `test/factories`, or `spec/factories` if
51
- `test_framework` generator is set to `:rspec`; change this behavior with:
100
+ If fixture replacement is enabled and you already have a `test/factories.rb`
101
+ file (or `spec/factories.rb` if using rspec_rails), generated factories will be
102
+ inserted at the top of the existing file.
103
+ Otherwise, factories will be generated in the
104
+ `test/factories` directory (`spec/factories` if using rspec_rails),
105
+ in a file matching the name of the table (e.g. `test/factories/users.rb`).
106
+
107
+ To generate factories in a different directory, you can use the following
108
+ configuration:
52
109
 
53
110
  ```ruby
54
111
  config.generators do |g|
@@ -56,38 +113,77 @@ config.generators do |g|
56
113
  end
57
114
  ```
58
115
 
59
- If you use factory_bot for fixture replacement, ensure that
60
- factory_bot_rails is available in the development group. If it's not, Rails
61
- will generate standard .yml files instead of factory files.
116
+ Note that factory\_bot\_rails will not automatically load files in custom
117
+ locations unless you add them to `config.factory_bot.definition_file_paths` as
118
+ well.
62
119
 
63
- factory_bot takes an option `suffix: 'some_suffix'` to generate factories as
64
- `modelname_some_suffix.rb`.
120
+ The suffix option allows you to customize the name of the generated file with a
121
+ suffix:
65
122
 
66
- If you use factory_bot for fixture replacement and already have a
67
- `factories.rb` file in the directory that contains your tests,
68
- factory_bot_rails will insert new factory definitions at the top of
69
- `factories.rb`.
123
+ ```ruby
124
+ config.generators do |g|
125
+ g.factory_bot suffix: "factory"
126
+ end
127
+ ```
128
+
129
+ This will generate `test/factories/users_factory.rb` instead of
130
+ `test/factories/users.rb`.
131
+
132
+ For even more customization, use the `filename_proc` option:
133
+
134
+ ```ruby
135
+ config.generators do |g|
136
+ g.factory_bot filename_proc: ->(table_name) { "prefix_#{table_name}_suffix" }
137
+ end
138
+ ```
70
139
 
71
- You may need to configure your test suite to include factory_bot methods; see
72
- [configuration](https://github.com/thoughtbot/factory_bot/blob/master/GETTING_STARTED.md#configure-your-test-suite).
140
+ To override the [default factory template][], define your own template in
141
+ `lib/templates/factory_bot/model/factories.erb`. This template will have
142
+ access to any methods available in `FactoryBot::Generators::ModelGenerator`.
143
+ Note that factory\_bot\_rails will only use this custom template if you are
144
+ generating each factory in a separate file; it will have no effect if you are
145
+ generating all of your factories in `test/factories.rb` or `spec/factories.rb`.
146
+
147
+ Factory\_bot\_rails will add a custom generator:
148
+
149
+ ```shell
150
+ rails generate factory_bot:model NAME [field:type field:type] [options]
151
+ ```
152
+
153
+ [default factory template]: https://github.com/thoughtbot/factory_bot_rails/tree/main/lib/generators/factory_bot/model/templates/factories.erb
154
+
155
+ ### Active Record Configuration
156
+
157
+ By default, FactoryBot will refuse to generate Active Record primary key
158
+ columns. Without additional configuration, an Active Record model treats a
159
+ column named `id` as its primary key.
160
+
161
+ For example, defining an `id` attribute with `add_attribute(:id)`, `id { ... }`,
162
+ or `sequence(:id)` will raise a `FactoryBot::AttributeDefinitionError`.
163
+
164
+ You can disable this behavior by adding the following to `config/application.rb`
165
+ or the appropriate environment configuration in `config/environments`:
166
+
167
+ ```ruby
168
+ config.factory_bot.reject_primary_key_attributes = false
169
+ ```
73
170
 
74
171
  ## Contributing
75
172
 
76
173
  Please see [CONTRIBUTING.md](CONTRIBUTING.md).
77
174
 
78
- factory_bot_rails was originally written by Joe Ferris and is now maintained by Josh
79
- Clayton. Many improvements and bugfixes were contributed by the [open source
175
+ factory_bot_rails was originally written by Joe Ferris and is maintained by thoughtbot. Many improvements and bugfixes were contributed by the [open source
80
176
  community](https://github.com/thoughtbot/factory_bot_rails/graphs/contributors).
81
177
 
82
178
  ## License
83
179
 
84
- factory_bot_rails is Copyright © 2008-2016 Joe Ferris and thoughtbot. It is free
180
+ factory_bot_rails is Copyright © 2008-2020 Joe Ferris and thoughtbot. It is free
85
181
  software, and may be redistributed under the terms specified in the
86
182
  [LICENSE](LICENSE) file.
87
183
 
88
184
  ## About thoughtbot
89
185
 
90
- ![thoughtbot](https://thoughtbot.com/logo.png)
186
+ ![thoughtbot](https://thoughtbot.com/brand_assets/93:44.svg)
91
187
 
92
188
  factory_bot_rails is maintained and funded by thoughtbot, inc.
93
189
  The names and logos for thoughtbot are trademarks of thoughtbot, inc.
@@ -97,8 +193,6 @@ See [our other projects][community].
97
193
  We are [available for hire][hire].
98
194
 
99
195
  [fb]: https://github.com/thoughtbot/factory_bot
100
- [ci]: http://travis-ci.org/thoughtbot/factory_bot_rails?branch=master
101
- [ci-image]: https://secure.travis-ci.org/thoughtbot/factory_bot_rails.svg
102
196
  [grade]: https://codeclimate.com/github/thoughtbot/factory_bot_rails
103
197
  [grade-image]: https://codeclimate.com/github/thoughtbot/factory_bot_rails.svg
104
198
  [community]: https://thoughtbot.com/community?utm_source=github
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FactoryBotRails
4
+ class DefinitionFilePaths
5
+ def initialize(definition_file_paths)
6
+ @files = []
7
+ @directories = {}
8
+
9
+ definition_file_paths.each do |path|
10
+ @files << "#{path}.rb"
11
+ @directories[path.to_s] = [:rb]
12
+ end
13
+ end
14
+
15
+ def directories
16
+ @directories.select { |path| Dir.exist?(path) }
17
+ end
18
+
19
+ def files
20
+ @files.select { |file| File.exist?(file) }
21
+ end
22
+
23
+ def any?
24
+ directories.any? || files.any?
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,18 @@
1
+ module FactoryBotRails
2
+ class FactoryValidator
3
+ class ActiveRecordValidator
4
+ def validate!(payload)
5
+ attributes, for_class = payload.values_at(:attributes, :class)
6
+ attributes.each do |attribute|
7
+ if for_class < ActiveRecord::Base && for_class.primary_key == attribute.name.to_s
8
+ raise FactoryBot::AttributeDefinitionError, <<~ERROR
9
+ Attribute generates #{for_class.primary_key.inspect} primary key for #{for_class.name}"
10
+
11
+ Do not define #{for_class.primary_key.inspect}. Instead, rely on the database to generate it.
12
+ ERROR
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,39 @@
1
+ module FactoryBotRails
2
+ class FactoryValidator
3
+ def initialize(validators = [])
4
+ @validators = Array(validators)
5
+ end
6
+
7
+ def add_validator(validator)
8
+ @validators << validator
9
+ end
10
+
11
+ def run
12
+ ActiveSupport::Notifications.subscribe("factory_bot.compile_factory", &validate_compiled_factory)
13
+ end
14
+
15
+ private
16
+
17
+ def validate_compiled_factory
18
+ if Rails.version >= "6.0"
19
+ rails_6_0_support
20
+ else
21
+ rails_5_2_support
22
+ end
23
+ end
24
+
25
+ def rails_6_0_support
26
+ proc do |event|
27
+ @validators.each { |validator| validator.validate!(event.payload) }
28
+ end
29
+ end
30
+
31
+ def rails_5_2_support
32
+ proc do |*notification_event_arguments|
33
+ event = ActiveSupport::Notifications::Event.new(*notification_event_arguments)
34
+
35
+ rails_6_0_support.call(event)
36
+ end
37
+ end
38
+ end
39
+ end
@@ -1,15 +1,11 @@
1
- require 'factory_bot_rails/generators/rspec_generator'
2
- require 'factory_bot_rails/generators/non_rspec_generator'
3
- require 'factory_bot_rails/generators/null_generator'
1
+ require "factory_bot_rails/generators/rspec_generator"
2
+ require "factory_bot_rails/generators/non_rspec_generator"
3
+ require "factory_bot_rails/generators/null_generator"
4
4
 
5
5
  module FactoryBotRails
6
6
  class Generator
7
7
  def initialize(config)
8
- @generators = if config.respond_to?(:app_generators)
9
- config.app_generators
10
- else
11
- config.generators
12
- end
8
+ @generators = config.app_generators
13
9
  end
14
10
 
15
11
  def run
@@ -17,14 +13,12 @@ module FactoryBotRails
17
13
  end
18
14
 
19
15
  def generator
20
- if factory_bot_disabled?
21
- Generators::NullGenerator
16
+ return Generators::NullGenerator if factory_bot_disabled?
17
+
18
+ if test_framework == :rspec
19
+ Generators::RSpecGenerator
22
20
  else
23
- if test_framework == :rspec
24
- Generators::RSpecGenerator
25
- else
26
- Generators::NonRSpecGenerator
27
- end
21
+ Generators::NonRSpecGenerator
28
22
  end
29
23
  end
30
24
 
@@ -6,7 +6,11 @@ module FactoryBotRails
6
6
  end
7
7
 
8
8
  def run
9
- @generators.test_framework test_framework, fixture: false, fixture_replacement: :factory_bot
9
+ @generators.test_framework(
10
+ test_framework,
11
+ fixture: false,
12
+ fixture_replacement: :factory_bot
13
+ )
10
14
  end
11
15
 
12
16
  private
@@ -1,7 +1,7 @@
1
1
  module FactoryBotRails
2
2
  module Generators
3
3
  class NullGenerator
4
- def initialize(generators)
4
+ def initialize(*)
5
5
  end
6
6
 
7
7
  def run
@@ -6,7 +6,10 @@ module FactoryBotRails
6
6
  end
7
7
 
8
8
  def run
9
- @generators.fixture_replacement fixture_replacement_setting, dir: factory_bot_directory
9
+ @generators.fixture_replacement(
10
+ fixture_replacement_setting,
11
+ dir: factory_bot_directory
12
+ )
10
13
  end
11
14
 
12
15
  private
@@ -16,7 +19,11 @@ module FactoryBotRails
16
19
  end
17
20
 
18
21
  def factory_bot_directory
19
- @generators.options.fetch(:factory_bot, {}).fetch(:dir, 'spec/factories')
22
+ factory_bot_options.fetch(:dir, "spec/factories")
23
+ end
24
+
25
+ def factory_bot_options
26
+ @generators.options.fetch(:factory_bot, {})
20
27
  end
21
28
  end
22
29
  end
@@ -1,27 +1,49 @@
1
- require 'factory_bot'
2
- require 'factory_bot_rails/generator'
3
- require 'rails'
1
+ # frozen_string_literal: true
4
2
 
5
- module FactoryBot
3
+ require "factory_bot"
4
+ require "factory_bot_rails/generator"
5
+ require "factory_bot_rails/reloader"
6
+ require "factory_bot_rails/factory_validator"
7
+ require "rails"
8
+
9
+ module FactoryBotRails
6
10
  class Railtie < Rails::Railtie
11
+ config.factory_bot = ActiveSupport::OrderedOptions.new
12
+ config.factory_bot.definition_file_paths = FactoryBot.definition_file_paths
13
+ config.factory_bot.reject_primary_key_attributes = true
14
+ config.factory_bot.validator = FactoryBotRails::FactoryValidator.new
7
15
 
8
16
  initializer "factory_bot.set_fixture_replacement" do
9
- FactoryBotRails::Generator.new(config).run
17
+ Generator.new(config).run
10
18
  end
11
19
 
12
20
  initializer "factory_bot.set_factory_paths" do
13
- FactoryBot.definition_file_paths = [
14
- Rails.root.join('factories'),
15
- Rails.root.join('test', 'factories'),
16
- Rails.root.join('spec', 'factories')
17
- ]
21
+ FactoryBot.definition_file_paths = definition_file_paths
22
+ end
23
+
24
+ initializer "factory_bot.reject_primary_key_attributes" do
25
+ ActiveSupport.on_load :active_record do
26
+ config = Rails.configuration.factory_bot
27
+
28
+ if config.reject_primary_key_attributes
29
+ require "factory_bot_rails/factory_validator/active_record_validator"
30
+
31
+ config.validator.add_validator FactoryValidator::ActiveRecordValidator.new
32
+ end
33
+ end
18
34
  end
19
35
 
20
- config.after_initialize do
36
+ config.after_initialize do |app|
21
37
  FactoryBot.find_definitions
38
+ Reloader.new(app).run
39
+ app.config.factory_bot.validator.run
40
+ end
41
+
42
+ private
22
43
 
23
- if defined?(Spring)
24
- Spring.after_fork { FactoryBot.reload }
44
+ def definition_file_paths
45
+ config.factory_bot.definition_file_paths.map do |path|
46
+ Rails.root.join(path)
25
47
  end
26
48
  end
27
49
  end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "factory_bot_rails/definition_file_paths"
4
+
5
+ module FactoryBotRails
6
+ class Reloader
7
+ def initialize(app)
8
+ @app = app
9
+ @paths = DefinitionFilePaths.new(FactoryBot.definition_file_paths)
10
+ end
11
+
12
+ def run
13
+ return unless @paths.any?
14
+
15
+ register_reloader(build_reloader)
16
+ end
17
+
18
+ private
19
+
20
+ attr_reader :app
21
+
22
+ def build_reloader
23
+ reloader_class.new(@paths.files, @paths.directories) do
24
+ FactoryBot.reload
25
+ end
26
+ end
27
+
28
+ def reloader_class
29
+ app.config.file_watcher
30
+ end
31
+
32
+ def register_reloader(reloader)
33
+ app.reloader.to_prepare do
34
+ reloader.execute
35
+ end
36
+
37
+ app.reloaders << reloader
38
+ end
39
+ end
40
+ end
@@ -1,4 +1,4 @@
1
- require 'factory_bot_rails/railtie'
1
+ require "factory_bot_rails/railtie"
2
2
 
3
3
  module FactoryBotRails
4
4
  end
@@ -1,5 +1,5 @@
1
- require 'generators/factory_bot'
2
- require 'factory_bot_rails'
1
+ require "generators/factory_bot"
2
+ require "factory_bot_rails"
3
3
 
4
4
  module FactoryBot
5
5
  module Generators
@@ -49,36 +49,30 @@ module FactoryBot
49
49
 
50
50
  def create_factory_file
51
51
  file = File.join(options[:dir], "#{filename}.rb")
52
- create_file(file, single_file_factory_definition)
52
+ template "factories.erb", file
53
53
  end
54
54
 
55
55
  def factory_definition
56
- <<-RUBY
57
- factory :#{singular_table_name}#{explicit_class_option} do
58
- #{factory_attributes.gsub(/^/, " ")}
59
- end
60
- RUBY
61
- end
56
+ <<~RUBY
57
+ factory :#{factory_name}#{explicit_class_option} do
58
+ #{factory_attributes.gsub(/^/, " ")}
59
+ end
62
60
 
63
- def single_file_factory_definition
64
- <<-RUBY
65
- FactoryBot.define do
66
- #{factory_definition.chomp}
67
- end
68
- RUBY
61
+ RUBY
69
62
  end
70
63
 
71
64
  def factory_attributes
72
- attributes.map do |attribute|
65
+ attributes.map { |attribute|
73
66
  "#{attribute.name} { #{attribute.default.inspect} }"
74
- end.join("\n")
67
+ }.join("\n")
75
68
  end
76
69
 
77
70
  def filename
78
71
  if factory_bot_options[:filename_proc].present?
79
72
  factory_bot_options[:filename_proc].call(table_name)
80
73
  else
81
- [table_name, filename_suffix].compact.join('_')
74
+ name = File.join(class_path, plural_name)
75
+ [name, filename_suffix].compact.join("_")
82
76
  end
83
77
  end
84
78
 
@@ -91,8 +85,7 @@ RUBY
91
85
  end
92
86
 
93
87
  def generators
94
- config = FactoryBot::Railtie.config
95
- config.respond_to?(:app_generators) ? config.app_generators : config.generators
88
+ FactoryBotRails::Railtie.config.app_generators
96
89
  end
97
90
  end
98
91
  end
@@ -0,0 +1,3 @@
1
+ FactoryBot.define do
2
+ <%= factory_definition.rstrip %>
3
+ end
@@ -1,14 +1,27 @@
1
- require 'rails/generators/named_base'
1
+ require "rails/generators/named_base"
2
2
 
3
3
  module FactoryBot
4
4
  module Generators
5
- class Base < Rails::Generators::NamedBase #:nodoc:
5
+ class Base < Rails::Generators::NamedBase # :nodoc:
6
6
  def self.source_root
7
- @_factory_bot_source_root ||= File.expand_path(File.join(File.dirname(__FILE__), 'factory_bot', generator_name, 'templates'))
7
+ path = File.join(
8
+ File.dirname(__FILE__),
9
+ "factory_bot",
10
+ generator_name,
11
+ "templates"
12
+ )
13
+
14
+ File.expand_path(path)
15
+ end
16
+
17
+ def factory_name
18
+ class_name.gsub("::", "").underscore
8
19
  end
9
20
 
10
21
  def explicit_class_option
11
- ", class: '#{class_name}'" unless class_name == singular_table_name.camelize
22
+ return if class_name.underscore == factory_name
23
+
24
+ ", class: '#{class_name}'"
12
25
  end
13
26
  end
14
27
  end
@@ -0,0 +1,26 @@
1
+ {
2
+ "spec/factories/*.rb": {
3
+ "alternate": "app/models/{singular}.rb",
4
+ "collection": "model",
5
+ "command": "factory",
6
+ "template": [
7
+ "FactoryBot.define do",
8
+ " factory :{singular} do",
9
+ " end",
10
+ "end"
11
+ ],
12
+ "test": "spec/models/{singular}_spec.rb"
13
+ },
14
+ "test/factories/*.rb": {
15
+ "alternate": "app/models/{singular}.rb",
16
+ "collection": "model",
17
+ "command": "factory",
18
+ "template": [
19
+ "FactoryBot.define do",
20
+ " factory :{singular} do",
21
+ " end",
22
+ "end"
23
+ ],
24
+ "test": "test/models/{singular}_test.rb"
25
+ }
26
+ }
metadata CHANGED
@@ -1,47 +1,73 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: factory_bot_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.11.1
4
+ version: 6.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joe Ferris
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-09-07 00:00:00.000000000 Z
11
+ date: 2023-11-23 00:00:00.000000000 Z
12
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.4'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '6.4'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: railties
15
29
  requirement: !ruby/object:Gem::Requirement
16
30
  requirements:
17
31
  - - ">="
18
32
  - !ruby/object:Gem::Version
19
- version: 3.0.0
33
+ version: 5.0.0
20
34
  type: :runtime
21
35
  prerelease: false
22
36
  version_requirements: !ruby/object:Gem::Requirement
23
37
  requirements:
24
38
  - - ">="
25
39
  - !ruby/object:Gem::Version
26
- version: 3.0.0
40
+ version: 5.0.0
27
41
  - !ruby/object:Gem::Dependency
28
- name: factory_bot
42
+ name: sqlite3
29
43
  requirement: !ruby/object:Gem::Requirement
30
44
  requirements:
31
- - - "~>"
45
+ - - ">="
32
46
  - !ruby/object:Gem::Version
33
- version: 4.11.1
34
- type: :runtime
47
+ version: '0'
48
+ type: :development
35
49
  prerelease: false
36
50
  version_requirements: !ruby/object:Gem::Requirement
37
51
  requirements:
38
- - - "~>"
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activerecord
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: 5.0.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
39
67
  - !ruby/object:Gem::Version
40
- version: 4.11.1
41
- description: |-
42
- factory_bot_rails provides integration between
43
- factory_bot and rails 3 or newer (currently just automatic factory definition
44
- loading)
68
+ version: 5.0.0
69
+ description: factory_bot_rails provides integration between factory_bot and rails
70
+ 5.0 or newer
45
71
  email: jferris@thoughtbot.com
46
72
  executables: []
47
73
  extensions: []
@@ -49,21 +75,27 @@ extra_rdoc_files: []
49
75
  files:
50
76
  - CONTRIBUTING.md
51
77
  - LICENSE
52
- - NEWS
78
+ - NEWS.md
53
79
  - README.md
54
80
  - lib/factory_bot_rails.rb
81
+ - lib/factory_bot_rails/definition_file_paths.rb
82
+ - lib/factory_bot_rails/factory_validator.rb
83
+ - lib/factory_bot_rails/factory_validator/active_record_validator.rb
55
84
  - lib/factory_bot_rails/generator.rb
56
85
  - lib/factory_bot_rails/generators/non_rspec_generator.rb
57
86
  - lib/factory_bot_rails/generators/null_generator.rb
58
87
  - lib/factory_bot_rails/generators/rspec_generator.rb
59
88
  - lib/factory_bot_rails/railtie.rb
89
+ - lib/factory_bot_rails/reloader.rb
60
90
  - lib/generators/factory_bot.rb
61
91
  - lib/generators/factory_bot/model/model_generator.rb
92
+ - lib/generators/factory_bot/model/templates/factories.erb
93
+ - lib/rails/projections.json
62
94
  homepage: https://github.com/thoughtbot/factory_bot_rails
63
95
  licenses:
64
96
  - MIT
65
97
  metadata: {}
66
- post_install_message:
98
+ post_install_message:
67
99
  rdoc_options: []
68
100
  require_paths:
69
101
  - lib
@@ -78,10 +110,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
78
110
  - !ruby/object:Gem::Version
79
111
  version: '0'
80
112
  requirements: []
81
- rubyforge_project:
82
- rubygems_version: 2.7.7
83
- signing_key:
113
+ rubygems_version: 3.4.13
114
+ signing_key:
84
115
  specification_version: 4
85
- summary: factory_bot_rails provides integration between factory_bot and rails 3 or
86
- newer
116
+ summary: factory_bot_rails provides integration between factory_bot and rails 5.0
117
+ or newer
87
118
  test_files: []
data/NEWS DELETED
@@ -1,36 +0,0 @@
1
- factory_bot_rails versioning is synced with factory_bot releases. For this reason
2
- there might not be any notable changes in new versions of this project.
3
-
4
- 4.11.1 (September 7, 2018)
5
- Update generator to use dynamic attributes instead of deprecated static attributes
6
-
7
- 4.11.0 (August 16, 2018)
8
- No notable changes
9
-
10
- 4.10.0 (May 25, 2018)
11
- No notable changes
12
-
13
- 4.8.2 (October 20, 2017)
14
- Rename factory_girl_rails to factory_bot_rails
15
-
16
- 4.7.0 (April 1, 2016)
17
- No notable changes
18
-
19
- 4.6.0 (February 1, 2016)
20
- No notable changes
21
-
22
- 4.5.0 (October 17, 2014)
23
- Improved README
24
-
25
- 4.4.1 (February 26, 2014)
26
- Support Spring
27
-
28
- 4.2.1 (February 8, 2013)
29
- Fix bug when configuring FG and RSpec fixture directory
30
- Remove debugging
31
- Require factory_girl_rails explicitly in generator
32
-
33
- 4.2.0 (January 25, 2013)
34
- Add appraisal and get test suite working reliably with turn gem
35
- Support MiniTest
36
- Allow a custom directory for factories to be specified