Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix persist before save in rails 5.2 #36

Merged
merged 3 commits into from
Nov 12, 2018

Conversation

h-lame
Copy link

@h-lame h-lame commented Nov 12, 2018

  1. Add rails 5, 5.1, and 5.2 to the matrix of ruby + rails versions we run the CI tests against.
  2. Make vault_persist_before_save! work properly in rails 5.2

Check the commits individually for more info.

spec/unit/encrypted_model_spec.rb Outdated Show resolved Hide resolved
spec/unit/encrypted_model_spec.rb Outdated Show resolved Hide resolved
@@ -6,6 +6,9 @@ sudo: false
gemfile:
- Gemfile
- gemfiles/rails_4.2.gemfile
- gemfiles/rails_5.0.gemfile

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it high complexity to just switch to a circleci config file instead?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably. Seems that travis has some baked in matrix version combination stuff that we'd need to manage manually in circleci because it doesn't support it out of the box. I imagine this is why wwtd exists.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we use TravisCI's config to run test with multiple versions of Ruby and ActiveRecord.

We add the new rails gemfiles using the `appraisals` gem.  This means that
we can run `bundle exec appraisal rake` locally to run the specs against
rails 4.2, 5, 5.1, and 5.2.  We've also added these gemfiles to the
`.travis.yml` so these rails versions are added to the ruby version test
matrix that we run on CI.  Note that although we use CircleCI to run our
CI tests, we use the `wwtd` gem to read the `.travis.yml` config and do
what it would do.  This is confusing, but it works.
In rails 5.2 the behaviour of dirty tracking methods changed when they are
run during callbacks.  Methods like `<attribute>_changed?` and `changes`
do the same thing in callbacks run before save, but will behave differently
in callbacks run after save.  We'd already written
`__vault_encrypt_attribute` to understand this and use the
`saved_change_to_attribute?` method instead of `<attribute>_changed?`
method for rails 5.2.  Unfortunately, this is only the correct approach
when we're running `__vault_encrypt_attribute` in an `after_save` callback,
which we're not if we have run `vault_persist_before_save!`.  This means
`__vault_encrypte_attribute` is run in a `before_save` callback where the
`saved_change_to_attribute?` methods don't do what we want (because we
haven't saved any changes yet).  In this case we do want to use the old API
that the raisl < 5.2 branch uses.
We use `Person` in the "when not used" context because that model class
has not had `vault_persist_before_save!` called on it.  `EagerPerson` has
and that's why we use it in the "when used" context.  It'd be clearer if
we could explicitly call it in the spec, but there's no clean up option to
unset it so we need separate models.
@h-lame h-lame force-pushed the fix-persist-before-save-in-rails-5.2 branch from 77dcc1a to f99aebd Compare November 12, 2018 15:17
Copy link

@finalwharf finalwharf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@h-lame h-lame merged commit 258e000 into master Nov 12, 2018
@h-lame h-lame deleted the fix-persist-before-save-in-rails-5.2 branch November 12, 2018 15:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants