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

Filtering group members based on membership_type #35

Open
adampetrie opened this issue Oct 19, 2015 · 1 comment
Open

Filtering group members based on membership_type #35

adampetrie opened this issue Oct 19, 2015 · 1 comment

Comments

@adampetrie
Copy link

Hey - great work on this gem!

I have created a group where each member has a membership_type. When a given user looks up their groups Id like to show them the other users in that group but I want to restrict it based on the current user's membership_type.

For example, a group 'owner' can see all members where as an 'editor' can only see other editors.

Is there a standard approach to something like that?

I've mucked around and come up with this but I'm not sure if its the best approach:

class ProjectGroup < Group
  def visible_members(user)
    user_membership = group_memberships.where(
      member_id: user.id,
      group_type: type
    ).first
    members.as(user_membership.membership_type)
  end
end

Thanks!

@dwbutler
Copy link
Owner

Hi,

Currently there isn't any standard API for querying for group membership types, or filtering members by shared membership type. I think it makes sense to add these. It may be sufficient to add a single method, GroupMember#group_membership_types(group), which would return all the membership types for a member in a given group.

For now, it looks like your solution probably works for your specific use case. Consider however that in the general case, a member may have multiple membership types in a single group, and a group may have multiple types of members. So consider using this modified version:

class ProjectGroup < Group
  def visible_members(user)
    membership_types = user.group_memberships.where(
      group: self
    ).select(:membership_type)
    members.as(membership_types)
  end
end

This will correctly query all membership types in the general case. It should also generate a single query with a subquery, instead of running two queries.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants