factory_bot-preload 0.2.2 → 0.3.0

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: 40f6db7436f6c2e5af62b1432b8d4c2e4910e68aa2ae6bfe3ffd9fb7d37540f8
4
- data.tar.gz: e03e9fcbc0772227d7f05032422cfea1488442b692ab873b0d2c4edd03855500
3
+ metadata.gz: d637fe4bb5ac2873148d2fb9c64772f5b5dcffad6eb317701bf56c67ad2131ce
4
+ data.tar.gz: 207e9f509ef305d20d11e3d3f8a3a663e62d7408064bb9f6a240d8a32b4753b2
5
5
  SHA512:
6
- metadata.gz: 4b5095f45aee005e1b1377d98a9649c8c46ee96a89218e46f06aa6f5e8c632a1a09b0db76c52fc7738c53ce6e4f67897d98829810dbc02ec2061fb6a612278cc
7
- data.tar.gz: 7607063d3be8e6af9aec024b8fe286442ae858d84f495cdec6679744af7a5fd2ff978dfef68faa6b52c9609dee6bf6d691788c7f094598dc621d5d4e791ab772
6
+ metadata.gz: 246ed765e74d0205c3a16cb2da2f83f783b627762eea4044b2cb028eaa31d36af6ba7b1b6ec97f47465856170a214a493eb8702b701406028caaafa6e96391e2
7
+ data.tar.gz: 5185f494d6cf186e9396e574f92eef8b77143edc14ae723175e7a27f2b2dcaf66493bb58cfe416aab5be68def1b7626b1f9cd2391b2a9ec6ff87ba0db8813970
@@ -1,5 +1,4 @@
1
1
  ---
2
-
3
2
  language: ruby
4
3
  sudo: false
5
4
  cache: bundler
@@ -10,6 +9,7 @@ before_script:
10
9
  - chmod +x ./cc-test-reporter
11
10
  - "./cc-test-reporter before-build"
12
11
  rvm:
12
+ - 2.7.0
13
13
  - 2.6.5
14
14
  - 2.5.7
15
15
  gemfile:
@@ -21,4 +21,4 @@ after_script:
21
21
  - "./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT"
22
22
  env:
23
23
  global:
24
- - secure: Euebw1utbb/4mu9AwgmnNk1cF4flaUVxterXl2yG9STc3oR9e8IczV+r3107CuIu57O6xbi5WarzSgyASSmC/0G9vhT3OaMx0lAY55zy93LaYbkcbcaAYCfbw6h1RHZ1pA3K4SVw/+n+p2jg9xFTTRLUmOHb2vXHH79WAo+Ri9s=
24
+ secure: ABKL9+QRkF2+B8kQicumx122YSivY0L4yIZnbr5PxoRkgWdUVqyDPmhkz6yL4vhu53dRciKRLel1r+6QZ7rGDxRA0sOOSVyOw3ciyGSgIUFPTe8hO/r4wVKjCqoqK35gzd96x8DUSXBNMoTxFbgAXr7EajMMX8FQY5KjIomVloA=
data/README.md CHANGED
@@ -30,13 +30,17 @@ gem "pg"
30
30
 
31
31
  group :test, :development do
32
32
  gem "factory_bot"
33
- gem "factory_bot-preload"
33
+ gem "factory_bot-preload", require: false
34
34
  end
35
35
  ```
36
36
 
37
+ Notice that adding `require: false` is important; otherwise you won't be able to
38
+ run commands such as `rails db:test:prepare`.
39
+
37
40
  ### RSpec Setup
38
41
 
39
- On your `spec/spec_helper.rb` file, make sure that transactional fixtures are enabled. Here's is my file without all those RSpec comments:
42
+ On your `spec/spec_helper.rb` file, make sure that transactional fixtures are
43
+ enabled. Here's is my file without all those RSpec comments:
40
44
 
41
45
  ```ruby
42
46
  ENV["RAILS_ENV"] ||= "test"
@@ -57,9 +61,21 @@ RSpec.configure do |config|
57
61
  end
