factory_bot_rails 4.11.1 → 5.0.0.rc1

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: aadc723c1245e45524befc2a6dadbea1481494355aeb8cb457ebc3370c21f967
4
+ data.tar.gz: 3c9300fe987e1ece26492e72f8d20798cba5d38f05c270f5b7967b8c92a4dee8
5
5
  SHA512:
6
- metadata.gz: 72c7160bdcd59e1e76c19aba443cfbeca65343565aa6a811ae42586fd9ac40cc84d08b43f9666c7457861a84f04d6511ba94606d20573b40ccbf22004234bf3d
7
- data.tar.gz: 1b042672955ce17426a880cd3391fb21a43222478009bd4d0c35e83761c367c7f6fff538f24b047f985063f949d6c3f474bba32ab94c0af01007cac907609415
6
+ metadata.gz: 74740e5223ad01bf0f6920b40ef9a0dbb3cce597103f3e25c7e25fb15352c3ca3a988d04016c2f330610773ac3c4574606951d9f43093a151007796c7e558ceb
7
+ data.tar.gz: b2683c67373f1be98ce138da1daed205fc72c77a7283d56c91b0e0af900249658771e3e61c7a25ad12f573d21b826a39a393f1a73ce0566f5fdf5da1384032eb
data/NEWS CHANGED
@@ -1,6 +1,14 @@
1
1
  factory_bot_rails versioning is synced with factory_bot releases. For this reason
2
2
  there might not be any notable changes in new versions of this project.
3
3
 
4
+ 5.0.0
5
+ Added: calling reload! in the Rails console will reload any factory definition files that have changed
6
+ Added: support for custom generator templates
7
+ Added: definition_file_paths configuration option, making it easier to place factories in custom locations
8
+ Changed: namespaced models are now generated inside a directory matching the namespace
9
+ Changed: added newline between factories generated into the same file
10
+ Removed: support for EOL version of Ruby and Rails
11
+
4
12
  4.11.1 (September 7, 2018)
5
13
  Update generator to use dynamic attributes instead of deprecated static attributes
6
14
 
data/README.md CHANGED
@@ -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,56 @@ 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
+ class MyEngine < ::Rails::Engine
70
+ config.factory_bot.definition_file_paths =
71
+ File.expand_path('../factories', __FILE__) if defined?(FactoryBotRails)
72
+ end
73
+ ```
74
+
75
+ You can also disable automatic factory definition loading entirely by
76
+ using an empty array:
77
+
78
+ ```rb
79
+ config.factory_bot.definition_file_paths = []
80
+ ```
81
+
82
+ ### Generators
83
+
84
+ Including factory\_bot\_rails in the development group of your Gemfile
85
+ will cause Rails to generate factories instead of fixtures.
86
+ If you want to disable this feature, you can either move factory\_bot\_rails out
87
+ of the development group of your Gemfile, or add the following configuration:
43
88
 
44
89
  ```ruby
45
90
  config.generators do |g|
@@ -47,8 +92,15 @@ config.generators do |g|
47
92
  end
48
93
  ```
49
94
 
50
- Default factories directory is `test/factories`, or `spec/factories` if
51
- `test_framework` generator is set to `:rspec`; change this behavior with:
95
+ If fixture replacement is enabled and you already have a `test/factories.rb`
96
+ file (or `spec/factories.rb` if using rspec_rails), generated factories will be
97
+ inserted at the top of the existing file.
98
+ Otherwise, factories will be generated in the
99
+ `test/factories` directory (`spec/factories` if using rspec_rails),
100
+ in a file matching the name of the table (e.g. `test/factories/users.rb`).
101
+
102
+ To generate factories in a different directory, you can use the following
103
+ configuration:
52
104
 
53
105
  ```ruby
54
106
  config.generators do |g|
@@ -56,20 +108,38 @@ config.generators do |g|
56
108
  end
57
109
  ```
58
110
 
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.
111
+ Note that factory\_bot\_rails will not automatically load files in custom
112
+ locations unless you add them to `config.factory_bot.defintion_file_paths` as
113
+ well.
62
114
 
63
- factory_bot takes an option `suffix: 'some_suffix'` to generate factories as
64
- `modelname_some_suffix.rb`.
115
+ The suffix option allows you to customize the name of the generated file with a
116
+ suffix:
65
117
 
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`.
118
+ ```ruby
119
+ config.generators do |g|
120
+ g.factory_bot suffix: "factory"
121
+ end
122
+ ```
70
123
 
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).
124
+ This will generate `test/factories/users_factory.rb` instead of
125
+ `test/factories/users.rb`.
126
+
127
+ For even more customization, use the `filename_proc` option:
128
+
129
+ ```ruby
130
+ config.generators do |g|
131
+ g.factory_bot filename_proc: ->(table_name) { "prefix_#{table_name}_suffix" }
132
+ end
133
+ ```
134
+
135
+ To override the [default factory template][], define your own template in
136
+ `lib/templates/factory_bot/models/factories.erb`. This template will have
137
+ access to any methods available in `FactoryBot::Generators::ModelGenerator`.
138
+ Note that factory\_bot\_rails will only use this custom template if you are
139
+ generating each factory in a separate file; it will have no effect if you are
140
+ generating all of your factories in `test/factories.rb` or `spec/factories.rb`.
141
+
142
+ [default factory template]: https://github.com/thoughtbot/factory_bot_rails/tree/master/lib/generators/factory_bot/model/templates/factories.erb
73
143
 
74
144
  ## Contributing
75
145
 
