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

Feature/536 investigate association rule issue #650

Merged
merged 3 commits into from Dec 9, 2020
Merged

Feature/536 investigate association rule issue #650

merged 3 commits into from Dec 9, 2020

Conversation

ghost
Copy link

@ghost ghost commented Aug 31, 2020

Fix the issue outlined in #536 and validate behaviour in test

Copy link
Member

@coorasse coorasse left a comment

Choose a reason for hiding this comment

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

does your PR allows us to use models directly instead of ids? could you please add some more complex tests and maybe verify the consistency of this behaviour also with accessible_by method?

@ghost
Copy link
Author

ghost commented Oct 10, 2020

Sure ^^ I will see to it today or tomorrow :D
And as far as I remember from working on it, it creates it directly rather than with the id. But I will check it and if needed update the pr

Fix an issue that caused associations not to work in their usage as a subject for a rule
@ghost
Copy link
Author

ghost commented Nov 1, 2020

I added some tests to verify that this also works with accessible_by.
Also added an example with multiple users and models that validate the access is made based on the role of a user

@@ -78,7 +78,8 @@ def condition_match?(attribute, value)

def hash_condition_match?(attribute, value)
if attribute.is_a?(Array) || (defined?(ActiveRecord) && attribute.is_a?(ActiveRecord::Relation))
attribute.any? { |element| matches_conditions_hash?(element, value) }
match_results = attribute.map { |element| matches_conditions_hash?(element, value) }
match_results.any?
Copy link
Contributor

Choose a reason for hiding this comment

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

rather than the multi-line solution, how do you feel about attribute.to_a.any? { |element| matches_conditions_hash?(element, value) }

if attribute is already an array, to_a will do nothing. if it's not, it'll force the relation to be loaded.

i just think that code makes a bit more sense than what you have here - if i saw this, i'd want to refactor it back to the old version.

Copy link
Author

Choose a reason for hiding this comment

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

That absolutely makes a lot of sense ^^ Your change looks a lot cleaner and thus I'm happy to revert to just adding to_a to force the relation loading

@ghost ghost removed their assignment Nov 5, 2020
@ghost ghost requested a review from coorasse November 5, 2020 20:31
@coorasse coorasse self-assigned this Dec 9, 2020
@coorasse coorasse merged commit 155dd5b into CanCanCommunity:develop Dec 9, 2020
@@ -78,7 +78,7 @@ def condition_match?(attribute, value)

def hash_condition_match?(attribute, value)
if attribute.is_a?(Array) || (defined?(ActiveRecord) && attribute.is_a?(ActiveRecord::Relation))
attribute.any? { |element| matches_conditions_hash?(element, value) }
attribute.to_a.any? { |element| matches_conditions_hash?(element, value) }
Copy link
Contributor

Choose a reason for hiding this comment

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

@Liberatys if I remove the .to_a here, all tests still pass locally for me.

Do you know if I am missing something?

I've tried testing against AR 5.2 and 6.0 on sqlite and postgres.

Copy link
Author

@ghost ghost Dec 14, 2020

Choose a reason for hiding this comment

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

@ghiculescu the tests are mostly written to ensure / prove the functionality. The issue prominently arose in the setting of the example provided in the original issue.

The discussion in the ticket outlined the issue and this change was made to address the behavior that was described in the example repo :)

Copy link
Contributor

Choose a reason for hiding this comment

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

Got it. My concern is that if the tests pass even with the fix removed, then they aren't preventing us from regressing again.

I'm doing some refactoring of this code as part of #653 and I don't want to accidentally break it.

Do you think there is a test missing that I can include in that PR to ensure I don't regress?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants