Skip to content

Commit

Permalink
Fix Rails/RedundantPresenceValidationOnBelongsTo cop
Browse files Browse the repository at this point in the history
  • Loading branch information
danidoni committed Dec 29, 2021
1 parent 2881477 commit d2cf533
Show file tree
Hide file tree
Showing 19 changed files with 8 additions and 35 deletions.
5 changes: 2 additions & 3 deletions src/api/app/models/attrib.rb
Expand Up @@ -21,11 +21,10 @@ class Attrib < ApplicationRecord
#### Validations macros
validates_associated :values
validates_associated :issues
validates :attrib_type, presence: true
# Either we belong to a project or to a package
validates :package, presence: true, if: proc { |attrib| attrib.project_id.nil? }
validates :package, if: proc { |attrib| attrib.project_id.nil? }
validates :package_id, absence: { message: "can't also be present" }, if: proc { |attrib| attrib.project_id.present? }
validates :project, presence: true, if: proc { |attrib| attrib.package_id.nil? }
validates :project, if: proc { |attrib| attrib.package_id.nil? }

validate :validate_value_count,
:validate_issues,
Expand Down
1 change: 0 additions & 1 deletion src/api/app/models/commit_activity.rb
@@ -1,7 +1,6 @@
class CommitActivity < ApplicationRecord
belongs_to :user

validates :user, :date, :project, :package, :count, presence: true

validates :count, numericality: { more_than_or_equal_to: 1,
only_integer: true }
Expand Down
1 change: 0 additions & 1 deletion src/api/app/models/download_repository.rb
Expand Up @@ -3,7 +3,6 @@ class DownloadRepository < ApplicationRecord

belongs_to :repository

validates :repository, presence: true
validates :arch, uniqueness: { scope: :repository_id, case_sensitive: false }, presence: true
validate :architecture_inclusion
validates :url, presence: true, format: { with: /\A[a-zA-Z]+:.*\Z/ } # from backend/BSVerify.pm
Expand Down
2 changes: 0 additions & 2 deletions src/api/app/models/group_maintainer.rb
Expand Up @@ -2,8 +2,6 @@ class GroupMaintainer < ApplicationRecord
belongs_to :user
belongs_to :group

validates :user, presence: true
validates :group, presence: true
validate :validate_duplicates, on: :create

private
Expand Down
2 changes: 0 additions & 2 deletions src/api/app/models/groups_user.rb
Expand Up @@ -4,8 +4,6 @@ class GroupsUser < ApplicationRecord
belongs_to :user
belongs_to :group

validates :user, presence: true
validates :group, presence: true
validate :validate_duplicates, on: :create
validates_with AllowedUserValidator

Expand Down
1 change: 0 additions & 1 deletion src/api/app/models/kiwi/profile.rb
Expand Up @@ -19,7 +19,6 @@ class Profile < ApplicationRecord
#### Validations macros
validates :name, presence: true
validates :description, presence: true
validates :image, presence: true
validates :selected, inclusion: { in: [true, false] }
validates :name, uniqueness: {
scope: :image,
Expand Down
2 changes: 0 additions & 2 deletions src/api/app/models/notified_project.rb
Expand Up @@ -2,8 +2,6 @@ class NotifiedProject < ApplicationRecord
belongs_to :notification
belongs_to :project

validates :notification, presence: true
validates :project, presence: true

validates :notification_id, uniqueness: { scope: :project_id, message: 'These notification and project are already associated' }
end
Expand Down
1 change: 0 additions & 1 deletion src/api/app/models/path_element.rb
Expand Up @@ -6,7 +6,6 @@ class PathElement < ApplicationRecord
# FIXME: This should be called repository
belongs_to :link, class_name: 'Repository', foreign_key: 'repository_id', inverse_of: :links

validates :link, :repository, presence: true
validates :repository, uniqueness: { scope: [:link, :kind] }
end

Expand Down
9 changes: 2 additions & 7 deletions src/api/app/models/relationship.rb
Expand Up @@ -9,7 +9,6 @@ class Relationship < ApplicationRecord
belongs_to :project, inverse_of: :relationships
belongs_to :package, inverse_of: :relationships

validates :role, presence: true

validate :check_global_role

Expand All @@ -22,16 +21,12 @@ class Relationship < ApplicationRecord
message: 'Package has non unique id'
}

validates :package, presence: {
message: 'Neither package nor project exists'
}, unless: proc { |relationship| relationship.project.present? }
validates :package, unless: proc { |relationship| relationship.project.present? }
validates :package, absence: {
message: 'Package and project can not exist at the same time'
}, if: proc { |relationship| relationship.project.present? }

