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

10 commits

Author SHA1 Message Date
Daniel Colson
5a3247dd8c Avoid stubbing id for records without primary key
Fixes #1305

Before Rails 6 when the build_stubbed strategy assigned an id on a
record without a primary key column, the `id=` method would no-op, and
the record would end up looking like a new_record (i.e. `new_record?`
would return true because the id was nil).

This problem surfaced because of a [change in Rails 6][rails], which
caused `id=` to raise a potentially confusing
`ActiveModel::MissingAttributeError: can't write unknown attribute ''`
for records without a primary key column.

Since build_stubbed stubbed was calling `id=` for all instances,
regardless of whether they had primary keys, it was raising the above
error.

To avoid this error, we check whether the instance has a primary_key
defined before setting the id.

We also changed `persisted?` and `new_record?` to be less dependent on
the id. That way those methods will work as expected for records without
primary keys.

[rails]: b6828fc915

Co-authored-by: Jesse Bailey <jbailey117@gmail.com>
2019-07-19 15:18:11 -04:00
Daniel Colson
cb9eb551c8
Arrest lines of an unsavory length
We had configured RuboCop to allow lines with up to 142 characters. This
PR fixes a few of the worst offenders so we can bring that number down
to 110.
2018-12-12 11:05:13 -05:00
Daniel Colson
f56394c187 Set timestamps before clearing changes
e7f66fb2 fixed one problem and introduced a new one. Setting timestamps
after clearing_changes_information doesn't work, since using the
created_at and updated_at setters will change the object again. I added
some additional specs for this case (our only spec related to clearing
changes used an object without timestamps).

For the timezone problem, I did a little digging and realized the
casting behavior was actually correct. The reason it was behaving in a
different way than I was expecting is because we had
`time_zone_aware_attributes` set to false, whereas the AR railtie
[sets it to true](54652d886a/activerecord/lib/active_record/railtie.rb (L71)). Setting it to true causes date and datetime attributes
[to get decorated with a TimeZoneConverter](54652d886a/activerecord/lib/active_record/attribute_methods/time_zone_conversion.rb (L68..L87))
2018-11-12 17:35:05 -05:00
Daniel Colson
e7f66fb2ba Set timestamps after clearing changes
Rather than redefining #created_at and #updated_at, we can use the
existing methods and just set them if there aren't already values
present. We need to do this after clearing changes information,
otherwise the times end up getting converted to the wrong zone.
2018-10-26 15:39:31 -04:00
Alex Tsui
dc327388fa Allow timestamp assignment in stubbed models 2018-10-26 15:39:31 -04:00
Hunter Braun
89ee4a4524 [Rubocop] Style Cop Offenses (#1208) 2018-10-07 18:02:54 -04:00
Daniel Colson
b4e1303904 Remove backport fro ActiveModel:Dirty
factory_bot 5 only supports Rails 4.2 and higher,
and #clear_changes_information has been around since then
(66d0a01535).

Fixes #941, since we will are no longer managing any instance variables.
2018-09-14 18:58:17 +00:00
Mandaryn
f1f7719f6e Do not add missing timestamps when using build stubbed strategy 2018-08-05 17:46:38 -04:00
Michael Koper
3db8a120ba Do not raise error for valid build_stubbed methods
decrement, increment, and toggle do not persist the toggled values, so
there is not reason to disable those methods.

Co-authored-by: Alex Tsui <alextsui05@gmail.com>
2018-08-05 17:14:46 -04: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