From fcd80b6043830e7234cd37588a665efed1cb4fba Mon Sep 17 00:00:00 2001 From: Albert Yi Date: Wed, 16 May 2018 15:33:32 -0700 Subject: [PATCH] Artist urls can be prepended with a '-' to mark is inactive (#3388) --- app/assets/javascripts/related_tag.js.erb | 16 ++++++++++++++-- app/models/artist_url.rb | 14 +++++++++++++- app/views/artists/_summary.html.erb | 2 +- ...0180516222413_add_is_active_to_artist_urls.rb | 7 +++++++ db/structure.sql | 6 ++++-- test/unit/artist_test.rb | 6 ++++++ test/unit/artist_url_test.rb | 7 +++++++ 7 files changed, 52 insertions(+), 6 deletions(-) create mode 100644 db/migrate/20180516222413_add_is_active_to_artist_urls.rb diff --git a/app/assets/javascripts/related_tag.js.erb b/app/assets/javascripts/related_tag.js.erb index 44c64f3c4..1d05d4704 100644 --- a/app/assets/javascripts/related_tag.js.erb +++ b/app/assets/javascripts/related_tag.js.erb @@ -142,7 +142,11 @@ tags.push(["BANNED_ARTIST", "banned"]); } $.each(Danbooru.RelatedTag.recent_artists[0].sorted_urls, function(i, url) { - tags.push([" " + url.url, 0]); + var x = url.url; + if (!url.is_active) { + x = "-" + x; + } + tags.push([" " + x, 0]); }); } else if (Danbooru.RelatedTag.recent_artists.length >= 10) { tags.push([" none", 0]); @@ -226,7 +230,15 @@ ); } else { var text = tag[0]; - if (text.match(/^ http/)) { + if (text.match(/^ -http/)) { + text = text.substring(1, 1000); + var desc = text.replace(/^-https?:\/\//, ""); + if (desc.length > 30) { + desc = desc.substring(0, 30) + "..."; + } + var $del = $("").html(desc); + $ul.append($("
  • ").html($del)); + } else if (text.match(/^ http/)) { text = text.substring(1, 1000); var $url = $(""); var desc = text.replace(/^https?:\/\//, ""); diff --git a/app/models/artist_url.rb b/app/models/artist_url.rb index fa450c34c..7952ac5f7 100644 --- a/app/models/artist_url.rb +++ b/app/models/artist_url.rb @@ -1,4 +1,5 @@ class ArtistUrl < ApplicationRecord + before_validation :parse_prefix before_save :initialize_normalized_url, on: [ :create ] before_save :normalize validates_presence_of :url @@ -58,6 +59,13 @@ class ArtistUrl < ApplicationRecord url = url.gsub(/^http:\/\/i\d+\.pixiv\.net\/img\d+/, "http://*.pixiv.net/img*") end + def parse_prefix + if url && url[0] == "-" + self.url = url[1..-1] + self.is_active = false + end + end + def priority if normalized_url =~ /pixiv\.net\/member\.php/ 10 @@ -89,7 +97,11 @@ class ArtistUrl < ApplicationRecord end def to_s - url + if is_active? + url + else + "-#{url}" + end end def validate_url_format diff --git a/app/views/artists/_summary.html.erb b/app/views/artists/_summary.html.erb index f51c51db4..fccff838e 100644 --- a/app/views/artists/_summary.html.erb +++ b/app/views/artists/_summary.html.erb @@ -30,7 +30,7 @@
      <% artist.urls.each do |url| %>
    • - <%= link_to h(url.to_s), h(url.to_s) %> + <%= link_to h(url.to_s), h(url) %> <% if CurrentUser.user.is_moderator? %> [<%= link_to("mass edit", edit_moderator_tag_path(:antecedent => "-#{artist.name} source:#{ArtistUrl.normalize_for_search(url.to_s)}", :consequent => artist.name)) %>] <% end %> diff --git a/db/migrate/20180516222413_add_is_active_to_artist_urls.rb b/db/migrate/20180516222413_add_is_active_to_artist_urls.rb new file mode 100644 index 000000000..225b4c3cd --- /dev/null +++ b/db/migrate/20180516222413_add_is_active_to_artist_urls.rb @@ -0,0 +1,7 @@ +class AddIsActiveToArtistUrls < ActiveRecord::Migration[5.2] + def change + ApplicationRecord.without_timeout do + add_column :artist_urls, :is_active, :boolean, null: false, default: true + end + end +end diff --git a/db/structure.sql b/db/structure.sql index aad387a7a..d25506007 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -683,7 +683,8 @@ CREATE TABLE public.artist_urls ( url text NOT NULL, normalized_url text NOT NULL, created_at timestamp without time zone, - updated_at timestamp without time zone + updated_at timestamp without time zone, + is_active boolean DEFAULT true NOT NULL ); @@ -7494,6 +7495,7 @@ INSERT INTO "schema_migrations" (version) VALUES ('20180116001101'), ('20180403231351'), ('20180413224239'), -('20180425194016'); +('20180425194016'), +('20180516222413'); diff --git a/test/unit/artist_test.rb b/test/unit/artist_test.rb index c0ce04e9d..d61f819fc 100644 --- a/test/unit/artist_test.rb +++ b/test/unit/artist_test.rb @@ -29,6 +29,12 @@ class ArtistTest < ActiveSupport::TestCase CurrentUser.ip_addr = nil end + should "parse inactive urls" do + @artist = Artist.create(name: "blah", url_string: "-http://monet.com") + assert_equal(["-http://monet.com"], @artist.urls.map(&:to_s)) + refute(@artist.urls[0].is_active?) + end + should "should have a valid name" do @artist = Artist.new(:name => "-blah") @artist.save diff --git a/test/unit/artist_url_test.rb b/test/unit/artist_url_test.rb index 37fa84bca..8cbeef412 100644 --- a/test/unit/artist_url_test.rb +++ b/test/unit/artist_url_test.rb @@ -12,6 +12,13 @@ class ArtistUrlTest < ActiveSupport::TestCase CurrentUser.ip_addr = nil end + should "allow urls to be marked as inactive" do + url = FactoryBot.create(:artist_url, :url => "-http://monet.com") + assert_equal("http://monet.com", url.url) + assert_equal("http://monet.com/", url.normalized_url) + refute(url.is_active?) + end + should "always add a trailing slash when normalized" do url = FactoryBot.create(:artist_url, :url => "http://monet.com") assert_equal("http://monet.com", url.url)