Skip to content

Commit

Permalink
Support rails 6.0.0 (#1172)
Browse files Browse the repository at this point in the history
* Change update_attributes to update

In Rails 6.0 update_attributes/update_attributes! is considered deprecated. Method update/update! is the replacement.

* CI: Don't use Bundler 1.16.1

- Bundler 1.16.1 has bug where dependencies can't be resolved properly
  when a gem is a release candidate or an alpha version.
  The underlying bundler issue can be found here rubygems/bundler#6449

* Disable eager_load in test env:

- In Rails 6.0, rails/rails@3b95478 made a change to eagerly define
  attribute methods of a Model when `eager_load` is enabled.
  This breaks our test suite because of the way we run migration.

  The TL;DR is that doing `People.attribute_names` will return an
  empty array instead of `[:id, time_zone, ...]`.
  You can find a failing build here https://travis-ci.org/paper-trail-gem/paper_trail/jobs/463369634

  Basically what happens is:

  1) The dummy app boot, attribute methods of each model are defined
     but since migration didn't run yet, the tables aren't even
     created resulting in a empty attribute set.
  2) Migration runs, but it's already too late.

  In this commit I disabled eager_loading in test, AFAIT there isn't
  much benefit in eager_loading the dummy app anyway.
  Also renaming the `user.rb` file to `postgres_user.rb` in order for
  rails autoloading to work correctly.
  • Loading branch information
Edouard-chin authored and jaredbeck committed Dec 4, 2018
1 parent 21cc89a commit 2b479a7
Show file tree
Hide file tree
Showing 44 changed files with 327 additions and 293 deletions.
20 changes: 18 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ cache: bundler
# For example, if our gemspec says `required_ruby_version = ">= 2.3.0"`, and
# ruby 2.5.0 has just been released, then we test 2.3 and 2.5, but not 2.4.
rvm:
- 2.5.0
- 2.3.6
- 2.3.8
- 2.4.5
- 2.5.3

env:
global:
Expand All @@ -25,11 +26,26 @@ env:
# the meantime.
sudo: required

before_install:
- gem update bundler

gemfile:
- gemfiles/ar_4.2.gemfile
- gemfiles/ar_5.1.gemfile
- gemfiles/ar_5.2.gemfile
- gemfiles/ar_6.0.gemfile
matrix:
exclude:
# rails 6 will not support ruby < 2.4.1
- rvm: 2.3.8
gemfile: gemfiles/ar_6.0.gemfile
# optimization: don't test intermediate rubies (see above)
- rvm: 2.4.5
gemfile: gemfiles/ar_4.2.gemfile
- rvm: 2.4.5
gemfile: gemfiles/ar_5.1.gemfile
- rvm: 2.4.5
gemfile: gemfiles/ar_5.2.gemfile
fast_finish: true
addons:
postgresql: "9.4"
6 changes: 6 additions & 0 deletions Appraisals
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,9 @@ appraise "ar-5.2" do
gem "activerecord", "~> 5.2.1"
gem "rails-controller-testing", "~> 1.0.2"
end

