diff options
Diffstat (limited to 'app/controllers')
| -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 |
