Skip to content

Ensure uniqueness validation matcher works with STI#1450

Closed
StefSchenkelaars wants to merge 1 commit intothoughtbot:masterfrom
StefSchenkelaars:issue/1245
Closed

Ensure uniqueness validation matcher works with STI#1450
StefSchenkelaars wants to merge 1 commit intothoughtbot:masterfrom
StefSchenkelaars:issue/1245

Conversation

@StefSchenkelaars
Copy link
Copy Markdown
Contributor

The uniqueness validation matcher generates a fake class when the
scope name ends on _type. This fake class is a duplicate of the
original class. In most cases this is not a problem, however when
using single table inheritance (STI), this new fake class is not
a subclass of the original class and therefore ActiveRecord will
throw an error. See #1245

A similar thing happens when the original class is using an AASM
state machine. The state machine settings are stored in a global
registry. So when the fake class is initialized, AASM cannot find
the settings of the set state machines (due to duplication) in the
registry. See #854

These problems are fixed by making the fake class inherrit from the
original class. Therefore it closes #1245 and closes #854

gem "spring-watcher-listen", "~> 2.0.0"
gem "pg", "~> 0.18"
gem "sqlite3", "~> 1.3.6"
gem "aasm"
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Not sure what the policy is about these kind of tests that require external libraries. I now only require this gem in the relevant spec but it's still needed in the gemfiles. I'm happy to change / remove it.

The uniqueness validation matcher generates a fake class when the
scope name ends on _type. This fake class is a duplicate of the
original class. In most cases this is not a problem, however when
using single table inheritance (STI), this new fake class is not
a subclass of the original class and therefore ActiveRecord will
throw an error. See thoughtbot#1245

A similar thing happens when the original class is using an AASM
state machine. The state machine settings are stored in a global
registry. So when the fake class is initialized, AASM cannot find
the settings of the set state machines (due to duplication) in the
registry. See thoughtbot#854

These problems are fixed by making the fake class inherrit from the
original class. Therefore it closes thoughtbot#1245 and closes thoughtbot#854
@matsales28
Copy link
Copy Markdown
Member

@StefSchenkelaars thanks for working on this, do you still want to work on that? If so I think we can just remove the part of this PR that introduces aasm, we shouldn't be testing specific behaviour of gems.

I can continue this work and give you credits if you're not interested anymore.

@StefSchenkelaars
Copy link
Copy Markdown
Contributor Author

Hi @matsales28
It has been some time ago I worked on this. Feel free to take over the PR. If you don't have time, let me know what things you would like to see different (next to removing the dependency). I can try to free up some time the following weeks but I'm kinda booked atm.
Good to see that it's picked up 😊
Stef

@matsales28
Copy link
Copy Markdown
Member

Closing this PR in favor of #1610

@matsales28 matsales28 closed this Feb 2, 2024
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.

Issue with validates uniqueness and STI/polymorphic models on rails 6? Issue with Shoulda::Matchers::ActiveRecord::Uniqueness::TestModels

3 participants