Skip to content

Commit

Permalink
Merge pull request #10013 from vpereira/refactor_user_relevant_packag…
Browse files Browse the repository at this point in the history
…es_for_status

Refactor method User#user_relevant_packages_for_status
  • Loading branch information
vpereira committed Aug 17, 2020
2 parents 74bd1ba + 31f0218 commit 9f4c3a3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
11 changes: 1 addition & 10 deletions src/api/app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -775,16 +775,7 @@ def involved_patchinfos
end

def user_relevant_packages_for_status
role_id = Role.hashed['maintainer'].id
# First fetch the project ids
projects_ids = involved_projects.select(:id)
packages = Package.joins("LEFT OUTER JOIN relationships ON (relationships.package_id = packages.id AND relationships.role_id = #{role_id})")
# No maintainers
packages = packages.where([
'(relationships.user_id = ?) OR '\
'(relationships.user_id is null AND packages.project_id in (?) )', id, projects_ids
])
packages.pluck(:id)
MaintainedPackagesByUserFinder.new(self).call.pluck(:id)
end

def state
Expand Down
30 changes: 30 additions & 0 deletions src/api/app/queries/maintained_packages_by_user_finder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class MaintainedPackagesByUserFinder
def initialize(user)
@user = user
end

def call
packages_maintained_by_the_user
end

# private

def maintainer_role_id
Role.hashed['maintainer'].id
end

def involved_projects_ids
@user.involved_projects.select(:id)
end

def packages_by_role
Package.left_outer_joins(:relationships).where('relationships.role_id = ?', maintainer_role_id)
end

def packages_maintained_by_the_user
packages_by_role.where([
'(relationships.user_id = ?) OR '\
'(relationships.user_id is null AND packages.project_id in (?) )', @user.id, involved_projects_ids
])
end
end

0 comments on commit 9f4c3a3

Please sign in to comment.