1
0
Fork 0
mirror of https://github.com/thoughtbot/factory_bot.git synced 2022-11-09 11:43:51 -05:00
Commit graph

20 commits

Author SHA1 Message Date
Daniel Colson
793a651aa3 Address Ruby 2.7 kwargs edge case
Closes #1423

5c071d42 fixed most kwarg deprecation warnings coming from within
factory_bot, but using the Ruby 3-style argument forwarding does not
handle every case.

To handle the case of `initialize_with` being used with a method that
takes a final hash argument, this commit adds an additional branch to
this code to use `ruby2_keywords` for Ruby >= 2.7 and < 3.0. On 3.0 we
use the 3.0-style forwarding.

I added a tests that was outputting a deprecation warning on Ruby 2.7
before this change, but that no longer outputs the warning after this
change.

Yes, this is a bit of a mess, but I don't see a better way. I got
inspiration here from this [fantastic blog post on argument delegating
with Ruby 2.7 and 3][delegating].

[delegating]: https://eregon.me/blog/2019/11/10/the-delegation-challenge-of-ruby27.html#the-delegation-challenge.
2020-07-16 09:10:06 -04:00
Adam Hess
5c071d42fd Fix ruby 2.7 kwargs warning
Ruby 2.7 deprecated passing kwargs when the method expects a hash or passing a hash when the method expects kwargs. In factory_bot, this creates the warning:

```
/Users/hparker/code/factory_bot/lib/factory_bot/decorator/new_constructor.rb:9: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call
/Users/hparker/code/factory_bot/spec/acceptance/initialize_with_spec.rb:220: warning: The called method `initialize' is defined here
```

We can fix this warning by updating the syntax. We need to include `**kwargs` in the `method_missing` calls when we are on ruby 2.7 or later.
In decorator.rb, adding `**kwargs` alone doesn't work since adding `**kwargs` can change what arguments remain in the `args`.

In this case we have to class eval the method if we are running ruby 2.7. This way the syntax is valid in previous versions and we can use the `...` operator which allows us to avoid changing the arguments passed on in method missing.

Co-authored-by: Lee Quarella <leequarella@gmail.com>
2020-06-24 18:27:38 -04:00
Daniel Colson
5f1a1de114 Run standardrb
This commit applies the changes from running `standardrb --fix`
2020-06-10 17:11:39 -04:00
Daniel Colson
0c17434b4a Avoid recursive call to AttributeHash#attributes
Fixes 1155

Defining an attribute called 'attributes', then referring to that
attribute in `initialize_with` was causing `AttributeHash#attributes`
to call itself recursively.
2018-10-26 10:51:05 -04:00
Hunter Braun
95bd3c6076 [Rubocop] Performance Cop Offenses (#1209) 2018-10-04 21:07:56 -04:00
Daniel Colson
bf04aaa068 Autocorrect all static attributes to dynamic
Most of this was fixed by adding the `attribute-defined-statically-cop`
branch of `thoughtbot/rubocop-rspec` to the Gemfile and running:

```sh
  rubocop \
    --require rubocop-rspec \
    --only FactoryBot/AttributeDefinedStatically \
    --auto-correct
```

I had to update the cucumber tests manually, and I realized our changes
don't handle `ignore` blocks or blocks with arity 1 that use the yielded
DefinitionProxy. I will update
https://github.com/rubocop-hq/rubocop-rspec/pull/666to handle these
cases.
2018-09-14 19:27:13 +00:00
Oli Peate
01d81f54b5 Remove unnecessary spec_helper requires
https://github.com/rspec/rspec/wiki#rspec
2018-05-21 18:03:28 +01:00
Avielle
c716ce01b4 Replace 'girl' with 'bot' everywhere (#1051)
Also: add a deprecation warning to factory_girl, asking users to switch to
factory_bot

https://github.com/thoughtbot/factory_girl/issues/921
2017-10-20 15:20:28 -04:00
Joshua Clayton
ee6caa2a6b Fix FactoryGirl::Decorator#send to pass blocks
Why?

When the decorated @component is also a decorated object, and
FactoryGirl::Decorator#method_missing calls @component#send and a block
is present, the block needs to be sent appropriately.

Closes #816
2016-02-06 00:31:26 -05:00
Ian Zabel
9610b38957 Deprecate #ignore in favor of #transient
Update documentation to reflect that transient is now preferred
2014-05-23 14:43:44 -04:00
Joshua Clayton
ed598ec54c Update repo to follow thoughtbot guide for whitespace in blocks 2013-12-14 22:33:15 -05:00
Joshua Clayton
b095f24598 Convert to expect syntax 2013-01-18 13:58:36 -05:00
Joshua Clayton
e9d9e3099b Prep for FactoryGirl 4.0 2012-08-02 11:17:39 -04:00
Joshua Clayton
2b50190a07 Make hash of public attributes available within the initialize_with block
Closes #344
2012-06-08 17:05:36 -04:00
Joshua Clayton
775dc8bd88 Fix namespace issues in 1.9.2 2012-06-08 12:17:21 -04:00
Joshua Clayton
cd984633bc Get spec passing on 1.9.2 2012-05-18 18:06:24 -04:00
Joshua Clayton
a5b3a97c9d Optionally disable duplicate assignment of attributes in initialize_with
By setting:

    FactoryGirl.duplicate_attribute_assignment_from_initialize_with =
false

This turns off duplicate assignment of attributes accessed from
initialize_with. This means any attributes accessed from the factory and
assigned in the initialize_with block won't be subsequently set after
the object has been instantiated.

This will be the default functionality in 4.0.

Closes #345
2012-05-18 17:38:23 -04:00
Joshua Clayton
9907826ee5 Base class is implied when calling .new within initialize_with
Closes #343
2012-04-23 22:52:59 -05:00
Joshua Clayton
6c29b11477 Use 1.9 hash syntax 2012-03-16 16:43:23 -04:00
Joshua Clayton
578036480f Implement initialize_with to allow overriding object instantiation
Factory Girl now allows factories to override object instantiation. This
means factories can use factory methods (e.g. methods other than new) as
well as pass arguments explicitly.

    factory :user do
      ignore do
        things { ["thing 1", "thing 2"] }
      end

      initialize_with { User.construct_with_things(things) }
    end

    factory :report_generator do
      ignore do
        name { "Generic Report" }
        data { {:foo => "bar", :baz => "buzz"} }
      end

      initialize_with { ReportGenerator.new(name, data) }
    end

Whitespace

Code recommendations
2012-01-20 15:37:36 -05:00