GithubHelp home page GithubHelp logo

evilmint / royce Goto Github PK

View Code? Open in Web Editor NEW

This project forked from martinjnash/royce

0.0 1.0 0.0 49 KB

A Ruby on Rails roles solution.

License: MIT License

Ruby 83.95% JavaScript 1.56% CSS 1.78% HTML 12.71%

royce's Introduction

Gem Version

Royce

Roles in Rails.

Installation

Add this to your Gemfile.

gem 'royce'

Be sure to run bundle install.

Run this in your terminal.

rails g royce:install

And run rake db:migrate.

Add this to a model.

royce_roles %w[ owner editor administrator ]

In Depth

Adding royce to a model is super simple. The following code will add the roles user, admin, and editor to your model class. You can pass in an array of strings or symbols. You can even pass in a mixed array of strings and symbols.

class User < ActiveRecord::Base
  royce_roles %w[ user admin editor ] # array of strings
end

class Sailor < ActiveRecord::Base
  royce_roles [ :captain, :bosun, :quartermaster ] # array of symbols
end

class RockAndRoller < ActiveRecord::Base
  royce_roles [ :drummer, 'bassist', :editor ] # array of strings and symbols
end

Now instances of your user class have some roles methods. You can add, remove, query role status, and even ask if an instance can accept such a role.

user = User.create()

user.add_role :user
user.add_role 'user'
user.add_role Royce::Role.find_by(name: 'user')

user.remove_role :user
user.remove_role 'user'
user.remove_role Royce::Role.find_by(name: 'user')

user.has_role? :user
user.has_role? 'user'
user.has_role? Royce::Role.find_by(name: 'user')

user.allowed_role? 'user'
user.allowed_role? :user
user.allowed_role? Royce::Role.find_by(name: 'user')

You also get some conveneint methods to query if a user has a certain named role.

user.admin?
user.editor?
user.user?

You can easily add a role to your model object using our bang! methods.

user.user!
user.admin!
user.editor!

Get a list of roles that a particular user has

user.add_role :user
user.add_role :admin
user.role_list # => ['user', 'admin']

Get a list of all roles available to a class

User.available_role_names # => ['user', 'admin', 'editor']

Not enough. You also get named scopes on your models.

User.admins
User.editors
User.users

If you liked that, you'll LOVE this. We've added the ability to take a role, and return all instances of a class that have that role.

For Users it'll look like this:

admin_role = Royce::Role.find_by(name: 'admin')
admin_role.users.all # Array of user objects

royce's People

Contributors

martinjnash avatar ready4god2513 avatar danielkehoe avatar

Watchers

Aleksander Lorenc avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.