Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
G

groupify

> 数据库
Open source

Add group and membership functionality to your Rails models

196 stars0 likes2 views
WebsiteGitHub

About

Add group and membership functionality to your Rails models

Groupify

Adds group and membership functionality to Rails models. Defines a polymorphic relationship between a Group model and any member model. Don't need a Group model? Use named groups instead to add members to named groups such as :admin or "Team Rocketpants".

Compatibility

The following ORMs are supported:

  • ActiveRecord 4.x, 5.x
  • Mongoid 4.x, 5.x, 6.x

The following Rubies are supported:

  • MRI Ruby 2.2, 2.3, 2.4
  • JRuby 9000

The following databases are supported:

  • MySQL
  • PostgreSQL
  • SQLite
  • MongoDB

Installation

Add this line to your application's Gemfile:

gem 'groupify'

And then execute:

$ bundle

Or install it yourself as:

$ gem install groupify

Setup

Active Record

Execute:

$ rails generate groupify:active_record:install

This will generate an initializer, Group model, GroupMembership model, and migrations.

Modify the models and migrations as needed, then run the migration:

$ rake db:migrate

Set up your member models:

ruby
class User  true

# Add multiple members at once
group.add(user, widget, task)

Remove from groups

ruby
users.groups.destroy(group)          # Destroys this user's group membership for this group
group.users.delete(user)             # Deletes this group's group membership for this user

Named groups

ruby
user.named_groups  true
user.named_groups.destroy(:admin)

Check if two members share any of the same groups:

ruby
user1.shares_any_group?(user2)          # Returns true if user1 and user2 are in any of the same groups
user2.shares_any_named_group?(user1)    # Also works for named groups

Query for groups & members:

ruby
User.in_group(group)                # Find all users in this group
User.in_named_group(:admin)         # Find all users in this named group
Group.with_member(user)             # Find all groups with this user

User.shares_any_group(user)         # Find all users that share any groups with this user
User.shares_any_named_group(user)   # Find all users that share any named groups with this user

Check if member belongs to any/all groups

…

Merge one group into another:

ruby
# Moves the members of source into destination, and destroys source
destination_group.merge!(source_group)

Membership Types

Membership types allow a member to belong to a group in a more specific way. For example, you can add a user to a group with membership type of "manager" to specify that this user has the "manager role" on that group.

This can be used to implement role-based authorization combined with group authorization, which could be used to mass-assign roles to groups of resources.

It could also be used to add users and resources to the same "sub-group" or "project" within a larger group (say, an organization).

…

Note that adding a member to a group with a specific membership type will automatically add them to that group without a specific membership type. This way you can still query groups and find the member in that group. If you then remove that specific membership type, they still remain in the group without a specific membership type.

Removing a member from a group will bulk remove any specific membership types as well.

group.add(manager, as: 'manager')
manager.groups.include?(group)              # => true

manager.groups.delete(group, as: 'manager')
manager.groups.include?(group)              # => true

group.add(employee, as: 'employee')
employee.groups.delete(group)
employee.in_group?(group)                   # => false
employee.in_group?(group, as: 'employee')   # => false

Using for Authorization

Groupify was originally created to help implement user authorization, although it can be used generically for much more than that. Here are some examples of how to do it.

With CanCan

ruby
class Ability
  include CanCan::Ability

  def initialize(user)
    # Implements group-based authorization
    # Users can only manage assignment which belong to the same group.
    can [:manage], Assignment, Assignment.shares_any_group(user) do |assignment|
      assignment.shares_any_group?(user)
    end
  end
end

With Authority

ruby
# Whatever class represents a logged-in user in your app
class User
  groupify :named_group_member
  include Authority::UserAbilities
end

class Widget
  groupify :named_group_member
  include Authority::Abilities
end

class WidgetAuthorizer   true
user.can_update?(widget)  # => true
user.can_delete?(widget)  # => false

With Pundit

ruby
class PostPolicy < Struct.new(:user, :post)
  # User can only update a published post if they are admin of the same group.
  def update?
    user.shares_any_group?(post, as: :admin) || !post.published?
  end

  class Scope < Struct.new(:user, :scope)
    def resolve
      if user.admin?
        # An admin can see all the posts in the group(s) they are admin for
        scope.shares_any_group(user).as(:admin)
      else
        # Normal users can only see published posts in the same group(s).
        scope.shares_any_group(user).where(published: true)
      end
    end
  end
end

Backwards-Incompatible Releases

0.9+ - Dropped support for Rails 3.2 and Ruby 1.9 - 2.1

Groupify 0.9 added support for Rails 5.1, and dropped support for EOL'ed versions of Ruby, Rails, ActiveRecord, and Mongoid.

ActiveRecord 5.1 no longer supports passing arguments to collection associations. Because of this, the undocumented syntax groups.as(:membership_type) is no longer supported.

0.8+ - Name Change for group_memberships Associations (ActiveRecord only)

Groupify 0.8 changed the ActiveRecord adapter to support configuring the same model as both a group and a group member. To accomplish this, the internal group_memberships association was renamed to be different for groups and members. If you were using it, please be aware that you will need to change your code. This association is considered to be an internal implementation details and not part of the public API, so please don't rely on it if you can avoid it.

0.7+ - Polymorphic Groups (ActiveRecord only)

Groupify < 0.7 required a single Group model used for all group memberships. Groupify 0.7+ supports using multiple models as groups by implementing polymorphic associations. Upgrading requires adding a new group_type column to the group_memberships table and populating that column with the class name of the group. Create the migration by executing:

$ rails generate groupify:active_record:upgrade

And then run the migration:

$ rake db:migrate

Please note that this migration may block writes in MySQL if your group_memberships table is large.

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Added some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

Contributors

See a list of contributors here.

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Rubyactiverecordauthorizationgroup-membershipgroups

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 18, 2026
Category数据库
PricingOpen source

> Related tools

P
PostgreSQL
功能强大的开源关系型数据库
R
Redis
内存数据结构存储,常用作缓存与队列
M
MySQL
广泛使用的开源关系型数据库