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:
@@ -7,7 +7,7 @@ class Ban < ApplicationRecord
|
|||||||
belongs_to :user
|
belongs_to :user
|
||||||
belongs_to :banner, :class_name => "User"
|
belongs_to :banner, :class_name => "User"
|
||||||
validate :user_is_inferior
|
validate :user_is_inferior
|
||||||
validates_presence_of :user_id, :reason, :duration
|
validates_presence_of :reason, :duration
|
||||||
before_validation :initialize_banner_id, :on => :create
|
before_validation :initialize_banner_id, :on => :create
|
||||||
|
|
||||||
scope :unexpired, -> { where("bans.expires_at > ?", Time.now) }
|
scope :unexpired, -> { where("bans.expires_at > ?", Time.now) }
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ class BulkUpdateRequest < ApplicationRecord
|
|||||||
belongs_to :forum_post, optional: true
|
belongs_to :forum_post, optional: true
|
||||||
belongs_to :approver, optional: true, class_name: "User"
|
belongs_to :approver, optional: true, class_name: "User"
|
||||||
|
|
||||||
validates_presence_of :user
|
|
||||||
validates_presence_of :script
|
validates_presence_of :script
|
||||||
validates_presence_of :title, if: ->(rec) {rec.forum_topic_id.blank?}
|
validates_presence_of :title, if: ->(rec) {rec.forum_topic_id.blank?}
|
||||||
validates_inclusion_of :status, :in => %w(pending approved rejected)
|
validates_inclusion_of :status, :in => %w(pending approved rejected)
|
||||||
|
|||||||
@@ -136,10 +136,6 @@ class Comment < ApplicationRecord
|
|||||||
extend SearchMethods
|
extend SearchMethods
|
||||||
include VoteMethods
|
include VoteMethods
|
||||||
|
|
||||||
def validate_post_exists
|
|
||||||
errors.add(:post, "must exist") unless Post.exists?(post_id)
|
|
||||||
end
|
|
||||||
|
|
||||||
def validate_creator_is_not_limited
|
def validate_creator_is_not_limited
|
||||||
if creator.is_comment_limited? && !do_not_bump_post?
|
if creator.is_comment_limited? && !do_not_bump_post?
|
||||||
errors.add(:base, "You can only post #{Danbooru.config.member_comment_limit} comments per hour")
|
errors.add(:base, "You can only post #{Danbooru.config.member_comment_limit} comments per hour")
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ class CommentVote < ApplicationRecord
|
|||||||
belongs_to :comment
|
belongs_to :comment
|
||||||
belongs_to :user
|
belongs_to :user
|
||||||
before_validation :initialize_user, :on => :create
|
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"
|
validates_uniqueness_of :user_id, :scope => :comment_id, :message => "have already voted for this comment"
|
||||||
validate :validate_user_can_vote
|
validate :validate_user_can_vote
|
||||||
validate :validate_comment_can_be_down_voted
|
validate :validate_comment_can_be_down_voted
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ class DmailFilter < ApplicationRecord
|
|||||||
extend Memoist
|
extend Memoist
|
||||||
|
|
||||||
belongs_to :user
|
belongs_to :user
|
||||||
validates_presence_of :user
|
|
||||||
before_validation :initialize_user
|
before_validation :initialize_user
|
||||||
|
|
||||||
def initialize_user
|
def initialize_user
|
||||||
|
|||||||
@@ -13,9 +13,8 @@ class ForumPost < ApplicationRecord
|
|||||||
after_create :update_topic_updated_at_on_create
|
after_create :update_topic_updated_at_on_create
|
||||||
after_update :update_topic_updated_at_on_update_for_original_posts
|
after_update :update_topic_updated_at_on_update_for_original_posts
|
||||||
after_destroy :update_topic_updated_at_on_destroy
|
after_destroy :update_topic_updated_at_on_destroy
|
||||||
validates_presence_of :body, :creator_id
|
validates_presence_of :body
|
||||||
validate :validate_topic_is_unlocked
|
validate :validate_topic_is_unlocked
|
||||||
validate :topic_id_not_invalid
|
|
||||||
validate :topic_is_not_restricted, :on => :create
|
validate :topic_is_not_restricted, :on => :create
|
||||||
before_destroy :validate_topic_is_unlocked
|
before_destroy :validate_topic_is_unlocked
|
||||||
after_save :delete_topic_if_original_post
|
after_save :delete_topic_if_original_post
|
||||||
@@ -144,13 +143,6 @@ class ForumPost < ApplicationRecord
|
|||||||
end
|
end
|
||||||
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
|
def topic_is_not_restricted
|
||||||
if topic && !topic.visible?(creator)
|
if topic && !topic.visible?(creator)
|
||||||
errors[:topic] << "is restricted"
|
errors[:topic] << "is restricted"
|
||||||
|
|||||||
@@ -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_one :original_post, -> {order("forum_posts.id asc")}, class_name: "ForumPost", foreign_key: "topic_id", inverse_of: :topic
|
||||||
has_many :subscriptions, :class_name => "ForumSubscription"
|
has_many :subscriptions, :class_name => "ForumSubscription"
|
||||||
before_validation :initialize_is_deleted, :on => :create
|
before_validation :initialize_is_deleted, :on => :create
|
||||||
validates_presence_of :title, :creator_id
|
validates_presence_of :title
|
||||||
validates_associated :original_post
|
validates_associated :original_post
|
||||||
validates_inclusion_of :category_id, :in => CATEGORIES.keys
|
validates_inclusion_of :category_id, :in => CATEGORIES.keys
|
||||||
validates_inclusion_of :min_level, :in => MIN_LEVELS.values
|
validates_inclusion_of :min_level, :in => MIN_LEVELS.values
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
class IpBan < ApplicationRecord
|
class IpBan < ApplicationRecord
|
||||||
IP_ADDR_REGEX = /\A(?:[0-9]{1,3}\.){3}[0-9]{1,3}\Z/
|
IP_ADDR_REGEX = /\A(?:[0-9]{1,3}\.){3}[0-9]{1,3}\Z/
|
||||||
belongs_to_creator
|
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_format_of :ip_addr, :with => IP_ADDR_REGEX
|
||||||
validates_uniqueness_of :ip_addr, :if => ->(rec) {rec.ip_addr =~ IP_ADDR_REGEX}
|
validates_uniqueness_of :ip_addr, :if => ->(rec) {rec.ip_addr =~ IP_ADDR_REGEX}
|
||||||
after_create do |rec|
|
after_create do |rec|
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ class JanitorTrial < ApplicationRecord
|
|||||||
belongs_to :user
|
belongs_to :user
|
||||||
after_create :send_dmail
|
after_create :send_dmail
|
||||||
after_create :promote_user
|
after_create :promote_user
|
||||||
validates_presence_of :user
|
|
||||||
belongs_to_creator
|
belongs_to_creator
|
||||||
validates_inclusion_of :status, :in => %w(active inactive)
|
validates_inclusion_of :status, :in => %w(active inactive)
|
||||||
before_validation :initialize_status
|
before_validation :initialize_status
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
class ModAction < ApplicationRecord
|
class ModAction < ApplicationRecord
|
||||||
belongs_to :creator, :class_name => "User"
|
belongs_to :creator, :class_name => "User"
|
||||||
before_validation :initialize_creator, :on => :create
|
before_validation :initialize_creator, :on => :create
|
||||||
validates_presence_of :creator_id
|
|
||||||
|
|
||||||
#####DIVISIONS#####
|
#####DIVISIONS#####
|
||||||
#Groups: 0-999
|
#Groups: 0-999
|
||||||
|
|||||||
@@ -5,8 +5,7 @@ class Note < ApplicationRecord
|
|||||||
belongs_to :post
|
belongs_to :post
|
||||||
belongs_to_creator
|
belongs_to_creator
|
||||||
has_many :versions, -> {order("note_versions.id ASC")}, :class_name => "NoteVersion", :dependent => :destroy
|
has_many :versions, -> {order("note_versions.id ASC")}, :class_name => "NoteVersion", :dependent => :destroy
|
||||||
validates_presence_of :creator_id, :x, :y, :width, :height, :body
|
validates_presence_of :x, :y, :width, :height, :body
|
||||||
validate :post_must_exist
|
|
||||||
validate :note_within_image
|
validate :note_within_image
|
||||||
after_save :update_post
|
after_save :update_post
|
||||||
after_save :create_version
|
after_save :create_version
|
||||||
@@ -68,13 +67,6 @@ class Note < ApplicationRecord
|
|||||||
extend SearchMethods
|
extend SearchMethods
|
||||||
include ApiMethods
|
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
|
def post_must_not_be_note_locked
|
||||||
if is_locked?
|
if is_locked?
|
||||||
errors.add :post, "is note locked"
|
errors.add :post, "is note locked"
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ class PostAppeal < ApplicationRecord
|
|||||||
|
|
||||||
belongs_to :creator, :class_name => "User"
|
belongs_to :creator, :class_name => "User"
|
||||||
belongs_to :post
|
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_post_is_inactive
|
||||||
validate :validate_creator_is_not_limited
|
validate :validate_creator_is_not_limited
|
||||||
before_validation :initialize_creator, :on => :create
|
before_validation :initialize_creator, :on => :create
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ class PostVote < ApplicationRecord
|
|||||||
attr_accessor :vote
|
attr_accessor :vote
|
||||||
|
|
||||||
after_initialize :initialize_attributes, if: :new_record?
|
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]
|
validates_inclusion_of :score, :in => [SuperVoter::MAGNITUDE, 1, -1, -SuperVoter::MAGNITUDE]
|
||||||
after_create :update_post_on_create
|
after_create :update_post_on_create
|
||||||
after_destroy :update_post_on_destroy
|
after_destroy :update_post_on_destroy
|
||||||
|
|||||||
@@ -27,8 +27,7 @@ class TagRelationship < ApplicationRecord
|
|||||||
before_validation :initialize_creator, :on => :create
|
before_validation :initialize_creator, :on => :create
|
||||||
before_validation :normalize_names
|
before_validation :normalize_names
|
||||||
validates_format_of :status, :with => /\A(active|deleted|pending|processing|queued|retired|error: .*)\Z/
|
validates_format_of :status, :with => /\A(active|deleted|pending|processing|queued|retired|error: .*)\Z/
|
||||||
validates_presence_of :creator_id, :antecedent_name, :consequent_name
|
validates_presence_of :antecedent_name, :consequent_name
|
||||||
validates :creator, presence: { message: "must exist" }, if: -> { creator_id.present? }
|
|
||||||
validates :approver, presence: { message: "must exist" }, if: -> { approver_id.present? }
|
validates :approver, presence: { message: "must exist" }, if: -> { approver_id.present? }
|
||||||
validates :forum_topic, presence: { message: "must exist" }, if: -> { forum_topic_id.present? }
|
validates :forum_topic, presence: { message: "must exist" }, if: -> { forum_topic_id.present? }
|
||||||
validate :antecedent_and_consequent_are_different
|
validate :antecedent_and_consequent_are_different
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ class UserFeedback < ApplicationRecord
|
|||||||
belongs_to :user
|
belongs_to :user
|
||||||
belongs_to_creator
|
belongs_to_creator
|
||||||
attr_accessor :disable_dmail_notification
|
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)
|
validates_inclusion_of :category, :in => %w(positive negative neutral)
|
||||||
validate :creator_is_gold
|
validate :creator_is_gold
|
||||||
validate :user_is_not_creator
|
validate :user_is_not_creator
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
class UserNameChangeRequest < ApplicationRecord
|
class UserNameChangeRequest < ApplicationRecord
|
||||||
after_initialize :initialize_attributes, if: :new_record?
|
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)
|
validates_inclusion_of :status, :in => %w(pending approved rejected)
|
||||||
belongs_to :user
|
belongs_to :user
|
||||||
belongs_to :approver, :class_name => "User", optional: true
|
belongs_to :approver, :class_name => "User", optional: true
|
||||||
|
|||||||
Reference in New Issue
Block a user