From bae1f39dcf262e428e9ce367982024cd099378e5 Mon Sep 17 00:00:00 2001 From: albert Date: Tue, 20 Dec 2011 11:46:07 -0500 Subject: [PATCH] added alias and implication importer --- ...lias_and_implication_imports_controller.rb | 18 +++++ app/logical/alias_and_implication_importer.rb | 65 +++++++++++++++++++ .../new.html.erb | 36 ++++++++++ app/views/static/site_map.html.erb | 1 + config/routes.rb | 1 + .../alias_and_implication_importer_test.rb | 42 ++++++++++++ 6 files changed, 163 insertions(+) create mode 100644 app/controllers/admin/alias_and_implication_imports_controller.rb create mode 100644 app/logical/alias_and_implication_importer.rb create mode 100644 app/views/admin/alias_and_implication_imports/new.html.erb create mode 100644 test/unit/alias_and_implication_importer_test.rb diff --git a/app/controllers/admin/alias_and_implication_imports_controller.rb b/app/controllers/admin/alias_and_implication_imports_controller.rb new file mode 100644 index 000000000..1bf119be1 --- /dev/null +++ b/app/controllers/admin/alias_and_implication_imports_controller.rb @@ -0,0 +1,18 @@ +module Admin + class AliasAndImplicationImportsController < ApplicationController + before_filter :admin_only + + def new + end + + def create + @importer = AliasAndImplicationImporter.new(params[:batch][:text], params[:batch][:forum_id]) + @importer.process! + flash[:notice] = "Import queued" + redirect_to new_admin_alias_and_implication_import_path + rescue => x + flash[:notice] = x.to_s + redirect_to new_admin_alias_and_implication_import_path + end + end +end diff --git a/app/logical/alias_and_implication_importer.rb b/app/logical/alias_and_implication_importer.rb new file mode 100644 index 000000000..a1905ed62 --- /dev/null +++ b/app/logical/alias_and_implication_importer.rb @@ -0,0 +1,65 @@ +class AliasAndImplicationImporter + attr_accessor :text, :commands, :forum_id + + def initialize(text, forum_id) + @forum_id = forum_id + @text = text + end + + def process! + tokens = tokenize(text) + parse(tokens) + end + +private + + def tokenize(text) + text.gsub!(/^\s+/, "") + text.gsub!(/\s+$/, "") + text.gsub!(/ {2,}/, " ") + text.split(/\r\n|\r|\n/).map do |line| + if line =~ /create alias (\S+) -> (\S+)/i + [:create_alias, $1, $2] + elsif line =~ /create implication (\S+) -> (\S+)/i + [:create_implication, $1, $2] + elsif line =~ /remove alias (\S+) -> (\S+)/i + [:remove_alias, $1, $2] + elsif line =~ /remove implication (\S+) -> (\S+)/i + [:remove_implication, $1, $2] + elsif line.empty? + # do nothing + else + raise "Unparseable line: #{line}" + end + end + end + + def parse(tokens) + ActiveRecord::Base.transaction do + tokens.map do |token| + case token[0] + when :create_alias + tag_alias = TagAlias.create(:forum_topic_id => forum_id, :status => "pending", :antecedent_name => token[1], :consequent_name => token[2]) + tag_alias.delay.process! + + when :create_implication + tag_implication = TagImplication.create(:forum_topic_id => forum_id, :status => "pending", :antecedent_name => token[1], :consequent_name => token[2]) + tag_implication.delay.process! + + when :remove_alias + tag_alias = TagAlias.where("antecedent_name = ?", token[1]).first + raise "Alias for #{token[1]} not found" if tag_alias.nil? + tag_alias.destroy + + when :remove_implication + tag_implication = TagImplication.where("antecedent_name = ? and consequent_name = ?", token[1], token[2]).first + raise "Implication for #{token[1]} not found" if tag_implication.nil? + tag_implication.destroy + + else + raise "Unknown token: #{token[0]}" + end + end + end + end +end diff --git a/app/views/admin/alias_and_implication_imports/new.html.erb b/app/views/admin/alias_and_implication_imports/new.html.erb new file mode 100644 index 000000000..7ac67cddf --- /dev/null +++ b/app/views/admin/alias_and_implication_imports/new.html.erb @@ -0,0 +1,36 @@ +
+
+

Alias & Implication Import

+ + <%= form_tag(admin_alias_and_implication_import_path, :class => "simple_form") do %> +
+Use the following format:
+
+remove alias aaa -> bbb
+remove implication aaa -> bbb
+create alias aaa -> bbb
+create implication aaa -> bbb
+      
+ +
+ + <%= text_area "batch", "text", :size => "60x20" %> +
+ +
+ + <%= text_field "batch", "forum_id" %> +
+ + <%= submit_tag %> + <% end %> +
+
+ +<% content_for(:page_title) do %> + Alias & Implication Import - <%= Danbooru.config.app_name %> +<% end %> diff --git a/app/views/static/site_map.html.erb b/app/views/static/site_map.html.erb index badd27e16..702dc6434 100644 --- a/app/views/static/site_map.html.erb +++ b/app/views/static/site_map.html.erb @@ -83,6 +83,7 @@
  • <%= link_to("Janitor Trials", janitor_trials_path) %>
  • <%= link_to("IP Bans", ip_bans_path) %>
  • <%= link_to("News Updates", news_updates_path) %>
  • +
  • <%= link_to("Alias & Implication Import", new_admin_alias_and_implication_import_path) %>
  • <% end %> diff --git a/config/routes.rb b/config/routes.rb index 85579bb0a..3beb5a90c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,6 +1,7 @@ Danbooru::Application.routes.draw do namespace :admin do resources :users, :only => [:edit, :update] + resource :alias_and_implication_import, :only => [:new, :create] end namespace :moderator do resource :dashboard, :only => [:show] diff --git a/test/unit/alias_and_implication_importer_test.rb b/test/unit/alias_and_implication_importer_test.rb new file mode 100644 index 000000000..f28ae5a2f --- /dev/null +++ b/test/unit/alias_and_implication_importer_test.rb @@ -0,0 +1,42 @@ +require 'test_helper' + +class AliasAndImplicationImporterTest < ActiveSupport::TestCase + context "The alias and implication importer" do + setup do + @user = Factory.create(:user) + CurrentUser.user = @user + CurrentUser.ip_addr = "127.0.0.1" + end + + teardown do + CurrentUser.user = nil + CurrentUser.ip_addr = nil + end + + context "given a valid list" do + setup do + @list = "create alias abc -> def\ncreate implication aaa -> bbb\n" + @importer = AliasAndImplicationImporter.new(@list, nil) + end + + should "process it" do + assert_difference("Delayed::Job.count", 2) do + @importer.process! + end + end + end + + context "given a list with a logic error" do + setup do + @list = "remove alias zzz -> yyy\n" + @importer = AliasAndImplicationImporter.new(@list, nil) + end + + should "throw an exception" do + assert_raises(RuntimeError) do + @importer.process! + end + end + end + end +end