58
62
  ```
59
63
 
64
+ You may want to configure the generated helper names. For instance, imagine you
65
+ have a namespace like `MyApp::Models::User`. That'd generate a helper method
66
+ like `myapp_models_user`. If you don't have conflicting names, you can strip
67
+ `myapp_models_` like this:
68
+
69
+ ```ruby
70
+ FactoryBot::Preload.helper_name = lambda do |class_name, helper_name|
71
+ helper_name.gsub(/^myapp_models_/, "")
72
+ end
73
+ ```
74
+
60
75
  ### Minitest Setup
61
76
 
62
- On your `test/test_helper.rb` file, make sure that transaction fixtures are enabled. Here's what your file may look like:
77
+ On your `test/test_helper.rb` file, make sure that transaction fixtures are
78
+ enabled. Here's what your file may look like:
63
79
 
64
80
  ```ruby
65
81
  ENV["RAILS_ENV"] ||= "test"
@@ -88,7 +104,9 @@ FactoryBot::Preload.minitest
88
104
 
89
105
  ### Usage
90
106
 
91
- Create your factories and load it from your setup file (either `test/test_helper.rb` or `spec/spec_helper.rb`) You may have something like this:
107
+ Create your factories and load it from your setup file (either
108
+ `test/test_helper.rb` or `spec/spec_helper.rb`) You may have something like
109
+ this:
92
110
 
93
111
  ```ruby
94
112
  FactoryBot.define do
@@ -151,7 +169,8 @@ FactoryBot.define do
151
169
  end
152
170
  ```
153
171
 
154
- Like Rails fixtures, FBP will define methods for each model. You can use it on your examples and alike.
172
+ Like Rails fixtures, FBP will define methods for each model. You can use it on
173
+ your examples and alike.
155
174
 
