From 69e6fca80f1d3abf6a66be3ab6a349be7db93859 Mon Sep 17 00:00:00 2001 From: Yaroslav Shmarov Date: Tue, 11 May 2021 13:37:44 +0200 Subject: [PATCH 1/2] Improve readme based on PR 181 comments https://github.com/RolifyCommunity/rolify/pull/181#issuecomment-838263755 --- README.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/README.md b/README.md index 8cb93c6d..dbfcc29e 100644 --- a/README.md +++ b/README.md @@ -267,6 +267,33 @@ end This method should be used with caution. If you don't preload the roles, the `has_cached_role?` might return `false`. In the above example, it would return `false` for `@user.has_cached_role?(:member, Forum)`, because `User.with_role(:admin, Forum)` will load only the `:admin` roles. +### Scoping + +user.rb +``` +has_many :posts, through: :roles, source: :resource, source_type: :Post +has_many :moderated_posts, -> { where(roles: {name: :moderator}) }, through: :roles, source: :resource, source_type: :Post +``` +let's you do +``` +@user.posts +# => [ all the posts where the @user has a role ] +@user.moderated_posts +# => [ all the posts where the @user has a moderator ] +``` +post.rb +``` +has_many :users, through: :roles, class_name: 'User', source: :users +has_many :moderators, -> { where(:roles => {name: :moderator}) }, through: :roles, class_name: 'User', source: :users +``` +let's you do +``` +@post.users +# => [ all the users that have a role in this post ] +@post.moderators +# => [ all the users that have a moderator role in this post ] +``` + ## Resources * [Wiki](https://github.com/RolifyCommunity/rolify/wiki) From baeb0b5fc0810153f71d13e9e44bfe983c58fd53 Mon Sep 17 00:00:00 2001 From: Yaroslav Shmarov Date: Sun, 18 Jul 2021 11:00:44 +0200 Subject: [PATCH 2/2] the link is no longer valid the link is no longer valid * [Step-by-step tutorial](http://railsapps.github.com/tutorial-rails-bootstrap-devise-cancan.html) provided by [RailsApps](http://railsapps.github.com/) --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index dbfcc29e..2403299c 100644 --- a/README.md +++ b/README.md @@ -301,7 +301,6 @@ let's you do * [Tutorials](https://github.com/RolifyCommunity/rolify/wiki#wiki-tutorials): * [How-To use rolify with Devise and CanCanCan](https://github.com/RolifyCommunity/rolify/wiki/Devise---CanCanCan---rolify-Tutorial) * [Using rolify with Devise and Authority](https://github.com/RolifyCommunity/rolify/wiki/Using-rolify-with-Devise-and-Authority) - * [Step-by-step tutorial](http://railsapps.github.com/tutorial-rails-bootstrap-devise-cancan.html) provided by [RailsApps](http://railsapps.github.com/) ## Upgrade from previous versions