Papeel
Setup
rails generate papeel:initializer
rails generate papeel:migration
rake db:migrate
In config/initializer/papeel.rb
, whitelist your roles (needed to validate a role):
Papeel.configure do |config|
config.roles = [:super_admin, :admin]
end
class User < ActiveRecord::Base
include Papeel::ActsAsPapeelUser
acts_as_papeel_user
end
class Forum < ActiveRecord::Base
include Papeel::ActsAsPapeelResource
acts_as_papeel_resource
end
What it does ?
acts_as_papeel_user
instance methods
It adds a relation has_many :roles
to the User class.
It adds a method named has_role?
which work as follow:
user = User.find(1)
Papeel::Role.create user: user, name: :admin
user.has_role? :admin
=> true
user.has_role? :admin, any: true
=> false
# =======================================
user = User.find(2)
forum = Forum.find(1)
Papeel::Role.create user: user, name: :admin, resource: forum
user.has_role? :admin
=> false
user.has_role? :admin, any: true
=> true
user.has_role? :admin, on: forum
=> true
user.has_role? :admin, on_type: "Forum"
=> true
user.has_role? :admin, on_type: "Forum", on_id: forum.id
=> true
it generates methods based on roles specified in papeel configuration, example:
# config/initialize
Papeel.configure do |config|
config.roles = [:super_admin, :admin]
end
# generates the following methods
user = User.find(1)
forum = Forum.find(1)
user.is_super_admin?
user.is_admin?
user.is_admin? on: forum
Those methods accept the same arguments as the has_role?
method.
class methods
it adds scopes based on papeel configuration, example:
User.super_admin
# return users who are super_admin with AND without resource,
# chain a where clause if you want to be more specific
User.admin
# same but for admin role
acts_as_papeel_resource
instance methods
It adds a polymorphic relation has_many :roles_as_resource
to the resource class.