diff --git a/app/assets/stylesheets/static.css.scss b/app/assets/stylesheets/static.css.scss new file mode 100644 index 0000000..b96eedb --- /dev/null +++ b/app/assets/stylesheets/static.css.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the Static controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/static_controller.rb b/app/controllers/static_controller.rb new file mode 100644 index 0000000..2378978 --- /dev/null +++ b/app/controllers/static_controller.rb @@ -0,0 +1,5 @@ +class StaticController < ApplicationController + def home + + end +end diff --git a/app/models/ability.rb b/app/models/ability.rb new file mode 100644 index 0000000..ece2d2c --- /dev/null +++ b/app/models/ability.rb @@ -0,0 +1,43 @@ +class Ability + include CanCan::Ability + + def initialize(user) + + user ||= User.new + + if user.is_admin? + can :manage, :all + end + + if user.id + can :manage, ActiveAdmin::Page, name: 'Dashboard' + end + + # Define abilities for the passed in user here. For example: + # + # user ||= User.new # guest user (not logged in) + # if user.admin? + # can :manage, :all + # else + # can :read, :all + # 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/ryanb/cancan/wiki/Defining-Abilities + end +end diff --git a/app/models/role.rb b/app/models/role.rb new file mode 100644 index 0000000..145baa7 --- /dev/null +++ b/app/models/role.rb @@ -0,0 +1,6 @@ +class Role < ActiveRecord::Base + has_and_belongs_to_many :users, :join_table => :users_roles + belongs_to :resource, :polymorphic => true + + scopify +end diff --git a/app/models/user.rb b/app/models/user.rb index 5dfa057..fd9e136 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,4 +1,5 @@ class User < ActiveRecord::Base + rolify # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable and :omniauthable devise :database_authenticatable, diff --git a/app/views/static/home.haml b/app/views/static/home.haml new file mode 100644 index 0000000..0e2b5b3 --- /dev/null +++ b/app/views/static/home.haml @@ -0,0 +1 @@ +%h1 Home \ No newline at end of file diff --git a/config/initializers/rolify.rb b/config/initializers/rolify.rb new file mode 100644 index 0000000..b90ef58 --- /dev/null +++ b/config/initializers/rolify.rb @@ -0,0 +1,8 @@ +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 + # Enable this feature _after_ running rake db:migrate as it relies on the roles table + config.use_dynamic_shortcuts +end diff --git a/config/routes.rb b/config/routes.rb index 32e989d..d9c2390 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -2,4 +2,6 @@ Rails.application.routes.draw do devise_for :users, ActiveAdmin::Devise.config ActiveAdmin.routes(self) + + root 'static#home', as: :home end diff --git a/db/migrate/20140513072121_rolify_create_roles.rb b/db/migrate/20140513072121_rolify_create_roles.rb new file mode 100644 index 0000000..999c94a --- /dev/null +++ b/db/migrate/20140513072121_rolify_create_roles.rb @@ -0,0 +1,19 @@ +class RolifyCreateRoles < ActiveRecord::Migration + 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 diff --git a/db/schema.rb b/db/schema.rb index 4e6f2c7..43d8c7c 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,18 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20140513065840) do +ActiveRecord::Schema.define(version: 20140513072121) do + + create_table "roles", force: true do |t| + t.string "name" + t.integer "resource_id" + t.string "resource_type" + t.datetime "created_at" + t.datetime "updated_at" + end + + add_index "roles", ["name", "resource_type", "resource_id"], name: "index_roles_on_name_and_resource_type_and_resource_id" + add_index "roles", ["name"], name: "index_roles_on_name" create_table "users", force: true do |t| t.string "name" @@ -32,4 +43,11 @@ ActiveRecord::Schema.define(version: 20140513065840) do add_index "users", ["email"], name: "index_users_on_email", unique: true add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true + create_table "users_roles", id: false, force: true do |t| + t.integer "user_id" + t.integer "role_id" + end + + add_index "users_roles", ["user_id", "role_id"], name: "index_users_roles_on_user_id_and_role_id" + end diff --git a/db/seeds.rb b/db/seeds.rb index 997068d..4edb1e8 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -5,5 +5,3 @@ # # cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }]) # Mayor.create(name: 'Emanuel', city: cities.first) -user = CreateAdminService.new.call -puts 'CREATED ADMIN USER: ' << user.email -- libgit2 0.21.2