diff options
| author | Miquel Sabaté Solà <msabate@suse.com> | 2024-03-20 16:00:18 +0100 |
|---|---|---|
| committer | Miquel Sabaté Solà <msabate@suse.com> | 2024-03-20 16:00:18 +0100 |
| commit | 819f299d3bdc2485394120583b8a76a67b10b418 (patch) | |
| tree | fc7cedbcfd4f4b435649ebe35763f28ff9cc6520 /app | |
| parent | ccf588845a6e595d1c9b2726a05eddd34bc92488 (diff) | |
| download | operum-819f299d3bdc2485394120583b8a76a67b10b418.tar.gz operum-819f299d3bdc2485394120583b8a76a67b10b418.zip | |
tags: attempt to modify searches on tag rename
We don't want to break existing searches upon renaming a tag, since most
of searches will be based on tag names.
NOTE: if the UI of searches is improved, maybe the `tags:""` clause can
contain tag ids instead of names, and hence we won't need all this dance
afterwards.
Signed-off-by: Miquel Sabaté Solà <msabate@suse.com>
Diffstat (limited to 'app')
| -rw-r--r-- | app/controllers/tags_controller.rb | 22 |
1 files changed, 21 insertions, 1 deletions
diff --git a/app/controllers/tags_controller.rb b/app/controllers/tags_controller.rb index 9d87687..ee837ef 100644 --- a/app/controllers/tags_controller.rb +++ b/app/controllers/tags_controller.rb @@ -24,11 +24,31 @@ class TagsController < ApplicationController end def update - if @tag.update(tag_params) + updated = false + + # An update on the tag name can also mean that we need to modify existing + # saved searches so we don't break them. Thus, everything needs to pass if + # we want this action to succeed. For searches it's fine to `#update!` with + # a bang and let the `rescue` below throw a generic error message. For + # `@tag` itself we can go the usual Rails-way, but since we don't want + # errors to be mixed together, we call `#update` without a bang and save the + # result on a boolean variable. + ActiveRecord::Base.transaction do + Search.where('body LIKE ?', "tag:\"#{@tag.name}\"").find_each do |s| + body = s.body.gsub("tag:\"#{@tag.name}\"", "tag:\"#{tag_params['name']}\"") + s.update!(body:) + end + + updated = @tag.update(tag_params) + end + + if updated redirect_to tags_url, notice: t('tags.update-success') else render :edit, status: :unprocessable_entity end + rescue ActiveRecord::RecordInvalid + redirect_to edit_tag_path(@tag), alert: t('tags.update-fail') end def destroy |