156
175
  ```ruby
157
176
  require "test_helper"
@@ -5,28 +5,37 @@ require "active_record"
5
5
 
6
6
  module FactoryBot
7
7
  module Preload
8
- require "factory_bot/preload/helpers"
9
- require "factory_bot/preload/version"
10
- require "factory_bot/preload/rspec" if defined?(RSpec)
11
- require "factory_bot/preload/minitest" if defined?(Minitest)
12
- require "factory_bot/preload/extension"
13
-
14
- ActiveSupport.on_load(:after_initialize) do
15
- ::FactoryBot::Preload::Helpers.load_models
16
- ::FactoryBot::SyntaxRunner.include ::FactoryBot::Preload::Helpers
17
- end
18
-
19
8
  class << self
20
9
  attr_accessor :preloaders
21
10
  attr_accessor :factories
22
11
  attr_accessor :record_ids
23
12
  attr_accessor :clean_with
13
+ attr_accessor :default_helper_name
14
+ attr_accessor :helper_name
15
+ attr_accessor :reserved_tables
24
16
  end
25
17
 
26
18
  self.preloaders = []
27
19
  self.factories = {}
28
20
  self.record_ids = {}
29
21
  self.clean_with = :truncation
22
+ self.default_helper_name = ->(_class_name, helper_name) { helper_name }
23
+ self.helper_name = default_helper_name
24
+ self.reserved_tables = %w[
25
+ ar_internal_metadata
26
+ schema_migrations
27
+ ]
28
+
29
+ require "factory_bot/preload/helpers"
30
+ require "factory_bot/preload/version"
31
+ require "factory_bot/preload/rspec" if defined?(RSpec)
32
+ require "factory_bot/preload/minitest" if defined?(Minitest)
33
+ require "factory_bot/preload/extension"
34
+
35
+ ActiveSupport.on_load(:after_initialize) do
36
+ ::FactoryBot::Preload::Helpers.load_models
37
+ ::FactoryBot::SyntaxRunner.include ::FactoryBot::Preload::Helpers
38
+ end
30
39
 
31
40
  def self.active_record
32
41
  ActiveRecord::Base
@@ -67,7 +76,7 @@ module FactoryBot
67
76
 
68
77
  def self.active_record_names
69
78
  names = active_record.descendants.collect(&:table_name).uniq.compact
70
- reserved_tables = %w[ar_internal_metadata schema_migrations]
79
+
71
80
  names.reject {|name| reserved_tables.include?(name) }
72
81
  end
73
82
 
@@ -15,9 +15,16 @@ module FactoryBot
15
15
 
16
16
  def self.define_helper_methods
17
17
  ActiveRecord::Base.descendants.each do |model|
18
- method_name = model.name.underscore.tr("/", "_").pluralize
18
+ next if FactoryBot::Preload.reserved_tables.include?(model.table_name)
19
19
 
20
- define_method(method_name) do |name|
20
+ helper_name = model.name.underscore.tr("/", "_").pluralize
21
+
22
+ helper_name = FactoryBot::Preload.helper_name.call(
23
+ model.name,
24
+ helper_name
25
+ )
26
+
27
+ define_method(helper_name) do |name|
21
28
  factory(name, model)
22
29
  end
23
30
  end
@@ -4,8 +4,8 @@ module FactoryBot
4
4
  module Preload
5
5
  module Version
6
6
  MAJOR = 0
7
- MINOR = 2
8
- PATCH = 2
7
+ MINOR = 3
8
+ PATCH = 0
9
9
  STRING = "#{MAJOR}.#{MINOR}.#{PATCH}"
10
10
  end
11
11
  end
@@ -1,8 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "spec_helper"
2
4
 
3
5
  describe FactoryBot::Preload do
4
6
  it "queues preloader block" do
5
- block = proc {}
7
+ block = proc { }
6
8
  FactoryBot.preload(&block)
7
9
  expect(FactoryBot::Preload.preloaders).to include(block)
8
10
  end
@@ -45,6 +47,33 @@ describe FactoryBot::Preload do
45
47
  expect { FactoryBot::Preload.clean }.to raise_error(%[Couldn't find invalid clean type])
46
48
  end
47
49
 
50
+ it "ignores reserved table names when creating helpers" do
51
+ mod = Module.new do
52
+ include FactoryBot::Preload::Helpers
53
+ end
54
+
55
+ instance = Object.new.extend(mod)
56
+
57
+ expect(instance).not_to respond_to(:active_record_internal_metadata)
58
+ expect(instance).not_to respond_to(:active_record_schema_migrations)
59
+ expect(instance).not_to respond_to(:primary_schema_migrations)
60
+ end
61
+
62
+ it "processes helper name" do
63
+ FactoryBot::Preload.helper_name = lambda do |_class_name, helper_name|
64
+ helper_name.gsub(/^models_/, "")
65
+ end
66
+
67
+ mod = Module.new do
68
+ include FactoryBot::Preload::Helpers
69
+ end
70
+
71
+ instance = Object.new.extend(mod)
72
+
73
+ expect(instance).to respond_to(:assets)
74
+ expect(assets(:asset).name).to eq("Some asset")
75
+ end
76
+
48
77
  example "association uses preloaded record" do
49
78
  expect(build(:skill).user).to eq(users(:john))
50
79
  end
@@ -1,7 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "simplecov"
4
- SimpleCov.start
4
+ SimpleCov.start do
5
+ add_filter(/schema\.rb/)
6
+ end
5
7
 
6
8
  ENV["RAILS_ENV"] = "test"
7
9
  ENV["BUNDLE_GEMFILE"] = File.dirname(__FILE__) + "/../Gemfile"
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Models
4
+ class Asset < ActiveRecord::Base
5
+ end
6
+ end
@@ -7,7 +7,8 @@ ActiveRecord::Schema.define(version: 0) do
7
7
  drop_table :preloads
8
8
  drop_table :categories
9
9
  drop_table :categories_users
10
- rescue Exception => e
10
+ drop_table :assets
11
+ rescue Exception => error
11
12
  end
12
13
 
13
14
  create_table :users do |t|
@@ -33,4 +34,8 @@ ActiveRecord::Schema.define(version: 0) do
33
34
  t.references :category
34
35
  t.references :user
35
36
  end
37
+
38
+ create_table :assets do |t|
39
+ t.string :name
40
+ end
36
41
  end
@@ -16,9 +16,14 @@ FactoryBot.define do
16
16
  name { "My Preload" }
17
17
  end
18
18
 
19
+ factory :asset, class: "Models::Asset" do
20
+ name { "Some asset" }
21
+ end
22
+
19
23
  preload do
20
24
  factory(:john) { create(:user) }
21
25
  factory(:ruby) { create(:skill, user: users(:john)) }
22
26
  factory(:my) { create(:preload) }
27
+ factory(:asset) { create(:asset) }
23
28
  end
24
29
  end
@@ -5,6 +5,7 @@ require "test_helper"
5
5
  class PreloadTest < ActiveSupport::TestCase
6
6
  setup do
7
7
  FactoryBot::Preload.clean_with = :truncation
8
+ FactoryBot::Preload.helper_name = FactoryBot::Preload.default_helper_name
8
9
  end
9
10
 
10
11
  test "queues preloader block" do
@@ -100,4 +101,31 @@ class PreloadTest < ActiveSupport::TestCase
100
101
  assert_equal 0, users(:john).invitations
101
102
  refute users(:john).frozen?
102
103
  end
104
+
105
+ test "ignores reserved table names" do
106
+ mod = Module.new do
107
+ include FactoryBot::Preload::Helpers
108
+ end
109
+
110
+ instance = Object.new.extend(mod)
111
+
112
+ refute_respond_to instance, :active_record_internal_metadata
113
+ refute_respond_to instance, :active_record_schema_migrations
114
+ refute_respond_to instance, :primary_schema_migrations
115
+ end
116
+
117
+ test "processes helper name" do
118
+ FactoryBot::Preload.helper_name = lambda do |_class_name, helper_name|
119
+ helper_name.gsub(/^models_/, "")
120
+ end
121
+
122
+ mod = Module.new do
123
+ include FactoryBot::Preload::Helpers
124
+ end
125
+
126
+ instance = Object.new.extend(mod)
127
+
128
+ assert_respond_to instance, :assets
129
+ assert_equal "Some asset", assets(:asset).name
130
+ end
103
131
  end
@@ -1,7 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "simplecov"
4
- SimpleCov.start
4
+ SimpleCov.start do
5
+ add_filter(/schema\.rb/)
6
+ end
5
7
 
6
8
  ENV["RAILS_ENV"] = "test"
7
9
  ENV["BUNDLE_GEMFILE"] = File.dirname(__FILE__) + "/../Gemfile"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: factory_bot-preload
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nando Vieira
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-11-28 00:00:00.000000000 Z
11
+ date: 2020-02-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -205,6 +205,7 @@ files:
205
205
  - spec/factory_bot/preload_spec.rb
206
206
  - spec/spec_helper.rb
207
207
  - spec/support/app/app/models/category.rb
208
+ - spec/support/app/app/models/models/asset.rb
208
209
  - spec/support/app/app/models/preload.rb
209
210
  - spec/support/app/app/models/skill.rb
210
211
  - spec/support/app/app/models/user.rb
@@ -231,7 +232,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
231
232
  - !ruby/object:Gem::Version
232
233
  version: '0'
233
234
  requirements: []
234
- rubygems_version: 3.0.3
235
+ rubygems_version: 3.1.2
235
236
  signing_key:
236
237
  specification_version: 4
237
238
  summary: Preload factories (Factory Bot) just like fixtures. It will be easier and,
@@ -240,6 +241,7 @@ test_files:
240
241
  - spec/factory_bot/preload_spec.rb
241
242
  - spec/spec_helper.rb
242
243
  - spec/support/app/app/models/category.rb
244
+ - spec/support/app/app/models/models/asset.rb
243
245
  - spec/support/app/app/models/preload.rb
244
246
  - spec/support/app/app/models/skill.rb
245
247
  - spec/support/app/app/models/user.rb