-
-
Notifications
You must be signed in to change notification settings - Fork 395
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
Use participatory spaces slugs in URLs #1842
Conversation
Codecov Report
@@ Coverage Diff @@
## master #1842 +/- ##
==========================================
- Coverage 98.5% 98.47% -0.03%
==========================================
Files 1127 1127
Lines 25284 25316 +32
==========================================
+ Hits 24906 24931 +25
- Misses 378 385 +7 |
@@ -30,7 +30,12 @@ def current_participatory_process(env, params) | |||
end | |||
|
|||
def detect_current_participatory_process(params) | |||
OrganizationParticipatoryProcesses.new(@organization).query.find_by_id(params["participatory_process_id"]) | |||
organization_processes.where(id: params["participatory_process_id"]).first || | |||
organization_processes.where(slug: params["participatory_process_id"]).first |
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.
Can't we use ActiveRecord's OR?
@@ -42,7 +42,12 @@ def ability_context | |||
|
|||
def detect_participatory_process | |||
request.env["current_participatory_process"] || | |||
OrganizationParticipatoryProcesses.new(current_organization).query.find(params[:participatory_process_id] || params[:id]) | |||
organization_processes.where(id: params[:participatory_process_id] || params[:id]).first || |
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.
Ditto.
@mrcasals No big deal but IMO redirecting is better than serving the same content for both URLs with a 200 response. Search engines don't like it when different URLs serve the same content. |
get "/processes/:process_id/f/:feature_id", to: redirect { |params, _request| | ||
process = Decidim::ParticipatoryProcess.where(id: params[:process_id]).first | ||
process ? "/processes/#{process.slug}/f/#{params[:feature_id]}" : "/404" | ||
}, constraints: { process_id: /[0-9]+/ } |
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.
@mrcasals Maybe you can use find(params[:process_id])
here. If the record is not found, an ActiveRecord::RecordNotFound
error will be raised, which will result in a 404. I think that's better than manually redirecting to the "/404" page, since in that case the request will have a 200 status instead of a 404.
Also, to be technically correct, I guess this should be a fallback and the preferred routes should be matched first? I'm think of the super-unlikely case of numerical slugs.
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.
Rails routes run the first definition that matches, so I need these redirections on top since they have the constraint.
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.
Right, I see. The other alternative would be to manually redirect from the controller instead. But numerical slugs is a small edge case anyways.
@deivid-rodriguez good point. I added redirections in 09eb417 right before your comment, today I'll fix the |
f3ee3a7
to
b6c6dcb
Compare
d89887b
to
5de65a3
Compare
@@ -50,10 +50,14 @@ def scope | |||
@scope ||= current_organization.scopes.where(id: scope_id).first | |||
end | |||
|
|||
def to_param | |||
id |
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.
This attribute is not defined, isn't it? I think you meant context[:assembly_id]
, right?
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.
This code was being used, actually, so 🔥
{ host: organization.host, foreign_key.to_sym => id } | ||
{ | ||
host: organization.host, | ||
"#{underscored_name}_slug".to_sym => slug |
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.
Such hack lol 😄
@@ -59,10 +59,14 @@ def participatory_process_group | |||
Decidim::ParticipatoryProcessGroup.where(id: participatory_process_group_id).first | |||
end | |||
|
|||
def to_param | |||
id |
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.
Ditto.
@@ -14,14 +14,14 @@ class AdminEngine < ::Rails::Engine | |||
paths["db/migrate"] = nil | |||
|
|||
routes do | |||
resources :assemblies do | |||
resources :assemblies, param: :slug, except: :show do |
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.
The param name slug
why sometimes is called like this and sometimes like assembly_slug
? Is it intended?
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.
I simply renamed the old :id
/:assembly_id
to use the slug. I guess :assembly_id
was used to differentiate assembly IDs from feature IDs. I don't want to change this because we might move features to use slugs in the URLs, so this is good.
@@ -15,7 +15,7 @@ class AdminEngine < ::Rails::Engine | |||
|
|||
routes do | |||
resources :participatory_process_groups | |||
resources :participatory_processes, except: :show do | |||
resources :participatory_processes, param: :slug, except: :show do |
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.
Ditto.
52d03f9
to
cb0e987
Compare
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.
Nice job! 🎉
Can you add an entry on the CHANGELOG indicating we use slugs on URLs now, and also a note about redirects?
resources :assemblies, only: [:index, :show], path: "assemblies" do | ||
get "assemblies/:assembly_id", to: redirect { |params, _request| | ||
assembly = Decidim::Assembly.find(params[:assembly_id]) | ||
assembly ? "/assemblies/#{assembly.slug}" : "/404" |
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.
@mrcasals Isn't this line dead code now? find
either rises or returns a record. I don't think we need the manual "/404" redirection anymore?
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.
EDIT: Not the whole line, just the "/404" branch.
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.
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.
Definitely unexpected... 🤔
I think I will go back to what we used to have, and then investigate again.
Agree on the approach 👍
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.
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.
OK, last commit should fix it.
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.
👍 If you want to test this stuff, I added some support for it a while ago. It's the a 404 page
shared example.
e4277de
to
e595b06
Compare
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.
Do we have format validations on the slug field to make sure we only accept valid chars (a-Z, 0-9 and - I guess)
CHANGELOG.md
Outdated
@@ -6,6 +6,10 @@ | |||
|
|||
- **decidim-proposals**: Hide proposal vote button when proposal is answered and rejected. [\#1861](https://github.com/decidim/decidim/pull/1861) | |||
|
|||
**Changed** | |||
|
|||
- **decidim**: URLs now use the participatory space slug instead of the URL, 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) |
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.
Instead of the ID
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.
We're not technically redirecting, only serving the same content (except for the participatory process and assembly page where we are indeed redirecting). There's a difference in terms of SEO, so I'd say "Old routes using the space IDs still serve the same content as the ones using the slug".
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.
Mh I think it's a proper redirection, sending a 301 HHTP status code:
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.
I just tried it and it working perfectly! I was probably just misreading the patch, sorry! 😊
e595b06
to
a1461d2
Compare
Did you notice this warning when seeding?
Also, can you generate a development app right now? I just had a dependency problem but I'm not sure if it's my environment... |
OrganizationAssemblies.new(@organization).query.find_by_id(params["assembly_id"]) | ||
organization_assemblies.where(slug: params["assembly_slug"]).or( | ||
organization_assemblies.where(id: params["assembly_id"]) | ||
).first! |
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.
I think this is what confused me... Do we need the assembly_id
part? I tried and the redirection seems to work fine without it.
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.
I'll check it, sure!
@deivid-rodriguez CircleCI has some problems:
I haven't generated the development app, let me check this tomorrow. |
I'm having a look at it and it seems like a problem most likely caused by #1845... |
a1461d2
to
6fd8567
Compare
acaaa02
to
ef7313a
Compare
ef7313a
to
ea8a29f
Compare
🎩 What? Why?
Use process and assemblies slugs in URLs.
📌 Related Issues
📋 Subtasks