appraise "ar-6.0" do
# TODO: Use actual version number once 6.0 beta is released
gem "activerecord", github: "rails/rails"
gem "rails-controller-testing", "~> 1.0.3"
end
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ recommendations of [keepachangelog.com](http://keepachangelog.com/).
- [#1158](https://github.com/paper-trail-gem/paper_trail/pull/1158) — Add the
ability to pass options, such as `scope` or `extend:` to the `has_many
:versions` association macro.
- [#1172](https://github.com/paper-trail-gem/paper_trail/pull/1172) -
Support rails 6.0.0 alpha.

### Fixed

Expand Down Expand Up @@ -1009,7 +1011,7 @@ in the `PaperTrail::Version` class through a `Rails::Engine` when the gem is use
`ActionController::API` for compatibility with the [`rails-api`](https://github.com/rails-api/rails-api) gem.
- [#312](https://github.com/paper-trail-gem/paper_trail/issues/312) - Fix RSpec `with_versioning` class level helper method.
- `model_instance.without_versioning` now yields the `model_instance`, enabling syntax like this:
`model_instance.without_versioning { |obj| obj.update_attributes(:name => 'value') }`.
`model_instance.without_versioning { |obj| obj.update(:name => 'value') }`.
- Deprecated `Model.paper_trail_on` and `Model.paper_trail_off` in favor of bang versions of the methods.
Deprecation warning informs users that the non-bang versions of the methods will be removed in version `4.0`

Expand Down
50 changes: 25 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ has been destroyed.
| paper_trail | branch | ruby | activerecord |
| -------------- | ---------- | -------- | ------------- |
| unreleased | master | >= 2.3.0 | >= 4.2, < 6 |
| 10 | 10-stable | >= 2.3.0 | >= 4.2, < 6 |
| 9 | 9-stable | >= 2.3.0 | >= 4.2, < 6 |
| 10 | 10-stable | >= 2.3.0 | >= 4.2, < 5.3 |
| 9 | 9-stable | >= 2.3.0 | >= 4.2, < 5.3 |
| 8 | 8-stable | >= 2.2.0 | >= 4.2, < 5.2 |
| 7 | 7-stable | >= 2.1.0 | >= 4.0, < 5.2 |
| 6 | 6-stable | >= 1.9.3 | >= 4.0, < 5.2 |
Expand Down Expand Up @@ -167,7 +167,7 @@ widget.name # 'Doobly'
# Add has_paper_trail to Widget model.
widget.versions # []
widget.update_attributes name: 'Wotsit'
widget.update name: 'Wotsit'
widget.versions.last.reify.name # 'Doobly'
widget.versions.last.event # 'update'
```
Expand Down Expand Up @@ -333,11 +333,11 @@ a = Article.create
a.versions.size # 1
a.versions.last.event # 'create'
a.paper_trail_event = 'update title'
a.update_attributes title: 'My Title'
a.update title: 'My Title'
a.versions.size # 2
a.versions.last.event # 'update title'
a.paper_trail_event = nil
a.update_attributes title: 'Alternate'
a.update title: 'Alternate'
a.versions.size # 3
a.versions.last.event # 'update'
```
Expand Down Expand Up @@ -414,9 +414,9 @@ Changes to other attributes will create a version record.
```ruby
a = Article.create
a.versions.length # 1
a.update_attributes title: 'My Title', rating: 3
a.update title: 'My Title', rating: 3
a.versions.length # 1
a.update_attributes title: 'Greeting', content: 'Hello'
a.update title: 'Greeting', content: 'Hello'
a.versions.length # 2
a.paper_trail.previous_version.title # 'My Title'
```
Expand All @@ -436,9 +436,9 @@ Only changes to the `title` will create a version record.
```ruby
a = Article.create
a.versions.length # 1
a.update_attributes title: 'My Title'
a.update title: 'My Title'
a.versions.length # 2
a.update_attributes content: 'Hello'
a.update content: 'Hello'
a.versions.length # 2
a.paper_trail.previous_version.content # nil
```
Expand All @@ -457,14 +457,14 @@ will create a version record.
```ruby
a = Article.create
a.versions.length # 1
a.update_attributes content: 'Hello'
a.update content: 'Hello'
a.versions.length # 2
a.update_attributes title: 'Title One'
a.update title: 'Title One'
a.versions.length # 3
a.update_attributes content: 'Hai'
a.update content: 'Hai'
a.versions.length # 3
a.paper_trail.previous_version.content # "Hello"
a.update_attributes title: 'Title Two'
a.update title: 'Title Two'
a.versions.length # 4
a.paper_trail.previous_version.content # "Hai"
```
Expand Down Expand Up @@ -592,7 +592,7 @@ PaperTrail makes reverting to a previous version easy:

```ruby
widget = Widget.find 42
widget.update_attributes name: 'Blah blah'
widget.update name: 'Blah blah'
# Time passes....
widget = widget.paper_trail.previous_version # the widget as it was before the update
widget.save # reverted
Expand Down Expand Up @@ -707,7 +707,7 @@ widget.versions.last.changeset
# "updated_at"=>[nil, 2015-08-10 04:10:40 UTC],
# "id"=>[nil, 1]
# }
widget.update_attributes name: 'Robert'
widget.update name: 'Robert'
widget.versions.last.changeset
# {
# "name"=>["Bob", "Robert"],
Expand Down Expand Up @@ -770,7 +770,7 @@ version's `whodunnit` column.

```ruby
PaperTrail.request.whodunnit = 'Andy Stewart'
widget.update_attributes name: 'Wibble'
widget.update name: 'Wibble'
widget.versions.last.whodunnit # Andy Stewart
```

Expand All @@ -795,7 +795,7 @@ To set whodunnit temporarily, for the duration of a block, use

```ruby
PaperTrail.request(whodunnit: 'Dorian Marié') do
widget.update_attributes name: 'Wibble'
widget.update name: 'Wibble'
end
```

Expand Down Expand Up @@ -839,10 +839,10 @@ like it does, call `paper_trail_originator` on the object.
```ruby
widget = Widget.find 153 # assume widget has 0 versions
PaperTrail.request.whodunnit = 'Alice'
widget.update_attributes name: 'Yankee'
widget.update name: 'Yankee'
widget.paper_trail.originator # 'Alice'
PaperTrail.request.whodunnit = 'Bob'
widget.update_attributes name: 'Zulu'
widget.update name: 'Zulu'
widget.paper_trail.originator # 'Bob'
first_version, last_version = widget.versions.first, widget.versions.last
first_version.whodunnit # 'Alice'
Expand Down Expand Up @@ -1413,9 +1413,9 @@ The `have_a_version_with` matcher makes assertions about versions using
```ruby
describe '`have_a_version_with` matcher' do
it 'is possible to do assertions on version attributes' do
widget.update_attributes!(name: 'Leonard', an_integer: 1)
widget.update_attributes!(name: 'Tom')
widget.update_attributes!(name: 'Bob')
widget.update!(name: 'Leonard', an_integer: 1)
widget.update!(name: 'Tom')
widget.update!(name: 'Bob')
expect(widget).to have_a_version_with name: 'Leonard', an_integer: 1
expect(widget).to have_a_version_with an_integer: 1
expect(widget).to have_a_version_with name: 'Tom'
Expand All @@ -1430,9 +1430,9 @@ The `have_a_version_with_changes` matcher makes assertions about versions using
```ruby
describe '`have_a_version_with_changes` matcher' do
it 'is possible to do assertions on version changes' do
widget.update_attributes!(name: 'Leonard', an_integer: 1)
widget.update_attributes!(name: 'Tom')
widget.update_attributes!(name: 'Bob')
widget.update!(name: 'Leonard', an_integer: 1)
widget.update!(name: 'Tom')
widget.update!(name: 'Bob')
expect(widget).to have_a_version_with_changes name: 'Leonard', an_integer: 2
expect(widget).to have_a_version_with_changes an_integer: 2
expect(widget).to have_a_version_with_changes name: 'Bob'
Expand Down
8 changes: 8 additions & 0 deletions gemfiles/ar_6.0.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# This file was generated by Appraisal

source "https://rubygems.org"

gem "activerecord", github: "rails/rails"
gem "rails-controller-testing", "~> 1.0.3"

gemspec path: "../"
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ def deserialize(array)
private

def active_record_pre_502?
::ActiveRecord::VERSION::MAJOR < 5 ||
(::ActiveRecord::VERSION::MINOR.zero? && ::ActiveRecord::VERSION::TINY < 2)
::ActiveRecord.gem_version < Gem::Version.new("5.0.2")
end

def serialize_with_ar(array)
Expand Down
2 changes: 1 addition & 1 deletion paper_trail.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ has been destroyed.
s.required_ruby_version = ">= 2.3.0"

# Rails does not follow semver, makes breaking changes in minor versions.
s.add_dependency "activerecord", [">= 4.2", "< 5.3"]
s.add_dependency "activerecord", [">= 4.2", "< 6.0"]
s.add_dependency "request_store", "~> 1.1"

s.add_development_dependency "appraisal", "~> 2.2"
Expand Down
2 changes: 1 addition & 1 deletion spec/dummy_app/app/controllers/widgets_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def create

def update
@widget = Widget.find params[:id]
@widget.update_attributes widget_params
@widget.update widget_params
head :ok
end

Expand Down
File renamed without changes.
7 changes: 5 additions & 2 deletions spec/dummy_app/config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@
# and recreated between test runs. Don't rely on the data there!
config.cache_classes = true

# Eager loads all registered namespaces
config.eager_load = true
# In Rails 6.0, rails/rails@3b95478 made a change to eagerly define attribute
# methods of a Model when `eager_load` is enabled. If we used `eager_load`,
# this would break our test suite because of the way we run migration. For
# example, `People.attribute_names` would return an empty array.
config.eager_load = false

if config.respond_to?(:public_file_server)
config.public_file_server.enabled = true
Expand Down
16 changes: 8 additions & 8 deletions spec/models/animal_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@

it "works with custom STI inheritance column" do
animal = Animal.create(name: "Animal")
animal.update_attributes(name: "Animal from the Muppets")
animal.update_attributes(name: "Animal Muppet")
animal.update(name: "Animal from the Muppets")
animal.update(name: "Animal Muppet")
animal.destroy
dog = Dog.create(name: "Snoopy")
dog.update_attributes(name: "Scooby")
dog.update_attributes(name: "Scooby Doo")
dog.update(name: "Scooby")
dog.update(name: "Scooby Doo")
dog.destroy
cat = Cat.create(name: "Garfield")
cat.update_attributes(name: "Garfield (I hate Mondays)")
cat.update_attributes(name: "Garfield The Cat")
cat.update(name: "Garfield (I hate Mondays)")
cat.update(name: "Garfield The Cat")
cat.destroy
expect(PaperTrail::Version.count).to(eq(12))
expect(animal.versions.count).to(eq(4))
Expand All @@ -45,7 +45,7 @@

it "allows the inheritance_column (species) to be updated" do
cat = Cat.create!(name: "Leo")
cat.update_attributes(name: "Spike", species: "Dog")
cat.update(name: "Spike", species: "Dog")
dog = Animal.find(cat.id)
expect(dog).to be_instance_of(Dog)
end
Expand All @@ -55,7 +55,7 @@
let(:callback_cat) { Cat.create(name: "Markus") }

it "trails all events" do
callback_cat.update_attributes(name: "Billie")
callback_cat.update(name: "Billie")
callback_cat.destroy
expect(callback_cat.versions.collect(&:event)).to eq %w[create update destroy]
end
Expand Down
Loading

0 comments on commit 2b479a7

Please sign in to comment.