models: drop unnecessary presence validations.

In rails 5, belongs_to associations automatically validate that the
associated item is present, meaning that we don't need to validate these
things manually any more.
This commit is contained in:
evazion
2019-08-04 14:40:37 -05:00
parent 7c8c4e9f82
commit a926b162be
16 changed files with 11 additions and 36 deletions

View File

@@ -7,7 +7,7 @@ class Ban < ApplicationRecord
belongs_to :user
belongs_to :banner, :class_name => "User"
validate :user_is_inferior
validates_presence_of :user_id, :reason, :duration
validates_presence_of :reason, :duration
before_validation :initialize_banner_id, :on => :create
scope :unexpired, -> { where("bans.expires_at > ?", Time.now) }

View File

@@ -6,7 +6,6 @@ class BulkUpdateRequest < ApplicationRecord
belongs_to :forum_post, optional: true
belongs_to :approver, optional: true, class_name: "User"
validates_presence_of :user
validates_presence_of :script
validates_presence_of :title, if: ->(rec) {rec.forum_topic_id.blank?}
validates_inclusion_of :status, :in => %w(pending approved rejected)

View File

@@ -136,10 +136,6 @@ class Comment < ApplicationRecord
extend SearchMethods
include VoteMethods
def validate_post_exists
errors.add(:post, "must exist") unless Post.exists?(post_id)
end
def validate_creator_is_not_limited
if creator.is_comment_limited? && !do_not_bump_post?
errors.add(:base, "You can only post #{Danbooru.config.member_comment_limit} comments per hour")

View File

@@ -4,7 +4,7 @@ class CommentVote < ApplicationRecord
belongs_to :comment
belongs_to :user
before_validation :initialize_user, :on => :create
validates_presence_of :user_id, :comment_id, :score
validates_presence_of :score
validates_uniqueness_of :user_id, :scope => :comment_id, :message => "have already voted for this comment"
validate :validate_user_can_vote
validate :validate_comment_can_be_down_voted

View File

@@ -2,7 +2,6 @@ class DmailFilter < ApplicationRecord
extend Memoist
belongs_to :user
validates_presence_of :user
before_validation :initialize_user
def initialize_user

View File

@@ -13,9 +13,8 @@ class ForumPost < ApplicationRecord
after_create :update_topic_updated_at_on_create
after_update :update_topic_updated_at_on_update_for_original_posts
after_destroy :update_topic_updated_at_on_destroy
validates_presence_of :body, :creator_id
validates_presence_of :body
validate :validate_topic_is_unlocked
validate :topic_id_not_invalid
validate :topic_is_not_restricted, :on => :create
before_destroy :validate_topic_is_unlocked
after_save :delete_topic_if_original_post
@@ -144,13 +143,6 @@ class ForumPost < ApplicationRecord
end
end
def topic_id_not_invalid
if topic_id && !topic
errors[:base] << "Topic ID is invalid"
return false
end
end
def topic_is_not_restricted
if topic && !topic.visible?(creator)
errors[:topic] << "is restricted"

View File

@@ -17,7 +17,7 @@ class ForumTopic < ApplicationRecord
has_one :original_post, -> {order("forum_posts.id asc")}, class_name: "ForumPost", foreign_key: "topic_id", inverse_of: :topic
has_many :subscriptions, :class_name => "ForumSubscription"
before_validation :initialize_is_deleted, :on => :create
validates_presence_of :title, :creator_id
validates_presence_of :title
validates_associated :original_post
validates_inclusion_of :category_id, :in => CATEGORIES.keys
validates_inclusion_of :min_level, :in => MIN_LEVELS.values

View File

@@ -1,7 +1,7 @@
class IpBan < ApplicationRecord
IP_ADDR_REGEX = /\A(?:[0-9]{1,3}\.){3}[0-9]{1,3}\Z/
belongs_to_creator
validates_presence_of :reason, :creator, :ip_addr
validates_presence_of :reason, :ip_addr
validates_format_of :ip_addr, :with => IP_ADDR_REGEX
validates_uniqueness_of :ip_addr, :if => ->(rec) {rec.ip_addr =~ IP_ADDR_REGEX}
after_create do |rec|

