factory_bot_rails 4.10.0 → 6.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 917aaa31074162b1470cb002cd09f6b2c5f2bafcdfc9865300ebbf8daa6fbfe0
4
- data.tar.gz: 4cab82f61488aae7350a5dd93104ebaa1ec596a0c5c6cb93dd638bb038dfee39
3
+ metadata.gz: c54ddb791dcf6eccf0e7ffe73cad7294a72b7d84a7c9b1c8be1b858e6ff0f209
4
+ data.tar.gz: 5c5b2bd9f09413c8a2cbbf5bba8c46f927c717de9caa798f3dd161cc464961cf
5
5
  SHA512:
6
- metadata.gz: 732c62eb130da9a8a7cc697627b2f22b10bc280e61107d8e95503ffce0489c143447903e59c9ce7b92857eed3368e4229573a892607adc55f27584540bbc6c1b
7
- data.tar.gz: 5e633a2cc4b296e2779c1d828753bf4d951c596a838c6da662c24b0710ad05d8056d48863c7a20a93f37ab66cb9b9dd713d9f4e13671b6ae7a8b0dd34e136d27
6
+ metadata.gz: 7fa8bcf45b0a9ac5bf032676109d341b7ec773479f53e71e3606a39b52a307b60c49d31ada524f25e77822a97724849a475afb5da0fffcefe7a91bb23d01f5cf
7
+ data.tar.gz: 7c78b5c2c241080b9bdbc59f402cb68034df1efe26ede947cc620118b67389272c14f063442946a1f5ea28c64204c273791c276c7b3c363b4cfa3b253bfdf611
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,88 @@
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.1.0 (July 8, 2020)
7
+
8
+ * Changed: factory\_bot dependency to ~> 6.1.0
9
+
10
+ ## 6.0.0 (June 18, 2020)
11
+
12
+ * Fixed: generate a plural factory name when the --force-plural flag is provided
13
+ * Changed: factory\_bot dependency to ~> 6.0.0
14
+ * Removed: `"factory_bot.register_reloader"` initializer, now registering the
15
+ reloader after application initialization
16
+ * Removed: support for EOL versions of Ruby (2.3, 2.4) and Rails (4.2)
17
+
18
+ ## 5.1.1 (September 24, 2019)
19
+
20
+ * Fixed: Ensure definitions do not load before I18n is initialized
21
+
22
+ ## 5.1.0 (September 24, 2019)
23
+
24
+ * Changed: factory\_bot dependency to ~> 5.1.0
25
+
26
+ ## 5.0.2 (April 14, 2019)
27
+
28
+ * Bugfix: Reload factory\_bot whenever the application changes to avoid holding
29
+ onto stale object references
30
+ * Bugfix: Avoid watching project root when no factory definitions exist
31
+
32
+ ## 5.0.1 (February 9, 2019)
33
+
34
+ * Bugfix: Avoid watching files and directories that don't exist (to avoid a
35
+ file watching bug in Rails https://github.com/rails/rails/issues/32700)
36
+
37
+ ## 5.0.0 (February 1, 2019)
38
+
39
+ * Added: calling reload! in the Rails console will reload any factory definition files that have changed
40
+ * Added: support for custom generator templates
41
+ * Added: `definition_file_paths` configuration option, making it easier to place factories in custom locations
42
+ * Changed: namespaced models are now generated inside a directory matching the namespace
43
+ * Changed: added newline between factories generated into the same file
44
+ * Removed: support for EOL version of Ruby and Rails
45
+
46
+ ## 4.11.1 (September 7, 2018)
47
+
48
+ * Update generator to use dynamic attributes instead of deprecated static attributes
49
+
50
+ ## 4.11.0 (August 16, 2018)
51
+
52
+ * No notable changes
53
+
54
+ ## 4.10.0 (May 25, 2018)
55
+
56
+ * No notable changes
57
+
58
+ ## 4.8.2 (October 20, 2017)
59
+
60
+ * Rename factory\_girl\_rails to factory\_bot\_rails
61
+
62
+ ## 4.7.0 (April 1, 2016)
63
+
64
+ * No notable changes
65
+
66
+ ## 4.6.0 (February 1, 2016)
67
+
68
+ * No notable changes
69
+
70
+ ## 4.5.0 (October 17, 2014)
71
+
72
+ * Improved README
73
+
74
+ ## 4.4.1 (February 26, 2014)
75
+
76
+ * Support Spring
77
+
78
+ ## 4.2.1 (February 8, 2013)
79
+
80
+ * Fix bug when configuring FG and RSpec fixture directory
81
+ * Remove debugging
82
+ * Require factory\_girl\_rails explicitly in generator
83
+
84
+ ## 4.2.0 (January 25, 2013)
85
+
86
+ * Add appraisal and get test suite working reliably with turn gem
87
+ * Support MiniTest
88
+ * 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]
1
+ # factory_bot_rails [![Build Status][ci-image]][ci] [![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,9 +12,7 @@ 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
18
  Ruby versions are listed in [`.travis.yml`](.travis.yml).
