1
0
Fork 0
mirror of https://github.com/thoughtbot/factory_bot_rails.git synced 2022-11-09 11:49:18 -05:00
Commit graph

24 commits

Author SHA1 Message Date
Daniel Colson
ce8bfb409b Run standardrb
This commit applies the changes from running `standardrb --fix`
2020-06-10 17:13:56 -04:00
Alex Golubenko
7780065bd2
Fix for factory class naming (#366)
Changing the process of name selection for factory defining

Example:

rails g model UserMedia filename:string --force-plural
...
invoke      factory_bot
create        spec/factories/user_media.rb

it generates the correct factory naming:

FactoryBot.define do
  factory :user_media do
    filename { "MyString" }
  end
end

Fixes: #356
2020-04-03 21:50:47 -04:00
Daniel Colson
baffdb9005 Generate namespaced factories in a directory
Closes #231

When generating factories for namespaced models
(like `Admin::User`), we can follow the pattern used by the model and
model test generators of generating the file inside a directory matching
the namespace. With this commit we generate
`test/factories/admin/user.rb` instead of
`test/factories/admin_user.rb`.
2018-12-03 23:09:17 -05:00
Daniel Colson
b48cff7d99 Allow using custom template for the generator
PR #135 introduced the ability to generate factories in one file, but it
also removed the ability to use custom templates for generating
factories. This PR adds back the ability to use custom templates when
generating factories in separate files. I doubt this was a heavily used
feature, but it has come up a few times and it I think supporting it
simplifies the generator.

Closes #147
Closes #148
Closes #193

PR #193 basically reverted #135, which is undesirable. thoughtbot
projects generally prefer putting all the factories in one file.

Co-authored-by: Nick Sanford <me@nicksanford.io>
2018-12-03 21:12:47 -05:00
Daniel Colson
79ab64d8ad
Add newline between factories in same file
Fixes #305

When generating factories in the same file (for example
spec/factories.rb), before this PR we didn't put newlines between the
factories. So if you generated a user factory, then a robot factory, you
would end up with something like this:

```rb
FactoryBot.define do
  factory :robot do
    name { "MyString" }
  end
  factory :user do
    name { "MyString" }
  end
end
```

With this PR, generating those factories will now look like:

```rb
FactoryBot.define do
  factory :robot do
    name { "MyString" }
  end

  factory :user do
    name { "MyString" }
  end

end
```

Note that the final newline is not ideal, but it will only be added when
generating the first factory, whereas the missing newline after
factories was happening every time we generated a new factory. So
with this PR we will only need to fix a whitespace style error once,
rather than N-1 times. We can could open another issue to fix the extra
newline at the end, but I think it may be more complicated than it
sounds.

Co-authored-by: Nick Sanford <me@nicksanford.io>
2018-11-30 22:07:38 -05:00
Daniel Colson
c84e6b2aa2 Fix remaining RuboCop TODOs 2018-10-03 21:40:07 -04:00
Alex
7907b085fd Address todos generated by rubocop for files in the lib/generators directory (#299)
* Address todos generated by rubocop for files in the lib/generators directory.

This partially addresses #293. Since rubocop generated quite a few todos, the commits addressing them are split up into a few different PRs that cover different files.

* Fix underscore seperator in model_generator.rb, and change memoized instance variable name to @_source_root in lib/generators/factory_bot.rb

* get rid of memoized _source_root variable

* get rid of extraneous rubocop rules
2018-09-28 22:03:04 -04:00
Daniel Colson
affd0771bf Remove unneeded conditional for app_generators
Configuration#generators was deprecated in favor of
Configuration#app_generators back in Rails 3.1
(04cbabb0a0).
Since we started work on factory_bot_rails 5, which only supports Rails
4.2+, we no longer need the conditional.

I also updated the development Gemfile, since it was out of date,
and replaced a nested if/else with elsif.
2018-09-14 19:52:24 +00:00
Daniel Colson
02a0f58487 Allow reloading of factory definitions
Closes #236

This commit uses ActiveSupport's FileUpdateChecker to allow reloading
FactoryBot of definitions whenever a file in
`FactoryBot.definition_file_paths` gets updated.
This is similar to reloading for
[I18n](ced104d579/activesupport/lib/active_support/i18n_railtie.rb (L60-L70))
and [react rails](83b6175460/lib/react/rails/railtie.rb (L35-L41))

This allows us to get rid of any Spring-specific logic in the railtie,
since [Spring hooks into the application
reloader](0c711ff10b/lib/spring/application.rb (L161)).

This partly solves #211, since we no longer call `FactoryBot.reload` at
all in `after_initialize`. Instead, we will only call it when one of the
files in `definition_file_paths` gets updated. I say it partly
solves #211 because once a definition file gets updated, reloading
would still give warnings about redefining any constants in the
definition files. I wonder how common it is to define constants in the
same file as factory definitions. It's probably better to keep constants
in the autoload path, allowing Rails to handle unloading and reloading
them in development. I would want to see some specific examples before
worrying too much about it.

I would also like to offer a better way to configure the definition file
paths (see #165 and #166) and possibly an option to opt out of loading
definitions at all (could help with issues like #192).

A couple of quirks here:
* the to_prepare block could potentially get
[called multiple times](https://github.com/rails/rails/issues/28108).
It shouldn't matter, since `execute_if_updated` is a no-op if no
updates have been made.

* I noticed that the first time I call `reload!` in the console the
factory definitions get reloaded even when I made no updates to the
definition files. I think it is related to
[this](f7c5a8ce26/activesupport/lib/active_support/evented_file_update_checker.rb (L18)). After the first call
`reload!` works as expected, only reloading the factory definitions
if the definition files were updated.

* Rails uses execute rather than execute_if_updated for the
[route
reloader](https://github.com/rails/rails/blob/master/railties/lib/rails/application/finisher.rb#L133)
and for the [watchable file
reloader](https://github.com/rails/rails/blob/master/railties/lib/rails/application/finisher.rb#L173).
This means that changes to factory definitions will cause reloading of
the whole application. I think this is fine.
2018-09-08 02:21:47 +00:00
Atul Bhosale
01a23ffbe1 Update ModelGenerator to generate dynamic attributes 2018-09-03 15:37:26 +00:00
Avielle Wolfe
c5d11518d7 Rename all girl -> bot
* Rename files and code
* Change factory_girl dependency to factory_bot

This change will bring _rails name in line with factory_bot
c716ce01b4
2017-10-20 18:21:52 -04:00
Kevin Reintjes
5b17b5777b Add filename_proc option to Factory generator 2015-03-16 09:02:12 -04:00
Joshua Clayton
3ac64d789f Factories are added after the define block
Fixes #150
2015-03-14 21:59:59 -04:00
Rodolfo Spalenza
c761d47c93 Fix when use fixture_replacement with suffix option.
Fix #113
2015-03-14 20:53:35 -04:00
Peter Marsh
3a050736f3 Change FactoryGirl::Generators::Base#explicit_class_option to output 1.9 Hashes 2015-03-14 20:30:51 -04:00
Takeo Fujita
3488c767c7 fix indent of generated factory file 2015-03-13 21:32:48 -04:00
Jessie A. Young
da421ce31d Do not generate new file if factories.rb exists
* Instead, insert factory into `factories.rb`
* Reference: https://github.com/thoughtbot/suspenders/pull/339
* Add info on change to README
2014-08-07 13:15:52 -07:00
Joshua Clayton
079657625d Require factory_girl_rails explicitly in generator
This resolves an issue where factory_girl_rails won't get required if
you only have factory_girl_rails declared in the :test group of a Rails
app.

Closes #89
2013-02-08 12:03:14 -05:00
Eric Hu
e85aa34ace Add suffix option for Rails config
Closes #78
2012-12-07 10:40:51 -05:00
Joshua Clayton
a6ccbcb9f6 Support yardoc
This commit renames the .rb template to .erb. With it renamed, yardoc
won't try and parse the file (which was causing the install to break).

Closes #52
2012-03-30 10:27:41 -04:00
Joshua Flanagan
7df9c9b13a Respect model namespaces when generating factories.
The factory name for the model Namespaced::User wil be :namespaced_user.
The :class option is specified as a string to prevent class reloading issues.

Addresses GH-44
2012-02-26 12:58:48 -06:00
Carlos Antonio da Silva
42f5a10554 Update github url in generator template to use https 2012-01-26 22:34:20 -02:00
Jeff Dutil
d364e832d6 Correct whitespace generated by factory template.
Closes #36
2011-10-14 10:36:05 -04:00
Mike Gehard
9f26cabfdf Move generators from rails3-generators gem over to factory_girl_rails. 2011-07-11 21:11:06 -06:00