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

Error with Single Table Inheritance #738

Closed
sreuterle opened this issue Mar 22, 2016 · 9 comments
Closed

Error with Single Table Inheritance #738

sreuterle opened this issue Mar 22, 2016 · 9 comments
Milestone

Comments

@sreuterle
Copy link

I'm using paper_trail 4.0.2 with ruby 2.3.0, rails 4.2.6. I want to update to latest version of paper_trail (4.1.0).
With new version running I can't refer to self.version from within a module.

I encapsulated history related stuff into a ActiveSupport::Concern for my model 'User':

class User < Person
  include UserHistory
  ...
end

module UserHistory
  extend ActiveSupport::Concern

  included do
    has_paper_trail :skip => [:updated_at, :created_at, :remember_created_at, :sign_in_count, :current_sign_in_at, :last_sign_in_at, :current_sign_in_ip, :last_sign_in_ip, :failed_attempts]
  end
  ...

  def was_attribute_changed?(attribute_name)
    (!is_just_created? && !last_changeset[attribute_name.to_sym].blank?)
  end

  def last_changeset
    @last_changeset ||= self.versions.last.changeset
  end
  ...
end

Calling was_attribute_changed? on a User instance (no problem) refers to the method last_changeset within the same module (no problem too). This calls the PaperTrail method versions but gets an error:

undefined method 'versions' for UserHistory:Module

This error only occurs with version 4.1.0 of paper_trail but not with 4.0.2. It occurs with both rails 4.2.6 and 4.2.5
Is there any change regarding that? Is it a feature or is it a bug?

I don't know how to use your template for reporting bugs, sorry.

@jaredbeck
Copy link
Member

I can't reproduce this issue. Here's what I tried: https://gist.github.com/jaredbeck/122239d2e62717f44808 What are we doing differently?

@sreuterle
Copy link
Author

Hi and sorry for being so late with my answer, I was on holidays a few days.

I could now isolate the the issue a way better. I edited your code sample so that the error is more visible. It seems to be an issue with Single Table Inheritance - nothing to do with calling self.* as I thought first:
https://gist.github.com/sreuterle/7de97c87741fe1da422a34c9bae1701d

You can switch paper_trail version between 4.0.2 and 4.1.0

I located that in PaperTrail::VersionConcern.load_changeset an error is thrown (copied from my real application):

NoMethodError: undefined method `unserialize_attribute_changes_for_paper_trail!' for #Class:0x007fa5e0145838

/Users/sreuter/.rvm/gems/ruby-2.3.0@global/gems/activerecord-4.2.6/lib/active_record/dynamic_matchers.rb:26:in `method_missing'

/Users/sreuter/.rvm/gems/ruby-2.3.0@global/gems/paper_trail-4.1.0/lib/paper_trail/version_concern.rb:263:in `load_changeset'

/Users/sreuter/.rvm/gems/ruby-2.3.0@global/gems/paper_trail-4.1.0/lib/paper_trail/version_concern.rb:215:in `changeset'

This error usually is suppressed and rescued by returning an empty Hash.

I hope you can reproduce it now?

@sreuterle sreuterle changed the title Referencing self.versions from within module Error with Single Table Inheritance Apr 11, 2016
@jaredbeck
Copy link
Member

I located that in PaperTrail::VersionConcern.load_changeset an error is [raised] .. This error .. is suppressed and rescued by returning an empty Hash. I hope you can reproduce it now?

Yes, I see the same error in load_changeset, undefined method unserialize_attribute_changes_for_paper_trail! for Person.

I'm not sure this is a bug though. Maybe you're supposed to include UserHistory in the parent class (Person) instead of in User? That's how it's done in our test suite (see Elephant < Animal).

@jaredbeck
Copy link
Member

Assuming this is not a bug (see above) I'd still like to find a way to provide a better error message.

@sreuterle
Copy link
Author

Thanks for your reply.
include UserHistory for the parent class Person is not an option because there are some more classes extending Person. We only want versioning for the class User.
The older version of paper_trail worked for this. Are the changes not able to work with such structure as before too?

@jaredbeck
Copy link
Member

You can switch paper_trail version between 4.0.2 and 4.1.0 .. The older version of paper_trail worked ..

Oops, I didn't catch that part, sorry. So this is a regression between 4.0 and 4.1. Noted, thanks!

@jaredbeck
Copy link
Member

jaredbeck commented Apr 15, 2016

To summarize, in https://gist.github.com/sreuterle/7de97c87741fe1da422a34c9bae1701d

  • Reproduced with PT 4.1.0
  • Cannot reproduce with PT 4.0.2

@jaredbeck jaredbeck added this to the 5.0.0 milestone Apr 20, 2016
jaredbeck added a commit that referenced this issue May 1, 2016
Fixes NoMethodError when an STI parent class is unversioned and
thus does not respond to `unserialize_attribute_changes_for_paper_trail!`

[Fixes #738]
@jaredbeck jaredbeck mentioned this issue May 1, 2016
@jaredbeck
Copy link
Member

Fixed by 23ffbdc

Sven, please try master, or wait for 5.0, which should be coming soon.

jaredbeck added a commit that referenced this issue May 1, 2016
@sreuterle
Copy link
Author

Yeah, I upgraded to 5.x and it works. Thank you very much!

zheileman added a commit to ministryofjustice/Claim-for-Crown-Court-Defence that referenced this issue Aug 8, 2016
…rsions.

Versions > 4.0.2 contains a bug that affects our setup with STI:
paper-trail-gem/paper_trail#738

The bug was fixed in version 5.0.0 but this version introduces
other breaking changes (whodunnit) that would require code changes
and considerable testing before releasing.
lostie pushed a commit to ministryofjustice/Claim-for-Crown-Court-Defence that referenced this issue May 30, 2018
The issue paper-trail-gem/paper_trail#738 that
was locking paper_trail to the version we were currently using was
solved and its available since 6.X.

For all purposes, this also takes the opportunity to upgrade the
version to the lost recent one.

I've been through all the deprecation notes in the gem and it doesn't
seems there's anything on our code impacted by it.

NOTE: This also solves the deprecation notice that was appearing when
running RSpec:

```
DEPRECATION WARNING: before_filter is deprecated and will be removed in
Rails 5.1. Use before_action instead. (called from <top (required)> at
config/application.rb:8)
```
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

No branches or pull requests

2 participants