added new tag batch change model

This commit is contained in:
albert
2011-08-03 18:32:57 -04:00
parent 04103c3352
commit 3e2b2e7418
6 changed files with 94 additions and 17 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -0,0 +1,21 @@
<div id="c-moderator-tags">
<div id="a-edit">
<h1>Edit Tags</h1>
<%= form_tag(moderator_tag_path, :method => :put) do %>
<div class="input">
<label>Predicate</label>
<%= text_field :tag, :predicate %>
</div>
<div class="input">
<label>Consequent</label>
<%= text_field :tag, :consequent %>
</div>
<div class="input">
<%= submit_tag %>
</div>
<% end %>
</div>
</div>

View File

@@ -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]

View File

@@ -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