Skip to content

Commit

Permalink
rolify
Browse files Browse the repository at this point in the history
  • Loading branch information
chonglou committed Jul 3, 2017
1 parent 452ec7d commit c0ad4a9
Show file tree
Hide file tree
Showing 11 changed files with 123 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,5 @@ gem 'country_select'
gem 'devise'
gem 'devise-i18n'
gem 'omniauth-facebook'
gem 'cancancan'
gem 'rolify'
4 changes: 4 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ GEM
bindex (0.5.0)
builder (3.2.3)
byebug (9.0.6)
cancancan (2.0.0)
capybara (2.14.4)
addressable
mime-types (>= 1.16)
Expand Down Expand Up @@ -191,6 +192,7 @@ GEM
responders (2.4.0)
actionpack (>= 4.2.0, < 5.3)
railties (>= 4.2.0, < 5.3)
rolify (5.1.0)
ruby_dep (1.5.0)
rubyzip (1.2.1)
sass (3.4.24)
Expand Down Expand Up @@ -251,6 +253,7 @@ PLATFORMS
DEPENDENCIES
bootstrap!
byebug
cancancan
capybara (~> 2.13)
coffee-rails (~> 4.2)
country_select
Expand All @@ -265,6 +268,7 @@ DEPENDENCIES
puma (~> 3.7)
rails (~> 5.1.2)
rails-i18n!
rolify
sass-rails (~> 5.0)
selenium-webdriver
simple_form
Expand Down
35 changes: 35 additions & 0 deletions app/models/ability.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class Ability
include CanCan::Ability

def initialize(user)
# Define abilities for the passed in user here. For example:
#
user ||= User.new # guest user (not logged in)
if user.is_admin?
can :manage, :all
else
can :read, :all
# can :read, Forum
# can :write, Forum if user.has_role?(:moderator, Forum)
# can :write, Forum, :id => Forum.with_role(:moderator, user).pluck(:id)
end
#
# The first argument to `can` is the action you are giving the user
# permission to do.
# If you pass :manage it will apply to every action. Other common actions
# here are :read, :create, :update and :destroy.
#
# The second argument is the resource the user can perform the action on.
# If you pass :all it will apply to every resource. Otherwise pass a Ruby
# class of the resource.
#
# The third argument is an optional hash of conditions to further filter the
# objects.
# For example, here the user can only update published articles.
#
# can :update, Article, :published => true
#
# See the wiki for details:
# https://github.com/CanCanCommunity/cancancan/wiki/Defining-Abilities
end
end
13 changes: 13 additions & 0 deletions app/models/role.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Role < ApplicationRecord
has_and_belongs_to_many :users, :join_table => :users_roles

belongs_to :resource,
:polymorphic => true,
:optional => true

validates :resource_type,
:inclusion => { :in => Rolify.resource_types },
:allow_nil => true

scopify
end
2 changes: 2 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class User < ApplicationRecord
rolify

# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
Expand Down
7 changes: 7 additions & 0 deletions config/initializers/rolify.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Rolify.configure do |config|
# By default ORM adapter is ActiveRecord. uncomment to use mongoid
# config.use_mongoid

# Dynamic shortcuts for User class (user.is_admin? like methods). Default is: false
config.use_dynamic_shortcuts
end
19 changes: 19 additions & 0 deletions db/migrate/20170703212023_rolify_create_roles.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class RolifyCreateRoles < ActiveRecord::Migration[5.1]
def change
create_table(:roles) do |t|
t.string :name
t.references :resource, :polymorphic => true

t.timestamps
end

create_table(:users_roles, :id => false) do |t|
t.references :user
t.references :role
end

add_index(:roles, :name)
add_index(:roles, [ :name, :resource_type, :resource_id ])
add_index(:users_roles, [ :user_id, :role_id ])
end
end
21 changes: 20 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,22 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20170703204501) do
ActiveRecord::Schema.define(version: 20170703212023) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

create_table "roles", force: :cascade do |t|
t.string "name"
t.string "resource_type"
t.bigint "resource_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["name", "resource_type", "resource_id"], name: "index_roles_on_name_and_resource_type_and_resource_id"
t.index ["name"], name: "index_roles_on_name"
t.index ["resource_type", "resource_id"], name: "index_roles_on_resource_type_and_resource_id"
end

create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
Expand Down Expand Up @@ -43,4 +54,12 @@
t.index ["unlock_token"], name: "index_users_on_unlock_token", unique: true
end

create_table "users_roles", id: false, force: :cascade do |t|
t.bigint "user_id"
t.bigint "role_id"
t.index ["role_id"], name: "index_users_roles_on_role_id"
t.index ["user_id", "role_id"], name: "index_users_roles_on_user_id_and_role_id"
t.index ["user_id"], name: "index_users_roles_on_user_id"
end

end
4 changes: 3 additions & 1 deletion db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@

root = User.new email:ENV['MANAGER'], name: 'root', password: 'changeme'
root.skip_confirmation!
root.save
root.save

%w(root admin).each {|n| root.add_role n}
11 changes: 11 additions & 0 deletions test/fixtures/roles.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

# This model initially had no columns defined. If you add columns to the
# model remove the '{}' from the fixture names and add the columns immediately
# below each fixture, per the syntax in the comments below
#
one: {}
# column: value
#
two: {}
# column: value
7 changes: 7 additions & 0 deletions test/models/role_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class RoleTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

0 comments on commit c0ad4a9

Please sign in to comment.