Skip to content

Commit

Permalink
Merge pull request #2119 from bgeuken/relationship_scope
Browse files Browse the repository at this point in the history
[api][webui] Replace mixin code by scopes for relationship model
  • Loading branch information
ChrisBr committed Sep 12, 2016
2 parents eb5353b + 49bf355 commit 98de3d7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
14 changes: 2 additions & 12 deletions src/api/app/mixins/has_relationships.rb
Expand Up @@ -11,16 +11,6 @@ def add_group(group, role, ignoreLock = nil)
Relationship.add_group(self, group, role, ignoreLock)
end

def users_and_roles
relationships.joins(:role, :user).order('role_name, login').
pluck('users.login as login, roles.title AS role_name')
end

def groups_and_roles
relationships.joins(:role, :group).order('role_name, title').
pluck('groups.title as title', 'roles.title as role_name')
end

# webui code is a huge table - TODO to optimize
def users
relationships.users.includes(:user).map { |r| r.user }.uniq
Expand All @@ -40,11 +30,11 @@ def bugowner_emails
end

def render_relationships(xml)
users_and_roles.each do |user, role|
relationships.with_users_and_roles.each do |user, role|
xml.person(userid: user, role: role)
end

groups_and_roles.each do |group, role|
relationships.with_groups_and_roles.each do |group, role|
xml.group(groupid: group, role: role)
end
end
Expand Down
8 changes: 8 additions & 0 deletions src/api/app/models/relationship.rb
Expand Up @@ -43,6 +43,14 @@ class SaveError < APIException; end
scope :packages, -> { where("package_id is not null") }
scope :groups, -> { where("group_id is not null") }
scope :users, -> { where("user_id is not null") }
scope :with_users_and_roles, lambda {
joins(:role, :user).order('role_name, login').
pluck('users.login as login, roles.title AS role_name')
}
scope :with_groups_and_roles, lambda {
joins(:role, :group).order('role_name, title').
pluck('groups.title as title', 'roles.title as role_name')
}

# we only care for project<->user relationships, but the cache is not *that* expensive
# to recalculate
Expand Down

0 comments on commit 98de3d7

Please sign in to comment.