View File

@@ -2,7 +2,6 @@ class JanitorTrial < ApplicationRecord
belongs_to :user
after_create :send_dmail
after_create :promote_user
validates_presence_of :user
belongs_to_creator
validates_inclusion_of :status, :in => %w(active inactive)
before_validation :initialize_status

View File

@@ -1,7 +1,6 @@
class ModAction < ApplicationRecord
belongs_to :creator, :class_name => "User"
before_validation :initialize_creator, :on => :create
validates_presence_of :creator_id
#####DIVISIONS#####
#Groups: 0-999

View File

@@ -5,8 +5,7 @@ class Note < ApplicationRecord
belongs_to :post
belongs_to_creator
has_many :versions, -> {order("note_versions.id ASC")}, :class_name => "NoteVersion", :dependent => :destroy
validates_presence_of :creator_id, :x, :y, :width, :height, :body
validate :post_must_exist
validates_presence_of :x, :y, :width, :height, :body
validate :note_within_image
after_save :update_post
after_save :create_version
@@ -68,13 +67,6 @@ class Note < ApplicationRecord
extend SearchMethods
include ApiMethods
def post_must_exist
if !Post.exists?(post_id)
errors.add :post, "must exist"
return false
end
end
def post_must_not_be_note_locked
if is_locked?
errors.add :post, "is note locked"

View File

@@ -3,7 +3,7 @@ class PostAppeal < ApplicationRecord
belongs_to :creator, :class_name => "User"
belongs_to :post
validates_presence_of :post, :reason, :creator_id, :creator_ip_addr
validates_presence_of :reason, :creator_ip_addr
validate :validate_post_is_inactive
validate :validate_creator_is_not_limited
before_validation :initialize_creator, :on => :create

View File

@@ -6,7 +6,7 @@ class PostVote < ApplicationRecord
attr_accessor :vote
after_initialize :initialize_attributes, if: :new_record?
validates_presence_of :post_id, :user_id, :score
validates_presence_of :score
validates_inclusion_of :score, :in => [SuperVoter::MAGNITUDE, 1, -1, -SuperVoter::MAGNITUDE]
after_create :update_post_on_create
after_destroy :update_post_on_destroy

View File

@@ -27,8 +27,7 @@ class TagRelationship < ApplicationRecord
before_validation :initialize_creator, :on => :create
before_validation :normalize_names
validates_format_of :status, :with => /\A(active|deleted|pending|processing|queued|retired|error: .*)\Z/
validates_presence_of :creator_id, :antecedent_name, :consequent_name
validates :creator, presence: { message: "must exist" }, if: -> { creator_id.present? }
validates_presence_of :antecedent_name, :consequent_name
validates :approver, presence: { message: "must exist" }, if: -> { approver_id.present? }
validates :forum_topic, presence: { message: "must exist" }, if: -> { forum_topic_id.present? }
validate :antecedent_and_consequent_are_different

View File

@@ -3,7 +3,7 @@ class UserFeedback < ApplicationRecord
belongs_to :user
belongs_to_creator
attr_accessor :disable_dmail_notification
validates_presence_of :user, :creator, :body, :category
validates_presence_of :body, :category
validates_inclusion_of :category, :in => %w(positive negative neutral)
validate :creator_is_gold
validate :user_is_not_creator

View File

@@ -1,6 +1,6 @@
class UserNameChangeRequest < ApplicationRecord
after_initialize :initialize_attributes, if: :new_record?
validates_presence_of :user_id, :original_name, :desired_name
validates_presence_of :original_name, :desired_name
validates_inclusion_of :status, :in => %w(pending approved rejected)
belongs_to :user
belongs_to :approver, :class_name => "User", optional: true