String: add truthy? & falsy? core extensions.

* Add `truthy?` and `falsy?` core extensions to String.

* Use `truthy?` and `falsy?` to replace ad-hoc parsing of boolean
  parameters in various places.
This commit is contained in:
evazion
2018-05-03 18:53:35 -05:00
parent 96669ca4b5
commit c7492343ce
15 changed files with 62 additions and 102 deletions

View File

@@ -75,10 +75,6 @@ class TagAliasRequest
end end
def skip_secondary_validations=(v) def skip_secondary_validations=(v)
if v == "1" or v == true or v =~ /t/ @skip_secondary_validations = v.to_s.truthy?
@skip_secondary_validations = true
else
@skip_secondary_validations = false
end
end end
end end

View File

@@ -75,10 +75,6 @@ class TagImplicationRequest
end end
def skip_secondary_validations=(v) def skip_secondary_validations=(v)
if v == "1" or v == true or v =~ /t/ @skip_secondary_validations = v.to_s.truthy?
@skip_secondary_validations = true
else
@skip_secondary_validations = false
end
end end
end end

View File

@@ -587,13 +587,13 @@ class Artist < ApplicationRecord
end end
# XXX deprecated, remove at some point. # XXX deprecated, remove at some point.
if params[:empty_only] == "true" if params[:empty_only].to_s.truthy?
params[:has_tag] = "false" params[:has_tag] = "false"
end end
if params[:has_tag] == "true" if params[:has_tag].to_s.truthy?
q = q.joins(:tag).where("tags.post_count > 0") q = q.joins(:tag).where("tags.post_count > 0")
elsif params[:has_tag] == "false" elsif params[:has_tag].to_s.falsy?
q = q.includes(:tag).where("tags.name IS NULL OR tags.post_count <= 0").references(:tags) q = q.includes(:tag).where("tags.name IS NULL OR tags.post_count <= 0").references(:tags)
end end

View File

@@ -41,15 +41,15 @@ class ArtistCommentary < ApplicationRecord
q = q.where(post_id: params[:post_id].split(",").map(&:to_i)) q = q.where(post_id: params[:post_id].split(",").map(&:to_i))
end end
if params[:original_present] == "yes" if params[:original_present].to_s.truthy?
q = q.where("(original_title != '') or (original_description != '')") q = q.where("(original_title != '') or (original_description != '')")
elsif params[:original_present] == "no" elsif params[:original_present].to_s.falsy?
q = q.where("(original_title = '') and (original_description = '')") q = q.where("(original_title = '') and (original_description = '')")
end end
if params[:translated_present] == "yes" if params[:translated_present].to_s.truthy?
q = q.where("(translated_title != '') or (translated_description != '')") q = q.where("(translated_title != '') or (translated_description != '')")
elsif params[:translated_present] == "no" elsif params[:translated_present].to_s.falsy?
q = q.where("(translated_title = '') and (translated_description = '')") q = q.where("(translated_title = '') and (translated_description = '')")
end end
@@ -84,37 +84,17 @@ class ArtistCommentary < ApplicationRecord
end end
def tag_post def tag_post
if remove_commentary_tag == "1" post.remove_tag("commentary") if remove_commentary_tag.to_s.truthy?
post.remove_tag("commentary") post.add_tag("commentary") if add_commentary_tag.to_s.truthy?
end
if add_commentary_tag == "1" post.remove_tag("commentary_request") if remove_commentary_request_tag.to_s.truthy?
post.add_tag("commentary") post.add_tag("commentary_request") if add_commentary_request_tag.to_s.truthy?
end
if remove_commentary_request_tag == "1" post.remove_tag("check_commentary") if remove_commentary_check_tag.to_s.truthy?
post.remove_tag("commentary_request") post.add_tag("check_commentary") if add_commentary_check_tag.to_s.truthy?
end
if add_commentary_request_tag == "1" post.remove_tag("partial_commentary") if remove_partial_commentary_tag.to_s.truthy?
post.add_tag("commentary_request") post.add_tag("partial_commentary") if add_partial_commentary_tag.to_s.truthy?
end
if remove_commentary_check_tag == "1"
post.remove_tag("check_commentary")
end
if add_commentary_check_tag == "1"
post.add_tag("check_commentary")
end
if remove_partial_commentary_tag == "1"
post.remove_tag("partial_commentary")
end
if add_partial_commentary_tag == "1"
post.add_tag("partial_commentary")
end
post.save if post.tag_string_changed? post.save if post.tag_string_changed?
end end

View File

