Skip to content

Commit

Permalink
Merge branch 'master' into role_model_codestyle
Browse files Browse the repository at this point in the history
  • Loading branch information
EppO authored Mar 1, 2019
2 parents 30176ca + bb3d445 commit 3e73146
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,17 @@ user.has_role? :moderator, Forum.last
=> true
```

To check if a user has the exact role scoped to a resource class:

```ruby
user = User.find(5)
user.add_role :moderator # sets a global role
user.has_role? :moderator, Forum.first
=> true
user.has_strict_role? :moderator, Forum.last
=> false
```

### 6. Resource roles querying

Starting from rolify 3.0, you can search roles on instance level or class level resources.
Expand Down
2 changes: 1 addition & 1 deletion lib/rolify/adapters/active_record/resource_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def applied_roles(relation, children)

def all_except(resource, excluded_obj)
prime_key = resource.primary_key.to_sym
resource.where(prime_key => (resource.all - excluded_obj).map(&prime_key))
resource.where.not(prime_key => excluded_obj.pluck(prime_key))
end

private
Expand Down
10 changes: 2 additions & 8 deletions lib/rolify/adapters/active_record/role_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,13 @@ def exists?(relation, column)
end

def scope(relation, conditions)
if Rails.version < "4.0"
query = relation.scoped
else
query = relation.all
end
query = query.joins(:roles)
query = relation.joins(:roles)
query = where(query, conditions)
query
end

def all_except(user, excluded_obj)
prime_key = user.primary_key.to_sym
user.where(prime_key => (user.all - excluded_obj).map(&prime_key))
user.where.not(user.primary_key => excluded_obj)
end

private
Expand Down

0 comments on commit 3e73146

Please sign in to comment.