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 joins(:versions) #1137

Closed
wants to merge 2 commits into from

Conversation

seanlinsley
Copy link
Member

@seanlinsley seanlinsley commented Aug 15, 2018

Fixes #1135 (followup to #1108).

Where should I add a test for my use of STI without a type column? I guess it'll require a new set of model definitions, so it might deserve its own test file, too.

@seanlinsley seanlinsley changed the title Fix joins Fix joins(:versions) Aug 15, 2018
@@ -112,7 +112,7 @@

# After creating a bunch of records above, we change the inheritance_column
# so that we can demonstrate passing hints to the migration generator.
context "simulate a historical change to inheritance_column" do
xcontext "simulate a historical change to inheritance_column" do
Copy link
Member Author

Choose a reason for hiding this comment

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

I don't totally understand these tests... not sure how to fix them. Any ideas? :octocat:

@jaredbeck
Copy link
Member

I have looked through this, but I didn't understand any of it, probably because I don't understand what problem you're trying to solve. If you could provide a reproduction of the issue using the bug report template that would help a lot.

@lorint can you help, please?

@seanlinsley
Copy link
Member Author

@jaredbeck yeah I understand it's a complex issue. I added failing tests as a separate commit so you can see the failures on CI.

@seanlinsley
Copy link
Member Author

Interesting that with the fix, the tests only pass on Rails 5.2. I wonder how the API changed 🤔

@lorint
Copy link
Contributor

lorint commented Aug 16, 2018

I think I've almost figured out in the has_many :versions lambda how to keep the various tests using .length and .size the way they were, so less overall impact and perhaps less of a breaking change for folks.

-= more news forthcoming =-

@seanlinsley
Copy link
Member Author

That's good to hear. In case it wasn't clear, I had to update those tests b/c the events in record_trail.rb were calling versions.build, and that apparently overrode the item_type that was being passed in. As a consequence, the in-memory association wasn't aware of the new record so count was needed to trigger a new database request.

unless item_type.nil?
relation = relation.unscope(where: :item_type).where(item_type: item_type)
end
item_type = klass.paper_trail.send(:versions_association_item_type)
Copy link
Member Author

Choose a reason for hiding this comment

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

it's now a public method, so send isn't required here

Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like the .unscope / .where can simply happen if klass.descendants.empty?

@lorint
Copy link
Contributor

lorint commented Aug 17, 2018

Here's the changes I would recommend:

https://github.com/seanlinsley/paper_trail/pull/1/files

Copy link
Contributor

@lorint lorint left a comment

Choose a reason for hiding this comment

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

Been fun to play with this change in the has_many lambda in order to get things going for .joins(). Thanks for bringing it up.

super
paper_trail.send(:setup_versions_association, klass)
end
})
Copy link
Contributor

Choose a reason for hiding this comment

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

Love this idea! Worked out well for the fixes for CelebrityFamily tests and migrations to work.

else
@model_class.name
end
end
Copy link
Contributor

@lorint lorint Aug 17, 2018

Choose a reason for hiding this comment

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

After playing around for a bit it ended up seeming that we could have just if klass.descendants.empty? in the has_many lambda to get total consistency with existing tests.

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't believe that'll work for the company / management scenario I described here: #1108 (comment)

Copy link
Contributor

@lorint lorint Aug 19, 2018

Choose a reason for hiding this comment

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

Oh -- I thought you had already written the tests you were after. Is this perhaps the goal? If you add in this PR on your branch then it passes BTW:

require "spec_helper"

class Management < Customer
end

RSpec.describe Management, type: :model, versioning: true do
  it { is_expected.to be_versioned }

  it "utilises the base_class for STI classes having no type column" do
    # Customer (and thus Management) have no stored `type` column.
    expect(Management.inheritance_column).to eq("type")
    expect(Management.columns.map(&:name)).not_to include("type")

    customer1 = Customer.create(name: "Cust 1")
    customer2 = Management.create(name: "Cust 2")

    # When creating a Management the version refers to Customer:
    cust_pts = PaperTrail::Version.where(item_type: "Customer")
    expect(cust_pts.count).to eq(2)

    # Nothing gets tracked as being Managment:
    mgmt_pts = PaperTrail::Version.where(item_type: "Management")
    expect(mgmt_pts.count).to eq(0)
  end
