FIX: Permalink.create didn't work as expected anymore (#29895)

This moves the logic of setting the correct permalink values back into the controller. And it replaces the validation with a simpler one, that always works, even when the model is loaded from the DB.

Follow-up to #29634 which broke import scripts and lots of documentation on Meta.
This commit is contained in:
Gerhard Schlager
2024-11-22 21:11:26 +01:00
committed by GitHub
parent 44fbf1048c
commit 0295b4165c
9 changed files with 78 additions and 216 deletions

View File

@ -20,24 +20,14 @@ class Admin::PermalinksController < Admin::AdminController
end
def create
permalink =
Permalink.create!(
url: permalink_params[:url],
permalink_type: permalink_params[:permalink_type],
permalink_type_value: permalink_params[:permalink_type_value],
)
permalink = Permalink.create!(permalink_params)
render_serialized(permalink, PermalinkSerializer)
rescue ActiveRecord::RecordInvalid => e
render_json_error(e.record.errors.full_messages)
end
def update
@permalink.update!(
url: permalink_params[:url],
permalink_type: permalink_params[:permalink_type],
permalink_type_value: permalink_params[:permalink_type_value],
)
@permalink.update!(permalink_params)
render_serialized(@permalink, PermalinkSerializer)
rescue ActiveRecord::RecordInvalid => e
render_json_error(e.record.errors.full_messages)
@ -55,6 +45,24 @@ class Admin::PermalinksController < Admin::AdminController
end
def permalink_params
params.require(:permalink).permit(:url, :permalink_type, :permalink_type_value)
permitted_params =
params.require(:permalink).permit(:url, :permalink_type, :permalink_type_value)
{
url: permitted_params[:url],
topic_id: extract_param(permitted_params, "topic"),
post_id: extract_param(permitted_params, "post"),
category_id: extract_param(permitted_params, "category"),
tag_id:
extract_param(permitted_params, "tag").then do |tag_name|
(Tag.where(name: tag_name).pluck(:id).first || -1) if tag_name
end,
user_id: extract_param(permitted_params, "user"),
external_url: extract_param(permitted_params, "external_url"),
}
end
def extract_param(permitted_params, type)
permitted_params[:permalink_type] == type ? permitted_params[:permalink_type_value] : nil
end
end