@@ -47,10 +47,8 @@ class Ban < ApplicationRecord
q = q.reason_matches(params[:reason_matches]) q = q.reason_matches(params[:reason_matches])
end end
case params[:expired] q = q.expired if params[:expired].to_s.truthy?
when "true" then q = q.expired q = q.unexpired if params[:expired].to_s.falsy?
when "false" then q = q.unexpired
end
case params[:order] case params[:order]
when "expires_at_desc" when "expires_at_desc"

View File

@@ -205,10 +205,6 @@ class BulkUpdateRequest < ApplicationRecord
end end
def skip_secondary_validations=(v) def skip_secondary_validations=(v)
if v == "1" or v == true @skip_secondary_validations = v.to_s.truthy?
@skip_secondary_validations = true
else
@skip_secondary_validations = false
end
end end
end end

View File

@@ -171,6 +171,10 @@ class Dmail < ApplicationRecord
end end
end end
def read
where(is_read: true)
end
def unread def unread
where("is_read = false and is_deleted = false") where("is_read = false and is_deleted = false")
end end
@@ -220,11 +224,8 @@ class Dmail < ApplicationRecord
q = q.where("is_spam = ?", false) q = q.where("is_spam = ?", false)
end end
if params[:read] == "true" q = q.read if params[:read].to_s.truthy?
q = q.where("is_read = true") q = q.unread if params[:read].to_s.falsy?
elsif params[:read] == "false"
q = q.unread
end
q.apply_default_order(params) q.apply_default_order(params)
end end

View File

@@ -1820,28 +1820,20 @@ class Post < ApplicationRecord
end end
def mark_as_translated(params) def mark_as_translated(params)
tags = self.tag_array.dup add_tag("check_translation") if params["check_translation"].to_s.truthy?
remove_tag("check_translation") if params["check_translation"].to_s.falsy?
if params["check_translation"] == "1" add_tag("partially_translated") if params["partially_translated"].to_s.truthy?
tags << "check_translation" remove_tag("partially_translated") if params["partially_translated"].to_s.falsy?
elsif params["check_translation"] == "0"
tags -= ["check_translation"]
end
if params["partially_translated"] == "1"
tags << "partially_translated"
elsif params["partially_translated"] == "0"
tags -= ["partially_translated"]
end
if params["check_translation"] == "1" || params["partially_translated"] == "1" if has_tag?("check_translation") || has_tag?("partially_translated")
tags << "translation_request" add_tag("translation_request")
tags -= ["translated"] remove_tag("translated")
else else
tags << "translated" add_tag("translated")
tags -= ["translation_request"] remove_tag("translation_request")
end end
self.tag_string = tags.join(" ")
save save
end end

View File

@@ -65,11 +65,8 @@ class PostAppeal < ApplicationRecord
q = q.post_tags_match(params[:post_tags_match]) q = q.post_tags_match(params[:post_tags_match])
end end
if params[:is_resolved] == "true" q = q.resolved if params[:is_resolved].to_s.truthy?
q = q.resolved q = q.unresolved if params[:is_resolved].to_s.falsy?
elsif params[:is_resolved] == "false"
q = q.unresolved
end
q.apply_default_order(params) q.apply_default_order(params)
end end

View File

@@ -130,10 +130,6 @@ class SavedSearch < ApplicationRecord
end end
def disable_labels=(value) def disable_labels=(value)
CurrentUser.update(disable_categorized_saved_searches: true) if value == "1" CurrentUser.update(disable_categorized_saved_searches: true) if value.to_s.truthy?
end
def disable_labels=(value)
CurrentUser.update(disable_categorized_saved_searches: true) if value == "1"
end end
end end

View File

@@ -887,19 +887,19 @@ class Tag < ApplicationRecord
q = q.where("category = ?", params[:category]) q = q.where("category = ?", params[:category])
end end
if params[:hide_empty].blank? || params[:hide_empty] != "no" if params[:hide_empty].blank? || params[:hide_empty].to_s.truthy?
q = q.where("post_count > 0") q = q.where("post_count > 0")
end end
if params[:has_wiki] == "yes" if params[:has_wiki].to_s.truthy?
q = q.joins(:wiki_page).where("wiki_pages.is_deleted = false") q = q.joins(:wiki_page).where("wiki_pages.is_deleted = false")
elsif params[:has_wiki] == "no" elsif params[:has_wiki].to_s.falsy?
q = q.joins("LEFT JOIN wiki_pages ON tags.name = wiki_pages.title").where("wiki_pages.title IS NULL OR wiki_pages.is_deleted = true") q = q.joins("LEFT JOIN wiki_pages ON tags.name = wiki_pages.title").where("wiki_pages.title IS NULL OR wiki_pages.is_deleted = true")
end end
if params[:has_artist] == "yes" if params[:has_artist].to_s.truthy?
q = q.joins("INNER JOIN artists ON tags.name = artists.name").where("artists.is_active = true") q = q.joins("INNER JOIN artists ON tags.name = artists.name").where("artists.is_active = true")
elsif params[:has_artist] == "no" elsif params[:has_artist].to_s.falsy?
q = q.joins("LEFT JOIN artists ON tags.name = artists.name").where("artists.name IS NULL OR artists.is_active = false") q = q.joins("LEFT JOIN artists ON tags.name = artists.name").where("artists.name IS NULL OR artists.is_active = false")
end end