@@ -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,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FactoryBotRails
4
+ class DefinitionFilePaths
5
+ attr_reader :files, :directories
6
+
7
+ def initialize(definition_file_paths)
8
+ @files = []
9
+ @directories = {}
10
+
11
+ definition_file_paths.each do |path|
12
+ @files << "#{path}.rb"
13
+ @directories[path.to_s] = [:rb]
14
+ end
15
+ end
16
+ end
17
+ 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,36 @@
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
19
+ end
20
+
21
+ initializer "factory_bot.register_reloader" do |app|
22
+ Reloader.new(app, config).run
18
23
  end
19
24
 
20
25
  config.after_initialize do
21
26
  FactoryBot.find_definitions
27
+ end
28
+
29
+ private
22
30
 
23
- if defined?(Spring)
24
- Spring.after_fork { FactoryBot.reload }
31
+ def definition_file_paths
32
+ config.factory_bot.definition_file_paths.map do |path|
33
+ Rails.root.join(path)
25
34
  end
26
35
  end
27
36
  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, config)
8
+ @app = app
9
+ @config = config
10
+ end
11
+
12
+ def run
13
+ register_reloader(build_reloader)
14
+ end
15
+
16
+ private
17
+
18
+ attr_reader :app, :config
19
+
20
+ def build_reloader
21
+ paths = DefinitionFilePaths.new(FactoryBot.definition_file_paths)
22
+
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
+ config.to_prepare do
34
+ reloader.execute_if_updated
35
+ end
36
+
37
+ app.reloaders << reloader
38
+ end
39
+ end
40
+ end
@@ -1,14 +1,23 @@
1
- require 'rails/generators/named_base'
1
+ require "rails/generators/named_base"
2
2
 
3
3
  module FactoryBot
4
4
  module Generators
5
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)
8
15
  end
9
16
 
10
17
  def explicit_class_option
11
- ", class: '#{class_name}'" unless class_name == singular_table_name.camelize
18
+ return if class_name == singular_table_name.camelize
19
+
20
+ ", class: '#{class_name}'"
12
21
  end
13
22
  end
14
23
  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
@@ -8,21 +8,21 @@ module FactoryBot
8
8
  :attributes,
9
9
  type: :array,
10
10
  default: [],
11
- banner: "field:type field:type"
11
+ banner: "field:type field:type",
12
12
  )
13
13
 
14
14
  class_option(
15
15
  :dir,
16
16
  type: :string,
17
17
  default: "test/factories",
18
- desc: "The directory or file root where factories belong"
18
+ desc: "The directory or file root where factories belong",
19
19
  )
20
20
 
21
21
  class_option(
22
22
  :suffix,
23
23
  type: :string,
24
24
  default: nil,
25
- desc: "Suffix to add factory file"
25
+ desc: "Suffix to add factory file",
26
26
  )
27
27
 
28
28
  def create_fixture_file
@@ -43,29 +43,22 @@ module FactoryBot
43
43
  insert_into_file(
44
44
  factories_file,
45
45
  factory_definition,
46
- after: "FactoryBot.define do\n"
46
+ after: "FactoryBot.define do\n",
47
47
  )
48
48
  end
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 :#{singular_table_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
@@ -78,7 +71,8 @@ RUBY
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
metadata CHANGED
@@ -1,47 +1,45 @@
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: 5.0.0.rc1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joe Ferris
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-09-07 00:00:00.000000000 Z
11
+ date: 2019-01-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: railties
14
+ name: factory_bot
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 3.0.0
19
+ version: 5.0.0.rc1
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ">="
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 3.0.0
26
+ version: 5.0.0.rc1
27
27
  - !ruby/object:Gem::Dependency
28
- name: factory_bot
28
+ name: railties
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: 4.11.1
33
+ version: 4.2.0
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ">="
39
39
  - !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)
40
+ version: 4.2.0
41
+ description: factory_bot_rails provides integration between factory_bot and rails
42
+ 3 or newer (currently just automatic factory definition loading)
45
43
  email: jferris@thoughtbot.com
46
44
  executables: []
47
45
  extensions: []
@@ -52,13 +50,16 @@ files:
52
50
  - NEWS
53
51
  - README.md
54
52
  - lib/factory_bot_rails.rb
53
+ - lib/factory_bot_rails/definition_file_paths.rb
55
54
  - lib/factory_bot_rails/generator.rb
56
55
  - lib/factory_bot_rails/generators/non_rspec_generator.rb
57
56
  - lib/factory_bot_rails/generators/null_generator.rb
58
57
  - lib/factory_bot_rails/generators/rspec_generator.rb
59
58
  - lib/factory_bot_rails/railtie.rb
59
+ - lib/factory_bot_rails/reloader.rb
60
60
  - lib/generators/factory_bot.rb
61
61
  - lib/generators/factory_bot/model/model_generator.rb
62
+ - lib/generators/factory_bot/model/templates/factories.erb
62
63
  homepage: https://github.com/thoughtbot/factory_bot_rails
63
64
  licenses:
64
65
  - MIT
@@ -74,12 +75,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
74
75
  version: '0'
75
76
  required_rubygems_version: !ruby/object:Gem::Requirement
76
77
  requirements:
77
- - - ">="
78
+ - - ">"
78
79
  - !ruby/object:Gem::Version
79
- version: '0'
80
+ version: 1.3.1
80
81
  requirements: []
81
82
  rubyforge_project:
82
- rubygems_version: 2.7.7
83
+ rubygems_version: 2.7.6
83
84
  signing_key:
84
85
  specification_version: 4
85
86
  summary: factory_bot_rails provides integration between factory_bot and rails 3 or