diff --git a/app/controllers/moderator/tags_controller.rb b/app/controllers/moderator/tags_controller.rb new file mode 100644 index 000000000..d579d7651 --- /dev/null +++ b/app/controllers/moderator/tags_controller.rb @@ -0,0 +1,14 @@ +module Moderator + class TagsController < ApplicationController + before_filter :moderator_only + + def edit + end + + def update + tag_batch_change = TagBatchChange.new(params[:tag][:predicate], params[:tag][:consequent]) + tag_batch_change.execute + redirect_to edit_moderator_tag_path, :notice => "Posts updated" + end + end +end diff --git a/app/logical/moderator/tag_batch_change.rb b/app/logical/moderator/tag_batch_change.rb new file mode 100644 index 000000000..835612239 --- /dev/null +++ b/app/logical/moderator/tag_batch_change.rb @@ -0,0 +1,24 @@ +module Moderator + class TagBatchChange + class Error < Exception ; end + + attr_reader :predicate, :consequent + + def initialize(predicate, consequent) + @predicate = predicate + @consequent = consequent + end + + def execute + raise Error.new("Predicate is missing") if predicate.blank? + + normalized_predicate = TagAlias.to_aliased(::Tag.scan_tags(predicate)) + normalized_consequent = TagAlias.to_aliased(::Tag.scan_tags(consequent)) + + ::Post.tag_match(predicate).each do |post| + tags = (post.tag_array - normalized_predicate + normalized_consequent).join(" ") + post.update_attributes(:tag_string => tags) + end + end + end +end diff --git a/app/models/tag.rb b/app/models/tag.rb index ee6d65cf6..2df386603 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -106,21 +106,7 @@ class Tag < ActiveRecord::Base m.extend(ClassMethods) end end - - module UpdateMethods - def mass_edit(start_tags, result_tags, updater_id, updater_ip_addr) - updater = User.find(updater_id) - Post.tag_match(start_tags).each do |p| - start = TagAlias.to_aliased(scan_tags(start_tags)) - result = TagAlias.to_aliased(scan_tags(result_tags)) - tags = (p.tag_array - start + result).join(" ") - CurrentUser.scoped(updater, updater_ip_addr) do - p.update_attributes(:tag_string => tags) - end - end - end - end - + module ParseMethods def normalize(query) query.to_s.downcase.strip @@ -367,7 +353,6 @@ class Tag < ActiveRecord::Base include CategoryMethods extend StatisticsMethods include NameMethods - extend UpdateMethods extend ParseMethods extend SuggestionMethods end diff --git a/app/views/moderator/tags/edit.html.erb b/app/views/moderator/tags/edit.html.erb new file mode 100644 index 000000000..0b56da32a --- /dev/null +++ b/app/views/moderator/tags/edit.html.erb @@ -0,0 +1,21 @@ +
diff --git a/config/routes.rb b/config/routes.rb index dda1785a8..f7bc45a8a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -10,7 +10,7 @@ Danbooru::Application.routes.draw do end end resources :invitations, :only => [:new, :create, :index] - resource :tag + resource :tag, :only => [:edit, :update] namespace :post do resource :dashboard, :only => [:show] resource :approval, :only => [:create] diff --git a/test/unit/moderator/tag_batch_change_test.rb b/test/unit/moderator/tag_batch_change_test.rb new file mode 100644 index 000000000..c547b6a45 --- /dev/null +++ b/test/unit/moderator/tag_batch_change_test.rb @@ -0,0 +1,33 @@ +require "test_helper" + +module Moderator + class TagBatchChangeTest < ActiveSupport::TestCase + context "a tag batch change" do + setup do + @user = Factory.create(:moderator_user) + CurrentUser.user = @user + CurrentUser.ip_addr = "127.0.0.1" + @post = Factory.create(:post, :tag_string => "aaa") + end + + teardown do + CurrentUser.user = nil + CurrentUser.ip_addr = nil + end + + should "execute" do + tag_batch_change = TagBatchChange.new("aaa", "bbb") + tag_batch_change.execute + @post.reload + assert_equal("bbb", @post.tag_string) + end + + should "raise an error if there is no predicate" do + tag_batch_change = TagBatchChange.new("", "bbb") + assert_raises(TagBatchChange::Error) do + tag_batch_change.execute + end + end + end + end +end