validates :user, presence: {
message: 'Neither user nor group exists'
}, unless: proc { |relationship| relationship.group.present? }
validates :user, unless: proc { |relationship| relationship.group.present? }
validates :user, absence: {
message: 'User and group can not exist at the same time'
}, if: proc { |relationship| relationship.group.present? }
Expand Down
1 change: 0 additions & 1 deletion src/api/app/models/repository.rb
Expand Up @@ -36,7 +36,6 @@ class Repository < ApplicationRecord
case_sensitive: true,
message: '%{value} is already used by a repository of this project' }

validates :project, presence: true
# NOTE: remote_project_name cannot be NULL because mysql UNIQUE KEY constraint does considers
# two NULL's to be distinct. (See mysql bug #8173)
validate :remote_project_name_not_nill
Expand Down
1 change: 0 additions & 1 deletion src/api/app/models/repository_architecture.rb
Expand Up @@ -6,7 +6,6 @@ class RepositoryArchitecture < ApplicationRecord

acts_as_list scope: [:repository_id], top_of_list: 0

validates :repository, :architecture, :position, presence: true
validates :repository, uniqueness: { scope: :architecture }

def build_id
Expand Down
8 changes: 4 additions & 4 deletions src/api/app/models/review.rb
Expand Up @@ -23,10 +23,10 @@ class NotFoundError < APIError
validates :reviewer, length: { maximum: 250 }
validates :reason, length: { maximum: 65_534 }

validates :user, presence: true, if: :by_user?
validates :group, presence: true, if: :by_group?
validates :project, presence: true, if: :by_project?, on: :create
validates :package, presence: true, if: :by_package?, on: :create
validates :user, if: :by_user?
validates :group, if: :by_group?
validates :project, if: :by_project?, on: :create
validates :package, if: :by_package?, on: :create
validates :by_project, presence: true, if: :by_package?, on: :create

validate :review_assignment
Expand Down
1 change: 0 additions & 1 deletion src/api/app/models/staging/request_exclusion.rb
Expand Up @@ -6,7 +6,6 @@ def self.table_name_prefix
belongs_to :staging_workflow, class_name: 'Staging::Workflow'
belongs_to :bs_request

validates :staging_workflow, :number, :description, presence: true
validates :bs_request_id, numericality: true, uniqueness: { scope: :staging_workflow_id, message: 'is already excluded' }
validates :description, length: { maximum: 255 }

Expand Down
1 change: 0 additions & 1 deletion src/api/app/models/staging/workflow.rb
Expand Up @@ -30,7 +30,6 @@ def ready_to_stage
has_many :request_exclusions, class_name: 'Staging::RequestExclusion', foreign_key: 'staging_workflow_id', dependent: :destroy
has_many :excluded_requests, through: :request_exclusions, source: :bs_request

validates :managers_group, :project, presence: true

after_create :create_staging_projects
after_create :add_reviewer_group
Expand Down
1 change: 0 additions & 1 deletion src/api/app/models/status/report.rb
Expand Up @@ -17,7 +17,6 @@ class Status::Report < ApplicationRecord
#### Scopes (first the default_scope macro if is used)

#### Validations macros
validates :checkable, presence: true
validates :uuid, presence: true

validates_each :checkable do |record, attr, value|
Expand Down
1 change: 0 additions & 1 deletion src/api/app/models/status_message.rb
Expand Up @@ -3,7 +3,6 @@ class StatusMessage < ApplicationRecord
has_many :status_message_acknowledgements, dependent: :destroy
has_many :users, through: :status_message_acknowledgements

validates :user, :severity, :message, presence: true

scope :announcements, -> { order('created_at DESC').where(severity: 'announcement') }
scope :for_current_user, -> { where(communication_scope: communication_scopes_for_current_user) }
Expand Down
2 changes: 0 additions & 2 deletions src/api/app/models/status_message_acknowledgement.rb
Expand Up @@ -2,8 +2,6 @@ class StatusMessageAcknowledgement < ApplicationRecord
belongs_to :status_message
belongs_to :user

validates :status_message, presence: true
validates :user, presence: true

validates :status_message_id, uniqueness: { scope: :user_id, message: 'You have already acknowledged the message' }
end
Expand Down
1 change: 0 additions & 1 deletion src/api/app/models/token.rb
Expand Up @@ -12,7 +12,6 @@ class Token < ApplicationRecord
end

validates :name, length: { maximum: 64 }
validates :user, presence: true
validates :string, uniqueness: { case_sensitive: false }
validates :scm_token, absence: true, if: -> { type != 'Token::Workflow' }

Expand Down
2 changes: 0 additions & 2 deletions src/api/app/models/watched_project.rb
Expand Up @@ -3,8 +3,6 @@ class WatchedProject < ApplicationRecord
belongs_to :user, inverse_of: :watched_projects
belongs_to :project, inverse_of: :watched_projects

validates :project, presence: true
validates :user, presence: true
end

# == Schema Information
Expand Down

0 comments on commit d2cf533

Please sign in to comment.