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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't use constants for authorization handlers configuration #2016

Merged
merged 5 commits into from
Oct 10, 2017

Conversation

deivid-rodriguez
Copy link
Contributor

@deivid-rodriguez deivid-rodriguez commented Oct 9, 2017

馃帺 What? Why?

Tests on decidim-barcelona running against current decidim master are failing because the authorization handler class is not yet available by the time tests are loaded.

I think it's better to not use constants directly on code that is evaluated at initialization/load time, since we need to make sure the constants are actually available at the time. That's inconvenient because you never need to care about that in other situations, and I sometimes have seen it causing problems with code (auto)loading.

This is similar to Rails deprecating specifying class_name as a constant in ActiveRecord associations.

This PR also includes some other refactorings in authorization handlers that I've extracted from another PR I'm working on (#1815 & #1871), to make reviewing that one easier.

馃搶 Related Issues

馃搵 Subtasks

None.

馃摲 Screenshots (optional)

None.

馃懟 GIF

house_of_cards

@ghost ghost assigned deivid-rodriguez Oct 9, 2017
@ghost ghost added the in-progress label Oct 9, 2017
@codecov
Copy link

codecov bot commented Oct 9, 2017

Codecov Report

Merging #2016 into master will increase coverage by <.01%.
The diff coverage is 100%.

@@            Coverage Diff             @@
##           master    #2016      +/-   ##
==========================================
+ Coverage   98.48%   98.48%   +<.01%     
==========================================
  Files        1144     1145       +1     
  Lines       25754    25756       +2     
==========================================
+ Hits        25363    25365       +2     
  Misses        391      391

CHANGELOG.md Outdated
@@ -16,6 +16,7 @@
- **decidim**: URLs now use the participatory space slug instead of the ID, both in the public pages and in the admin. Old routes using the space IDs now redirect to the ones using the slug. [\#1842](https://github.com/decidim/decidim/pull/1842)
- **decidim**: `bin/rails generate decidim:demo` is no longer available in generated applications. Use the `--demo` flag when generating your application or do the change manually if you want to use the "demo" authorization handler. [\#1978](https://github.com/decidim/decidim/pull/1978)
- **decidim-core**: Changes some texts in the homepage ("How do I take part in a process?" section) [\#1947](https://github.com/decidim/decidim/pull/1947)
- **decidim-core**: Authorization handlers now must be specified as strings. To migrate, change `config.authorization_handlers = [MyHandler]` to `config.authorization_handlers = ["MyHandler"]`. [\#19](https://github.com/decidim/decidim/pull/19)
Copy link
Contributor

Choose a reason for hiding this comment

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

s/19/2016/

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ooops 馃槼, updated!

@@ -14,7 +14,7 @@

<div class="field">
<%= f.label :available_authorizations %>
<%= f.collection_check_boxes :available_authorizations, Decidim.authorization_handlers, :name, :name %>
<%= f.collection_check_boxes :available_authorizations, Decidim.authorization_handlers, :itself, :itself %>
Copy link
Contributor

Choose a reason for hiding this comment

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

itself?????????????? OMG I didn't know this existed 馃槶

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Pretty crazy, eh? Ruby style. I remember seeing it in docs or somewhere, but only now I found a use case.

Copy link
Contributor

Choose a reason for hiding this comment

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

It's awesome 鉂わ笍

Copy link
Contributor

Choose a reason for hiding this comment

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

Sad & nice at the same time!

And set it just once before the whole suite.
So we don't depend on the constant being already available when
configuration is read.
The handler is never supposed to be instantiated from the model and it's
only used for validations. This changes make that more explicit.
Copy link
Contributor

@josepjaume josepjaume left a comment

Choose a reason for hiding this comment

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

Super nice! Rails auto-loading & initialization time constants don't play along together.

@mrcasals mrcasals merged commit daacf10 into master Oct 10, 2017
@mrcasals mrcasals deleted the fix/delay_constant_evaluation branch October 10, 2017 08:58
@ghost ghost removed the in-progress label Oct 10, 2017
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.

None yet

3 participants