From 1d5646e61c091d41fab7e01d043a3feebcae6e6f Mon Sep 17 00:00:00 2001 From: Ben Date: Wed, 3 Apr 2024 16:49:16 -0500 Subject: [PATCH] Rolify has been added successfully to the system. --- app/models/role.rb | 14 +++++++++++++ app/models/user.rb | 1 + config/initializers/rolify.rb | 10 +++++++++ .../20240403213530_rolify_create_roles.rb | 19 +++++++++++++++++ db/schema.rb | 21 ++++++++++++++++++- test/fixtures/roles.yml | 14 ++++++++----- 6 files changed, 73 insertions(+), 6 deletions(-) create mode 100644 app/models/role.rb create mode 100644 config/initializers/rolify.rb create mode 100644 db/migrate/20240403213530_rolify_create_roles.rb diff --git a/app/models/role.rb b/app/models/role.rb new file mode 100644 index 0000000..29e7592 --- /dev/null +++ b/app/models/role.rb @@ -0,0 +1,14 @@ +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 diff --git a/app/models/user.rb b/app/models/user.rb index d890566..86f8c1c 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,3 +1,4 @@ class User < ApplicationRecord + rolify devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable end \ No newline at end of file diff --git a/config/initializers/rolify.rb b/config/initializers/rolify.rb new file mode 100644 index 0000000..bbf8fe2 --- /dev/null +++ b/config/initializers/rolify.rb @@ -0,0 +1,10 @@ +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 + + # Configuration to remove roles from database once the last resource is removed. Default is: true + # config.remove_role_if_empty = false +end diff --git a/db/migrate/20240403213530_rolify_create_roles.rb b/db/migrate/20240403213530_rolify_create_roles.rb new file mode 100644 index 0000000..80ea4fc --- /dev/null +++ b/db/migrate/20240403213530_rolify_create_roles.rb @@ -0,0 +1,19 @@ +class RolifyCreateRoles < ActiveRecord::Migration[7.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 diff --git a/db/schema.rb b/db/schema.rb index 1fc644d..9e1803d 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2024_04_02_214014) do +ActiveRecord::Schema[7.1].define(version: 2024_04_03_213530) do create_table "bank_accounts", force: :cascade do |t| t.string "institution_name" t.string "account_type" @@ -149,6 +149,17 @@ ActiveRecord::Schema[7.1].define(version: 2024_04_02_214014) do t.datetime "updated_at", null: false end + create_table "roles", force: :cascade do |t| + t.string "name" + t.string "resource_type" + t.integer "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" + end + create_table "service_contracts", force: :cascade do |t| t.integer "participant_id", null: false t.integer "vendor_id", null: false @@ -173,6 +184,14 @@ ActiveRecord::Schema[7.1].define(version: 2024_04_02_214014) do t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true end + create_table "users_roles", id: false, force: :cascade do |t| + t.integer "user_id" + t.integer "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 + create_table "vendors", force: :cascade do |t| t.string "name" t.string "phone" diff --git a/test/fixtures/roles.yml b/test/fixtures/roles.yml index 7d41224..d7a3329 100644 --- a/test/fixtures/roles.yml +++ b/test/fixtures/roles.yml @@ -1,7 +1,11 @@ # Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html -one: - name: MyString - -two: - name: MyString +# 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