end

Perhaps I'll throw it in the mix for good measure because to me it seems to addresses what sounded like your suggestion. But if this isn't the goal then please write a failing test that addresses Company / Management.

Copy link
Member Author

Choose a reason for hiding this comment

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

It looks like that test covers the scenario I described; thanks for writing it. Are you sure that it passes when checking descendants.empty?? It'd seem like it'd only pass when we rely on descends_from_active_record?.

Copy link
Contributor

@lorint lorint Aug 19, 2018

Choose a reason for hiding this comment

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

Wonderfully enough it works fine with descendants.empty?. Now I've investigated the other override you mentioned, for creation_attributes. It ends up a bit more involved since the patch is a more generic class method that must check to see if it's dealing with a PaperTrail::Version. (The other override was only creating methods directly on Reflection objects.) Pick whichever one you prefer as both approaches tick all of the boxes for the tests.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, interesting. I didn't realize that a monkeypatch on creation_attributes is effectively global, while the collection? monkeypatch is tied specifically to the versions assocation.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'll go back to the .collection?() override then, which is specific to a Reflection object.

@@ -84,8 +84,7 @@ def record_create
# `data_for_create` but PT-AT still does.
data = event.data.merge(data_for_create)

versions_assoc = @record.send(@record.class.versions_association_name)
version = versions_assoc.new(data)
version = @record.class.paper_trail.version_class.new(data)
Copy link
Contributor

Choose a reason for hiding this comment

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

Weirdly I couldn't get these guys to fully work unless I did:
version = @record.send(@record.class.versions_association_name).new(data)

@lorint
Copy link
Contributor

lorint commented Aug 18, 2018

... events in record_trail.rb ... overrode the item_type that was being passed in. As a consequence, the in-memory association wasn't aware of the new record so count was needed to trigger a new database request.

You describe the bane of my existence. Although this last update in PR #1 might offer an unobtrusive solution to all of this.

@jaredbeck
Copy link
Member

  • Should this block the release of PT 10?
  • Do you need my help reviewing this? If so, is it ready for review?

Thanks.

@lorint
Copy link
Contributor

lorint commented Aug 20, 2018

Should this block the release of PT 10?

I would love to see this be a part of PT 10 because it brings together a couple great fixes, and gets us well-prepared to bring the association_tracking gem further.

The fully working version with all the bells and whistles is here, and I feel that it's ready for review.

With that one all existing tests get to stay exactly the same -- except for one simple line added in the dummy app's callback_modifier.rb. It also incorporates the Company / Management STI fix Sean had requested.

@jaredbeck
Copy link
Member

I would love to see this be a part of PT 10 ..

Fine by me.

The fully working version with all the bells and whistles is here, and I feel that it's ready for review.

So what's the plan? Sean merges that (seanlinsley#1) and then I review it here? Sorry for jumping into the middle of the conversation here.

With that one all existing tests get to stay exactly the same ..

That's the dream 👍

@lorint
Copy link
Contributor

lorint commented Aug 21, 2018

the plan -- Sean merges that and then I review it here?

Yeah, that's what I was hoping for. I had originally set up as that PR to his branch instead of a review here with the hope to make it easy to incorporate all the little changes that bring everything together. And if you like what you see out there then it's cool also if you'd like to just make a single commit here that brings all of that in. Whatever is easy.

@seanlinsley
Copy link
Member Author

I was planning to merge seanlinsley#1 into this branch, likely as a squash commit.

@jaredbeck we could use more eyes on the monkeypatch part of the PR: seanlinsley#1 (comment)

jaredbeck added a commit that referenced this pull request Aug 22, 2018
This partially reverts commit 58369e1.
I have kept the specs, skipped.

Per the following, this approach does not seem to be working:

- #1135
- #1137
- seanlinsley#1
@jaredbeck
Copy link
Member

I believe this can be closed per #1143, correct me if I'm wrong

@jaredbeck jaredbeck closed this Aug 27, 2018
aried3r pushed a commit to aried3r/paper_trail that referenced this pull request Dec 14, 2020
This partially reverts commit 58369e1.
I have kept the specs, skipped.

Per the following, this approach does not seem to be working:

- paper-trail-gem#1135
- paper-trail-gem#1137
- seanlinsley#1
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