From 141044d352de804500e640433e1f70e66d0d8b20 Mon Sep 17 00:00:00 2001 From: evazion Date: Wed, 18 May 2022 12:54:43 -0500 Subject: [PATCH] posts: refactor hardcoded ratings. Refactor ratings to not be hardcoded in various places. Make it so all ratings are defined in Post::RATINGS. Also make it so that you can search multiple ratings at once with `rating:q,e`. --- app/javascript/src/javascripts/blacklists.js | 2 +- app/logical/autocomplete_service.rb | 2 +- app/logical/post_query_builder.rb | 2 +- app/models/post.rb | 27 ++++++++++--------- app/models/post_version.rb | 11 +------- app/views/posts/partials/show/_edit.html.erb | 2 +- .../uploads/_single_asset_upload.html.erb | 2 +- test/unit/post_query_builder_test.rb | 1 + 8 files changed, 21 insertions(+), 28 deletions(-) diff --git a/app/javascript/src/javascripts/blacklists.js b/app/javascript/src/javascripts/blacklists.js index 2a4c6006d..6f827e327 100644 --- a/app/javascript/src/javascripts/blacklists.js +++ b/app/javascript/src/javascripts/blacklists.js @@ -33,7 +33,7 @@ Blacklist.parse_entry = function(string) { } Blacklist.parse_entries = function() { - var entries = (Utility.meta("blacklisted-tags") || "nozomiisthebestlovelive").replace(/(rating:[qes])\w+/ig, "$1").toLowerCase().split(/,/); + var entries = (Utility.meta("blacklisted-tags") || "nozomiisthebestlovelive").replace(/(rating:\w)\w+/ig, "$1").toLowerCase().split(/,/); entries = entries.filter(e => e.trim() !== ""); entries.forEach(function(tags) { diff --git a/app/logical/autocomplete_service.rb b/app/logical/autocomplete_service.rb index 4ebb06603..d9e5358b3 100644 --- a/app/logical/autocomplete_service.rb +++ b/app/logical/autocomplete_service.rb @@ -16,7 +16,7 @@ class AutocompleteService status: %w[any] + POST_STATUSES, child: %w[any none] + POST_STATUSES, parent: %w[any none] + POST_STATUSES, - rating: %w[safe questionable explicit], + rating: Post::RATINGS.values.map(&:downcase), embedded: %w[true false], filetype: %w[jpg png gif swf zip webm mp4], commentary: %w[true false translated untranslated], diff --git a/app/logical/post_query_builder.rb b/app/logical/post_query_builder.rb index 2ff19fe9b..b7060b6c0 100644 --- a/app/logical/post_query_builder.rb +++ b/app/logical/post_query_builder.rb @@ -128,7 +128,7 @@ class PostQueryBuilder when "child" relation.child_matches(value) when "rating" - relation.where(rating: value.first.downcase) + relation.rating_matches(value) when "embedded" relation.embedded_matches(value) when "source" diff --git a/app/models/post.rb b/app/models/post.rb index 2ea366e10..3f3f2b7f2 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -10,6 +10,12 @@ class Post < ApplicationRecord RESTRICTED_TAGS_REGEX = /(?:^| )(?:#{Danbooru.config.restricted_tags.join("|")})(?:$| )/o + RATINGS = { + s: "Safe", + q: "Questionable", + e: "Explicit", + }.with_indifferent_access + deletable has_bit_flags %w[has_embedded_notes _unused_has_cropped is_taken_down] @@ -21,7 +27,7 @@ class Post < ApplicationRecord before_validation :remove_parent_loops validates :md5, uniqueness: { message: ->(post, _data) { "Duplicate of post ##{Post.find_by_md5(post.md5).id}" }}, on: :create validates :rating, presence: { message: "not selected" } - validates :rating, inclusion: { in: %w[s q e], message: "must be S, Q, or E" }, if: -> { rating.present? } + validates :rating, inclusion: { in: RATINGS.keys, message: "must be #{RATINGS.keys.map(&:upcase).to_sentence(last_word_connector: ", or ")}" }, if: -> { rating.present? } validates :source, length: { maximum: 1200 } validate :post_is_not_its_own_parent validate :uploader_is_not_limited, on: :create @@ -291,16 +297,7 @@ class Post < ApplicationRecord end def pretty_rating - case rating - when "q" - "Questionable" - - when "e" - "Explicit" - - when "s" - "Safe" - end + RATINGS.fetch(rating) end def parsed_source @@ -575,7 +572,7 @@ class Post < ApplicationRecord remove_parent_loops end - in "rating", /\A([qse])/i + in "rating", /\A([#{RATINGS.keys.join}])/i self.rating = $1.downcase in "source", "none" @@ -1114,6 +1111,10 @@ class Post < ApplicationRecord end end + def rating_matches(rating) + where(rating: rating.downcase.split(/,/).map(&:first)) + end + def source_matches(source, quoted = false) if source.empty? where(source: "") @@ -1516,7 +1517,7 @@ class Post < ApplicationRecord end def safeblocked? - CurrentUser.safe_mode? && (rating != "s" || Danbooru.config.safe_mode_restricted_tags.any? { |tag| tag.in?(tag_array) }) + CurrentUser.safe_mode? && (rating.in?(["q", "e"]) || Danbooru.config.safe_mode_restricted_tags.any? { |tag| tag.in?(tag_array) }) end def levelblocked?(user = CurrentUser.user) diff --git a/app/models/post_version.rb b/app/models/post_version.rb index e6259dad5..885cb40f0 100644 --- a/app/models/post_version.rb +++ b/app/models/post_version.rb @@ -151,16 +151,7 @@ class PostVersion < ApplicationRecord end def pretty_rating - case rating - when "q" - "Questionable" - - when "e" - "Explicit" - - when "s" - "Safe" - end + Post::RATINGS.fetch(rating) end def changes diff --git a/app/views/posts/partials/show/_edit.html.erb b/app/views/posts/partials/show/_edit.html.erb index 836cc64ce..a60ecf8c8 100644 --- a/app/views/posts/partials/show/_edit.html.erb +++ b/app/views/posts/partials/show/_edit.html.erb @@ -15,7 +15,7 @@ <%= f.input :old_source, as: :hidden, input_html: { value: post.source } %> <%= f.input :old_rating, as: :hidden, input_html: { value: post.rating } %> - <%= f.input :rating, collection: [["Explicit", "e"], ["Questionable", "q"], ["Safe", "s"]], as: :radio_buttons, boolean_style: :inline %> + <%= f.input :rating, collection: Post::RATINGS.invert.reverse_each.to_h, as: :radio_buttons, boolean_style: :inline %>
diff --git a/app/views/uploads/_single_asset_upload.html.erb b/app/views/uploads/_single_asset_upload.html.erb index 17c63b25a..b6633f953 100644 --- a/app/views/uploads/_single_asset_upload.html.erb +++ b/app/views/uploads/_single_asset_upload.html.erb @@ -46,7 +46,7 @@ <%= hidden_field_tag :upload_media_asset_id, upload_media_asset.id %> <%= f.input :source, as: :string, input_html: { value: post.source } %> - <%= f.input :rating, collection: [["Explicit", "e"], ["Questionable", "q"], ["Safe", "s"]], as: :radio_buttons, selected: post.rating %> + <%= f.input :rating, collection: Post::RATINGS.invert.reverse_each.to_h, as: :radio_buttons, selected: post.rating %> <%= f.input :parent_id, label: "Parent ID", as: :string, input_html: { value: post.parent_id } %>
diff --git a/test/unit/post_query_builder_test.rb b/test/unit/post_query_builder_test.rb index 44daea01f..2d488e5f9 100644 --- a/test/unit/post_query_builder_test.rb +++ b/test/unit/post_query_builder_test.rb @@ -950,6 +950,7 @@ class PostQueryBuilderTest < ActiveSupport::TestCase assert_tag_match([s], "rating:s") assert_tag_match([q], "rating:q") assert_tag_match([e], "rating:e") + assert_tag_match([e, q], "rating:q,e") assert_tag_match([], "rating:s rating:q") assert_tag_match(all - [s], "-rating:s")