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`.
This commit is contained in:
evazion
2022-05-18 12:54:43 -05:00
parent af8ef8b277
commit 141044d352
8 changed files with 21 additions and 28 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -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 %>
<fieldset class="inline-fieldset post_has_embedded_notes_fieldset">
<label>Notes</label>

View File

@@ -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 } %>
<div class="input upload_artist_commentary_container">