-
-
Notifications
You must be signed in to change notification settings - Fork 640
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
Conversation
There was a problem hiding this 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?
Sure ^^ I will see to it today or tomorrow :D |
Fix an issue that caused associations not to work in their usage as a subject for a rule
I added some tests to verify that this also works with |
lib/cancan/conditions_matcher.rb
Outdated
@@ -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? |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
@@ -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) } |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 :)
There was a problem hiding this comment.
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?
Fix the issue outlined in #536 and validate behaviour in test