Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use associations to define rubygem reverse dependencies #4512

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions app/models/dependency.rb
@@ -1,6 +1,7 @@
class Dependency < ApplicationRecord
belongs_to :rubygem, optional: true
belongs_to :version
has_one :version_rubygem, through: :version, source: :rubygem

before_validation :use_gem_dependency,
:use_existing_rubygem,
Expand Down
18 changes: 4 additions & 14 deletions app/models/rubygem.rb
Expand Up @@ -22,6 +22,10 @@ class Rubygem < ApplicationRecord
has_many :audits, as: :auditable, inverse_of: :auditable
has_many :link_verifications, as: :linkable, inverse_of: :linkable, dependent: :destroy
has_many :oidc_rubygem_trusted_publishers, class_name: "OIDC::RubygemTrustedPublisher", inverse_of: :rubygem, dependent: :destroy
has_many :incoming_dependencies, -> { where(versions: { indexed: true, position: 0 }) }, class_name: "Dependency", inverse_of: :rubygem
has_many :reverse_dependencies, through: :incoming_dependencies, source: :version_rubygem
has_many :reverse_development_dependencies, -> { merge(Dependency.development) }, through: :incoming_dependencies, source: :version_rubygem
has_many :reverse_runtime_dependencies, -> { merge(Dependency.runtime) }, through: :incoming_dependencies, source: :version_rubygem

validates :name,
length: { maximum: Gemcutter::MAX_FIELD_LENGTH },
Expand Down Expand Up @@ -341,20 +345,6 @@ def release_reserved_namespace!
update_attribute(:updated_at, 101.days.ago)
end

def reverse_dependencies
self.class.joins("inner join versions as v on v.rubygem_id = rubygems.id
inner join dependencies as d on d.version_id = v.id").where("v.indexed = 't'
and v.position = 0 and d.rubygem_id = ?", id)
end

def reverse_development_dependencies
reverse_dependencies.where("d.scope = 'development'")
end

def reverse_runtime_dependencies
reverse_dependencies.where("d.scope ='runtime'")
end

def metadata_mfa_required?
latest_version&.rubygems_metadata_mfa_required?
end
Expand Down