Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

How to specify rule conditions (aka attributes_for used for building resources) when defining a rule with the block syntax #630

Closed
dzello opened this Issue May 26, 2012 · 8 comments

Comments

Projects
None yet
2 participants

dzello commented May 26, 2012

I wrote up a gist that shows how to get conditions to apply even when using the block syntax.

https://gist.github.com/2792585

Is there a better way to do this?

Collaborator

andhapp commented May 27, 2012

@dzello The gist will add user_id clause to all the rules where as with a rule without block syntax you only apply it to that particular rule. This could be a solution for one particular instance, but will fail in other situations. Your thoughts please, thanks.

dzello commented May 28, 2012

rules.last returns the last rule defined. So only that rule will have its conditions hash set.

Ideally, it'd be nice to get the rule as the return value of the can/cannot statement. However, those methods return the array of all rules specified thus far (which is already accessible as .rules).

Collaborator

andhapp commented May 28, 2012

The rules will be merged if you have multiple rules defined on the same class. In that case, it may result in unexpected result. Although, I am finding it hard to find such an instance. It's a little bit annoying the way it works and somewhat unexpected. Something that should be part of the next version.

dzello commented May 28, 2012

@andhapp Yes, that could cause unintended side effects.

However, I don't see any code that merges conditions of rules together by subject. (subject i.e. class). Can you elaborate further or tell me where you're seeing this in the code?

Collaborator

andhapp commented May 28, 2012

Yeah, sure. Please have a look at database_records method in the activerecord adapter. This method gets called to fetch the records and subsequently results in a call to merge_conditions method.

dzello commented May 28, 2012

Yeah, I see now, thanks. It seems like this is a potential confusion point with Cancan itself. In other words, whether you define conditions normally or hack them manually into a rule object a la the gist I wrote, you still need to be aware that these conditions are potentially merged for the #accessible_by checks.

Collaborator

andhapp commented May 28, 2012

Yeah. I don't know why we have merge conditions in there (probably to support Rails 2, I guess). However, with the newer versions that support Rails3, I'd like to see all the main work handled off to MetaWhere, Squeel or Arel itself. I did add a pull request to fix an issue around it.

Collaborator

andhapp commented Jun 7, 2012

@dzello: I'd close this issue since it's not actionable. However, if you'd like to discuss this issue further please subscribe to cancan@librelist.com mailing list and post your question there.

Please feel free to submit a pull request around your solution with some specs. Thanks. 👍

@andhapp andhapp closed this Jun 7, 2012

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