Skip to content

Migrate cancan to pundit [open review]#520

Merged
devton merged 27 commits into
catarse:masterfrom
diogob:migrate_cancan_to_pundit
Jan 27, 2014
Merged

Migrate cancan to pundit [open review]#520
devton merged 27 commits into
catarse:masterfrom
diogob:migrate_cancan_to_pundit

Conversation

@diogob
Copy link
Copy Markdown
Contributor

@diogob diogob commented Jan 13, 2014

This is an example of how we could implement our privileges system using Pundit instead of CanCan. I've replaced CanCan only in ProjectsController to make this PR smaller. Doing so we can make a smooth migration analysing and discussing smaller changes and removing cancan only after everything is working properly with Pundit.

It's important to observe that later refactorings will be necessary to DRY the code after the migration is complete.

@diogob
Copy link
Copy Markdown
Contributor Author

diogob commented Jan 13, 2014

@devton @adrianob @josemarluedke & @joaomilho please take a look. This is a first draft I've made of a new privilege system for Catarse. This PR would start a smooth migration, so it would be the first of several and it would imply Pundit and CanCan coexisting for a while. I've chosen the ProjectsController as a starting point for it's a central piece with some attribute restrictions and complex rules.

@coveralls
Copy link
Copy Markdown

Coverage Status

Changes Unknown when pulling 45cde19 on diogob:migrate_cancan_to_pundit into * on catarse:master*.

Comment thread app/policies/project_policy.rb Outdated
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The only thing I disagree for now is this line, we should specify the project attributes even for admins. With that change, I guest it should be as the example on pundit documentation, return only an array with the params and let the controller deal with the rest.

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.

Yeah, I thought the same initially but it seemed a bit clumsy to specify all fields considering that I'm an admin and I have the permit!
But I guess I can return all fields dynamically when user is an admin.

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.

I've done this but our code is still a bit different from the Pundit example, as the permitted attributes is done by inherited resources as seen here:
https://github.com/josevalim/inherited_resources/blob/master/lib/inherited_resources/base_helpers.rb#L321

@josemarluedke
Copy link
Copy Markdown
Contributor

Is there anyway to make the pundit and inherited_resources work together instead of saying authorize on each action?

@josemarluedke
Copy link
Copy Markdown
Contributor

Also we should create specs for the policies.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

dont forget to remove before merging

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.

Thanks, I'll remove this.

@joaomilho
Copy link
Copy Markdown
Contributor

Overall I think Pundit is the way to go.

@diogob
Copy link
Copy Markdown
Contributor Author

diogob commented Jan 15, 2014

@josemarluedke We'll aim for a better integration with inherited resources after migrating everything from CanCan, but that's the idea ;)

@diogob
Copy link
Copy Markdown
Contributor Author

diogob commented Jan 15, 2014

@josemarluedke & @joaomilho regarding the policies specs I'm in doubt. Because the controller specs give us a good coverage (except in cases where we use policies in views). Nevertheless I feel more confortable creating specs with Pundit than I was with that loathsome ability_spec. So do you think it's worth to spec every policy?

@joaomilho
Copy link
Copy Markdown
Contributor

I'm not sure unit testing policies is a good thing. I have to think more about it. Do you have a stance on it?

Anyway, it's not hard http://thunderboltlabs.com/blog/2013/03/27/testing-pundit-policies-with-rspec/ (well, testing belongs/has is also easy but controversial)

@joaomilho
Copy link
Copy Markdown
Contributor

I'm using "unit testing" as a verb. :/

@josemarluedke
Copy link
Copy Markdown
Contributor

Well, I have been thinking about this since @diogob said about not having specs for it. I'm not sure anymore about testing each policy separated. But if we choose to not test it, we need make sure that controllers specs cover all cases of permission. Also the policy on views is other open question at this point.

@coveralls
Copy link
Copy Markdown

Coverage Status

Coverage remained the same when pulling 7ebb4ac on diogob:migrate_cancan_to_pundit into ffa1967 on catarse:master.

@coveralls
Copy link
Copy Markdown

Coverage Status

Coverage remained the same when pulling bbfd865 on diogob:migrate_cancan_to_pundit into ffa1967 on catarse:master.

@coveralls
Copy link
Copy Markdown

Coverage Status

Coverage remained the same when pulling bbfd865 on diogob:migrate_cancan_to_pundit into ffa1967 on catarse:master.

@diogob
Copy link
Copy Markdown
Contributor Author

diogob commented Jan 20, 2014

@josemarluedke @joaomilho I've decided to create the policy specs as we use the project policy in some views. I still need to add some commits to this PR migrating the view policy checks.

@joaomilho
Copy link
Copy Markdown
Contributor

cool, are you helping the brazilian guy?

@diogob
Copy link
Copy Markdown
Contributor Author

diogob commented Jan 20, 2014

@joaomilho brazilian guy?

@luizfonseca
Copy link
Copy Markdown
Contributor

@diogob maybe using this gem (https://github.com/yakko/before_actions) from a friend of mine could help you with DRY on the controller (and get rid of Inherited Resources too).

+1 for specs for policies.

@diogob
Copy link
Copy Markdown
Contributor Author

diogob commented Jan 20, 2014

@luizfonseca why get rid of inherited_resources ? We could also DRY the controller using it in a better way ;)

@diogob
Copy link
Copy Markdown
Contributor Author

diogob commented Jan 20, 2014

@devton @adrianob please DO NOT merge this yet, I still need to fix some things in the project view.

@coveralls
Copy link
Copy Markdown

Coverage Status

Coverage remained the same when pulling 2267994 on diogob:migrate_cancan_to_pundit into 71e9978 on catarse:master.

@coveralls
Copy link
Copy Markdown

Coverage Status

Coverage remained the same when pulling 881b3f3 on diogob:migrate_cancan_to_pundit into 71e9978 on catarse:master.

@coveralls
Copy link
Copy Markdown

Coverage Status

Coverage remained the same when pulling 881b3f3 on diogob:migrate_cancan_to_pundit into 71e9978 on catarse:master.

@coveralls
Copy link
Copy Markdown

Coverage Status

Coverage remained the same when pulling 881b3f3 on diogob:migrate_cancan_to_pundit into 71e9978 on catarse:master.

@coveralls
Copy link
Copy Markdown

Coverage Status

Coverage remained the same when pulling 835a6dc on diogob:migrate_cancan_to_pundit into 71e9978 on catarse:master.

@diogob
Copy link
Copy Markdown
Contributor Author

diogob commented Jan 24, 2014

@adrianob @devton I think this is ready to merge now.

@josemarluedke
Copy link
Copy Markdown
Contributor

Nice!

devton added a commit that referenced this pull request Jan 27, 2014
Migrate cancan to pundit [open review]
@devton devton merged commit 582b108 into catarse:master Jan 27, 2014
@diogob diogob deleted the migrate_cancan_to_pundit branch January 31, 2014 17:01
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.

6 participants