diff --git a/app/controllers/tag_corrections_controller.rb b/app/controllers/tag_corrections_controller.rb index 6dec9b5c8..c5e849558 100644 --- a/app/controllers/tag_corrections_controller.rb +++ b/app/controllers/tag_corrections_controller.rb @@ -5,6 +5,7 @@ class TagCorrectionsController < ApplicationController before_action :janitor_only, only: [:new, :create] def new + @from_wiki = request.referer.try(:include?, "wiki_pages") || false @correction = TagCorrection.new(params[:tag_id]) respond_with(@correction) end @@ -19,9 +20,12 @@ class TagCorrectionsController < ApplicationController if params[:commit] == "Fix" @correction.fix! - redirect_to tags_path(:search => {:name_matches => @correction.tag.name, :hide_empty => "no"}), :notice => "Tag will be fixed in a few seconds" + if params[:from_wiki].to_s.truthy? + return redirect_to(show_or_new_wiki_pages_path(title: WikiPage.normalize_name(@correction.tag.name)), notice: "Tag will be fixed in a few seconds") + end + redirect_to(tags_path(search: { name_matches: @correction.tag.name, hide_empty: "no"}), notice: "Tag will be fixed in a few seconds") else - redirect_to tags_path(:search => {:name_matches => @correction.tag.name}) + redirect_to(tags_path(search: { name_matches: @correction.tag.name })) end end end diff --git a/app/controllers/tags_controller.rb b/app/controllers/tags_controller.rb index 046a955d4..6cbe6c321 100644 --- a/app/controllers/tags_controller.rb +++ b/app/controllers/tags_controller.rb @@ -1,10 +1,11 @@ # frozen_string_literal: true class TagsController < ApplicationController - before_action :member_only, :only => [:edit, :update, :preview] + before_action :member_only, only: %i[edit update preview] respond_to :html, :json def edit + @from_wiki = request.referer.try(:include?, "wiki_pages") || false @tag = Tag.find(params[:id]) check_privilege(@tag) respond_with(@tag) @@ -38,7 +39,15 @@ class TagsController < ApplicationController @tag = Tag.find(params[:id]) check_privilege(@tag) @tag.update(tag_params) - respond_with(@tag) + respond_with(@tag) do |format| + format.html do + if @tag.from_wiki.to_s.truthy? + return redirect_to(show_or_new_wiki_pages_path(title: WikiPage.normalize_name(@tag.name)), notice: "Tag updated") + else + redirect_to(tags_path(search: { name_matches: @tag.name, hide_empty: "no" })) + end + end + end end private @@ -48,7 +57,7 @@ class TagsController < ApplicationController end def tag_params - permitted_params = [:category] + permitted_params = %i[category from_wiki] permitted_params << :is_locked if CurrentUser.is_admin? params.require(:tag).permit(permitted_params) diff --git a/app/models/tag.rb b/app/models/tag.rb index 819dbdff1..62db4a89c 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -16,6 +16,8 @@ class Tag < ApplicationRecord before_save :update_category, if: :category_changed? + attr_accessor :from_wiki + class CategoryMapping TagCategory::REVERSE_MAPPING.each do |value, category| define_method(category) do diff --git a/app/models/wiki_page.rb b/app/models/wiki_page.rb index 6099ac742..42969006f 100644 --- a/app/models/wiki_page.rb +++ b/app/models/wiki_page.rb @@ -47,7 +47,7 @@ class WikiPage < ApplicationRecord module SearchMethods def titled(title) - find_by(title: title&.downcase&.tr(" ", "_")) + find_by(title: WikiPage.normalize_name(title)) end def active @@ -217,6 +217,10 @@ class WikiPage < ApplicationRecord name.unicode_normalize(:nfkc).gsub(/[[:space:]]+/, " ").strip.tr(" ", "_") end + def self.normalize_name(name) + name&.downcase&.tr(" ", "_") + end + def skip_secondary_validations=(value) @skip_secondary_validations = value.to_s.truthy? end diff --git a/app/views/tag_corrections/new.html.erb b/app/views/tag_corrections/new.html.erb index b5ca7227c..2263d545c 100644 --- a/app/views/tag_corrections/new.html.erb +++ b/app/views/tag_corrections/new.html.erb @@ -11,7 +11,8 @@ - <%= form_tag(tag_correction_path(:tag_id => @correction.tag.id)) do %> + <%= form_tag(tag_correction_path(tag_id: @correction.tag.id)) do %> + <%= hidden_field_tag "from_wiki", @from_wiki %> <%= submit_tag "Fix" %> <%= submit_tag "Cancel" %> <% end %> diff --git a/app/views/tags/edit.html.erb b/app/views/tags/edit.html.erb index 208dae66e..b28651793 100644 --- a/app/views/tags/edit.html.erb +++ b/app/views/tags/edit.html.erb @@ -3,6 +3,7 @@

Tag: <%= link_to @tag.name, posts_path(:tags => @tag.name), :class => "tag-type-#{@tag.category}" %>

<%= custom_form_for(@tag) do |f| %> + <%= f.hidden_field(:from_wiki, value: @from_wiki) %> <% if @tag.is_locked? %>

This tag is category locked

<% else %> diff --git a/test/functional/tags_controller_test.rb b/test/functional/tags_controller_test.rb index 58dfc15e5..23aff4a71 100644 --- a/test/functional/tags_controller_test.rb +++ b/test/functional/tags_controller_test.rb @@ -53,14 +53,14 @@ class TagsControllerTest < ActionDispatch::IntegrationTest should "update the tag" do put_auth tag_path(@tag), @user, params: { tag: { category: Tag.categories.general } } - assert_redirected_to tag_path(@tag) + assert_redirected_to(tags_path(search: { name_matches: @tag.name, hide_empty: "no" })) assert_equal(Tag.categories.general, @tag.reload.category) end should "lock the tag for an admin" do put_auth tag_path(@tag), create(:admin_user), params: { tag: { is_locked: true } } - assert_redirected_to @tag + assert_redirected_to(tags_path(search: { name_matches: @tag.name, hide_empty: "no" })) assert_equal(true, @tag.reload.is_locked) end @@ -87,7 +87,7 @@ class TagsControllerTest < ActionDispatch::IntegrationTest @admin = create(:admin_user) put_auth tag_path(@tag), @admin, params: { tag: { category: Tag.categories.general } } - assert_redirected_to @tag + assert_redirected_to(tags_path(search: { name_matches: @tag.name, hide_empty: "no" })) assert_equal(Tag.categories.general, @tag.reload.category) end end