为您的 Rails 模型添加组和成员功能
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".
The following ORMs are supported:
The following Rubies are supported:
The following databases are supported:
Add this line to your application's Gemfile:
gem 'groupify'And then execute:
$ bundleOr install it yourself as:
$ gem install groupifyExecute:
$ rails generate groupify:active_record:installThis will generate an initializer, Group model, GroupMembership model, and migrations.
Modify the models and migrations as needed, then run the migration:
$ rake db:migrateSet up your member models:
class User true
# Add multiple members at once
group.add(user, widget, task)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 useruser.named_groups true
user.named_groups.destroy(:admin)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 groupsUser.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…# Moves the members of source into destination, and destroys source
destination_group.merge!(source_group)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') # => falseGroupify 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.
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# 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) # => falseclass 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
endGroupify 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.
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.
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:upgradeAnd then run the migration:
$ rake db:migratePlease note that this migration may block writes in MySQL if your group_memberships
table is large.
git checkout -b my-new-feature)git commit -am 'Added some feature')git push origin my-new-feature)See a list of contributors here.
暂无开放 Issues,或尚未同步最近议题。