View File

@@ -426,9 +426,9 @@ class Upload < ApplicationRecord
q = q.attribute_matches(:post_id, params[:post_id]) q = q.attribute_matches(:post_id, params[:post_id])
end end
if params[:has_post] == "yes" if params[:has_post].to_s.truthy?
q = q.where.not(post_id: nil) q = q.where.not(post_id: nil)
elsif params[:has_post] == "no" elsif params[:has_post].to_s.falsy?
q = q.where(post_id: nil) q = q.where(post_id: nil)
end end
@@ -490,10 +490,10 @@ class Upload < ApplicationRecord
end end
def upload_as_pending? def upload_as_pending?
as_pending == "1" as_pending.to_s.truthy?
end end
def include_artist_commentary? def include_artist_commentary?
include_artist_commentary == "1" include_artist_commentary.to_s.truthy?
end end
end end

View File

@@ -843,10 +843,10 @@ class User < ApplicationRecord
[:can_approve_posts, :can_upload_free, :is_super_voter].each do |x| [:can_approve_posts, :can_upload_free, :is_super_voter].each do |x|
if params[x].present? if params[x].present?
attr_idx = BOOLEAN_ATTRIBUTES.index(x.to_s) attr_idx = BOOLEAN_ATTRIBUTES.index(x.to_s)
if params[x] == "true" if params[x].to_s.truthy?
bitprefs_include ||= "0"*bitprefs_length bitprefs_include ||= "0"*bitprefs_length
bitprefs_include[attr_idx] = '1' bitprefs_include[attr_idx] = '1'
elsif params[x] == "false" elsif params[x].to_s.falsy?
bitprefs_exclude ||= "0"*bitprefs_length bitprefs_exclude ||= "0"*bitprefs_length
bitprefs_exclude[attr_idx] = '1' bitprefs_exclude[attr_idx] = '1'
end end
@@ -865,7 +865,7 @@ class User < ApplicationRecord
{:len => bitprefs_length, :bits => bitprefs_exclude}) {:len => bitprefs_length, :bits => bitprefs_exclude})
end end
if params[:current_user_first] == "true" && !CurrentUser.is_anonymous? if params[:current_user_first].to_s.truthy? && !CurrentUser.is_anonymous?
q = q.order("id = #{CurrentUser.user.id.to_i} desc") q = q.order("id = #{CurrentUser.user.id.to_i} desc")
end end

View File

@@ -82,13 +82,13 @@ class WikiPage < ApplicationRecord
q = q.where("creator_id = (select _.id from users _ where lower(_.name) = ?)", params[:creator_name].tr(" ", "_").mb_chars.downcase) q = q.where("creator_id = (select _.id from users _ where lower(_.name) = ?)", params[:creator_name].tr(" ", "_").mb_chars.downcase)
end end
if params[:hide_deleted] =~ /y/i if params[:hide_deleted].to_s.truthy?
q = q.where("is_deleted = false") q = q.where("is_deleted = false")
end end
if params[:other_names_present] == "yes" if params[:other_names_present].to_s.truthy?
q = q.where("other_names is not null and other_names != ''") q = q.where("other_names is not null and other_names != ''")
elsif params[:other_names_present] == "no" elsif params[:other_names_present].to_s.falsy?
q = q.where("other_names is null or other_names = ''") q = q.where("other_names is null or other_names = ''")
end end
@@ -165,7 +165,7 @@ class WikiPage < ApplicationRecord
end end
def skip_secondary_validations=(value) def skip_secondary_validations=(value)
@skip_secondary_validations = (value == true || value == "1") @skip_secondary_validations = value.to_s.truthy?
end end
def category_name def category_name

View File

@@ -23,6 +23,14 @@ module Danbooru
def to_escaped_for_tsquery def to_escaped_for_tsquery
"'#{gsub(/\0/, '').gsub(/'/, '\0\0').gsub(/\\/, '\0\0\0\0')}'" "'#{gsub(/\0/, '').gsub(/'/, '\0\0').gsub(/\\/, '\0\0\0\0')}'"
end end
def truthy?
self.match?(/\A(true|t|yes|y|on|1)\z/i)
end
def falsy?
self.match?(/\A(false|f|no|n|off|0)\z/i)
end
end end
end end
end end