@@ -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/master/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,43 +113,71 @@ 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
+ ```
70
128
 
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).
129
+ This will generate `test/factories/users_factory.rb` instead of
130
+ `test/factories/users.rb`.
73
131
 
74
- ## Contributing
132
+ For even more customization, use the `filename_proc` option:
75
133
 
76
- Please see [CONTRIBUTING.md](CONTRIBUTING.md).
134
+ ```ruby
135
+ config.generators do |g|
136
+ g.factory_bot filename_proc: ->(table_name) { "prefix_#{table_name}_suffix" }
137
+ end
138
+ ```
77
139
 
78
- ## Credits
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`.
79
146
 
80
- [factory_bot][fb] was originally written by Joe Ferris.
147
+ [default factory template]: https://github.com/thoughtbot/factory_bot_rails/tree/master/lib/generators/factory_bot/model/templates/factories.erb
81
148
 
82
- ![thoughtbot](http://thoughtbot.com/images/tm/logo.png)
149
+ ## Contributing
83
150
 
84
- factory_bot is maintained and funded by [thoughtbot, inc](http://thoughtbot.com/community)
151
+ Please see [CONTRIBUTING.md](CONTRIBUTING.md).
85
152
 
86
- The names and logos for thoughtbot are trademarks of thoughtbot, inc.
153
+ factory_bot_rails was originally written by Joe Ferris and is maintained by thoughtbot. Many improvements and bugfixes were contributed by the [open source
154
+ community](https://github.com/thoughtbot/factory_bot_rails/graphs/contributors).
87
155
 
88
156
  ## License
89
157
 
90
- factory_bot_rails is Copyright © 2008-2016 Joe Ferris and thoughtbot. It is free
158
+ factory_bot_rails is Copyright © 2008-2020 Joe Ferris and thoughtbot. It is free
91
159
  software, and may be redistributed under the terms specified in the
92
160
  [LICENSE](LICENSE) file.
93
161
 
162
+ ## About thoughtbot
163
+
164
+ ![thoughtbot](https://thoughtbot.com/brand_assets/93:44.svg)
165
+
166
+ factory_bot_rails is maintained and funded by thoughtbot, inc.
167
+ The names and logos for thoughtbot are trademarks of thoughtbot, inc.
168
+
169
+ We are passionate about open source software.
170
+ See [our other projects][community].
171
+ We are [available for hire][hire].
172
+
94
173
  [fb]: https://github.com/thoughtbot/factory_bot
95
174
  [ci]: http://travis-ci.org/thoughtbot/factory_bot_rails?branch=master
96
- [ci-image]: https://secure.travis-ci.org/thoughtbot/factory_bot_rails.png
175
+ [ci-image]: https://secure.travis-ci.org/thoughtbot/factory_bot_rails.svg
97
176
  [grade]: https://codeclimate.com/github/thoughtbot/factory_bot_rails
98
- [grade-image]: https://codeclimate.com/github/thoughtbot/factory_bot_rails.png
177
+ [grade-image]: https://codeclimate.com/github/thoughtbot/factory_bot_rails.svg
178
+ [community]: https://thoughtbot.com/community?utm_source=github
179
+ [hire]: https://thoughtbot.com/hire-us?utm_source=github
180
+ [version-image]: https://badge.fury.io/rb/factory_bot_rails.svg
181
+ [version]: https://badge.fury.io/rb/factory_bot_rails
182
+ [hound-image]: https://img.shields.io/badge/Reviewed_by-Hound-8E64B0.svg
183
+ [hound]: https://houndci.com
@@ -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
@@ -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
@@ -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
@@ -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,33 @@
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 "rails"
7
+
8
+ module FactoryBotRails
6
9
  class Railtie < Rails::Railtie
10
+ config.factory_bot = ActiveSupport::OrderedOptions.new
11
+ config.factory_bot.definition_file_paths = FactoryBot.definition_file_paths
7
12
 
8
13
  initializer "factory_bot.set_fixture_replacement" do
9
- FactoryBotRails::Generator.new(config).run
14
+ Generator.new(config).run
10
15
  end
11
16
 
12
17
  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
- ]
18
+ FactoryBot.definition_file_paths = definition_file_paths
18
19
  end
19
20
 
20
- config.after_initialize do
21
+ config.after_initialize do |app|
21
22
  FactoryBot.find_definitions
23
+ Reloader.new(app).run
24
+ end
25
+
26
+ private
22
27
 
23
- if defined?(Spring)
24
- Spring.after_fork { FactoryBot.reload }
28
+ def definition_file_paths
29
+ config.factory_bot.definition_file_paths.map do |path|
30
+ Rails.root.join(path)
25
31
  end
26
32
  end
27
33
  end