2019-05-02 17:17:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-07-15 07:54:28 -05:00
|
|
|
class Admin::PermalinksController < Admin::AdminController
|
2024-11-13 17:03:58 -06:00
|
|
|
before_action :fetch_permalink, only: %i[show update destroy]
|
2015-07-15 07:54:28 -05:00
|
|
|
|
|
|
|
def index
|
2015-07-17 05:23:48 -05:00
|
|
|
url = params[:filter]
|
|
|
|
permalinks = Permalink.filter_by(url)
|
2015-07-15 07:54:28 -05:00
|
|
|
render_serialized(permalinks, PermalinkSerializer)
|
|
|
|
end
|
|
|
|
|
2024-11-13 17:03:58 -06:00
|
|
|
def new
|
|
|
|
end
|
|
|
|
|
|
|
|
def edit
|
|
|
|
end
|
2015-07-15 07:54:28 -05:00
|
|
|
|
2024-11-13 17:03:58 -06:00
|
|
|
def show
|
|
|
|
render_serialized(@permalink, PermalinkSerializer)
|
|
|
|
end
|
2020-05-25 04:48:54 -05:00
|
|
|
|
2024-11-13 17:03:58 -06:00
|
|
|
def create
|
2024-11-22 14:11:26 -06:00
|
|
|
permalink = Permalink.create!(permalink_params)
|
2024-11-13 17:03:58 -06:00
|
|
|
render_serialized(permalink, PermalinkSerializer)
|
|
|
|
rescue ActiveRecord::RecordInvalid => e
|
|
|
|
render_json_error(e.record.errors.full_messages)
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2024-11-22 14:11:26 -06:00
|
|
|
@permalink.update!(permalink_params)
|
2024-11-13 17:03:58 -06:00
|
|
|
render_serialized(@permalink, PermalinkSerializer)
|
|
|
|
rescue ActiveRecord::RecordInvalid => e
|
|
|
|
render_json_error(e.record.errors.full_messages)
|
2015-07-15 07:54:28 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
@permalink.destroy
|
|
|
|
render json: success_json
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def fetch_permalink
|
|
|
|
@permalink = Permalink.find(params[:id])
|
|
|
|
end
|
2024-11-13 17:03:58 -06:00
|
|
|
|
|
|
|
def permalink_params
|
2024-11-22 14:11:26 -06:00
|
|
|
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
|
2024-11-13 17:03:58 -06:00
|
|
|
end
|
2015-07-15 07:54:28 -05:00
|
|
|
end
|