Commit 3f864de1958e52ba1be5a7bb22ead8b1c392218a

Authored by Fernando Brito
1 parent 15668d05
Exists in master and in 2 other branches v2, wikilibras

CanCan and Rolify

app/assets/stylesheets/static.css.scss 0 → 100644
... ... @@ -0,0 +1,3 @@
  1 +// Place all the styles related to the Static controller here.
  2 +// They will automatically be included in application.css.
  3 +// You can use Sass (SCSS) here: http://sass-lang.com/
... ...
app/controllers/static_controller.rb 0 → 100644
... ... @@ -0,0 +1,5 @@
  1 +class StaticController < ApplicationController
  2 + def home
  3 +
  4 + end
  5 +end
... ...
app/models/ability.rb 0 → 100644
... ... @@ -0,0 +1,43 @@
  1 +class Ability
  2 + include CanCan::Ability
  3 +
  4 + def initialize(user)
  5 +
  6 + user ||= User.new
  7 +
  8 + if user.is_admin?
  9 + can :manage, :all
  10 + end
  11 +
  12 + if user.id
  13 + can :manage, ActiveAdmin::Page, name: 'Dashboard'
  14 + end
  15 +
  16 + # Define abilities for the passed in user here. For example:
  17 + #
  18 + # user ||= User.new # guest user (not logged in)
  19 + # if user.admin?
  20 + # can :manage, :all
  21 + # else
  22 + # can :read, :all
  23 + # end
  24 + #
  25 + # The first argument to `can` is the action you are giving the user
  26 + # permission to do.
  27 + # If you pass :manage it will apply to every action. Other common actions
  28 + # here are :read, :create, :update and :destroy.
  29 + #
  30 + # The second argument is the resource the user can perform the action on.
  31 + # If you pass :all it will apply to every resource. Otherwise pass a Ruby
  32 + # class of the resource.
  33 + #
  34 + # The third argument is an optional hash of conditions to further filter the
  35 + # objects.
  36 + # For example, here the user can only update published articles.
  37 + #
  38 + # can :update, Article, :published => true
  39 + #
  40 + # See the wiki for details:
  41 + # https://github.com/ryanb/cancan/wiki/Defining-Abilities
  42 + end
  43 +end
... ...
app/models/role.rb 0 → 100644
... ... @@ -0,0 +1,6 @@
  1 +class Role < ActiveRecord::Base
  2 + has_and_belongs_to_many :users, :join_table => :users_roles
  3 + belongs_to :resource, :polymorphic => true
  4 +
  5 + scopify
  6 +end
... ...
app/models/user.rb
1 1 class User < ActiveRecord::Base
  2 + rolify
2 3 # Include default devise modules. Others available are:
3 4 # :confirmable, :lockable, :timeoutable and :omniauthable
4 5 devise :database_authenticatable,
... ...
app/views/static/home.haml 0 → 100644
... ... @@ -0,0 +1 @@
  1 +%h1 Home
0 2 \ No newline at end of file
... ...
config/initializers/rolify.rb 0 → 100644
... ... @@ -0,0 +1,8 @@
  1 +Rolify.configure do |config|
  2 + # By default ORM adapter is ActiveRecord. uncomment to use mongoid
  3 + # config.use_mongoid
  4 +
  5 + # Dynamic shortcuts for User class (user.is_admin? like methods). Default is: false
  6 + # Enable this feature _after_ running rake db:migrate as it relies on the roles table
  7 + config.use_dynamic_shortcuts
  8 +end
... ...
config/routes.rb
... ... @@ -2,4 +2,6 @@ Rails.application.routes.draw do
2 2 devise_for :users, ActiveAdmin::Devise.config
3 3  
4 4 ActiveAdmin.routes(self)
  5 +
  6 + root 'static#home', as: :home
5 7 end
... ...
db/migrate/20140513072121_rolify_create_roles.rb 0 → 100644
... ... @@ -0,0 +1,19 @@
  1 +class RolifyCreateRoles < ActiveRecord::Migration
  2 + def change
  3 + create_table(:roles) do |t|
  4 + t.string :name
  5 + t.references :resource, :polymorphic => true
  6 +
  7 + t.timestamps
  8 + end
  9 +
  10 + create_table(:users_roles, :id => false) do |t|
  11 + t.references :user
  12 + t.references :role
  13 + end
  14 +
  15 + add_index(:roles, :name)
  16 + add_index(:roles, [ :name, :resource_type, :resource_id ])
  17 + add_index(:users_roles, [ :user_id, :role_id ])
  18 + end
  19 +end
... ...
db/schema.rb
... ... @@ -11,7 +11,18 @@
11 11 #
12 12 # It's strongly recommended that you check this file into your version control system.
13 13  
14   -ActiveRecord::Schema.define(version: 20140513065840) do
  14 +ActiveRecord::Schema.define(version: 20140513072121) do
  15 +
  16 + create_table "roles", force: true do |t|
  17 + t.string "name"
  18 + t.integer "resource_id"
  19 + t.string "resource_type"
  20 + t.datetime "created_at"
  21 + t.datetime "updated_at"
  22 + end
  23 +
  24 + add_index "roles", ["name", "resource_type", "resource_id"], name: "index_roles_on_name_and_resource_type_and_resource_id"
  25 + add_index "roles", ["name"], name: "index_roles_on_name"
15 26  
16 27 create_table "users", force: true do |t|
17 28 t.string "name"
... ... @@ -32,4 +43,11 @@ ActiveRecord::Schema.define(version: 20140513065840) do
32 43 add_index "users", ["email"], name: "index_users_on_email", unique: true
33 44 add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
34 45  
  46 + create_table "users_roles", id: false, force: true do |t|
  47 + t.integer "user_id"
  48 + t.integer "role_id"
  49 + end
  50 +
  51 + add_index "users_roles", ["user_id", "role_id"], name: "index_users_roles_on_user_id_and_role_id"
  52 +
35 53 end
... ...
db/seeds.rb
... ... @@ -5,5 +5,3 @@
5 5 #
6 6 # cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }])
7 7 # Mayor.create(name: 'Emanuel', city: cities.first)
8   -user = CreateAdminService.new.call
9   -puts 'CREATED ADMIN USER: ' << user.email
... ...