From 705a465292adb0d4219362783c137a45d8c496e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Geonizeli?= Date: Thu, 21 Jul 2022 10:45:48 -0300 Subject: [PATCH] add roles to admin panel --- app/admin/roles.rb | 3 + app/admin/users.rb | 5 +- app/models/role.rb | 18 ++ app/models/user.rb | 2 + db/migrate/20220721133408_create_roles.rb | 11 + ...20721133421_create_join_table_user_role.rb | 8 + db/schema.rb | 16 +- erd.svg | 210 ++++++++++-------- spec/factories/roles.rb | 18 ++ spec/models/role_spec.rb | 18 ++ 10 files changed, 209 insertions(+), 100 deletions(-) create mode 100644 app/admin/roles.rb create mode 100644 app/models/role.rb create mode 100644 db/migrate/20220721133408_create_roles.rb create mode 100644 db/migrate/20220721133421_create_join_table_user_role.rb create mode 100644 spec/factories/roles.rb create mode 100644 spec/models/role_spec.rb diff --git a/app/admin/roles.rb b/app/admin/roles.rb new file mode 100644 index 0000000..288fb6e --- /dev/null +++ b/app/admin/roles.rb @@ -0,0 +1,3 @@ +ActiveAdmin.register Role do + permit_params :name +end diff --git a/app/admin/users.rb b/app/admin/users.rb index 75277d0..872ed37 100644 --- a/app/admin/users.rb +++ b/app/admin/users.rb @@ -1,5 +1,5 @@ ActiveAdmin.register User do - permit_params :email, :name + permit_params :email, :name, role_ids: [] index do selectable_column @@ -17,7 +17,8 @@ ActiveAdmin.register User do form do |f| f.inputs do f.input :email - f.input :email + f.input :name + f.input :roles, as: :check_boxes end f.actions end diff --git a/app/models/role.rb b/app/models/role.rb new file mode 100644 index 0000000..a612a08 --- /dev/null +++ b/app/models/role.rb @@ -0,0 +1,18 @@ +# == Schema Information +# +# Table name: roles +# +# id :bigint not null, primary key +# name :string +# created_at :datetime not null +# updated_at :datetime not null +# +# Indexes +# +# index_roles_on_name (name) UNIQUE +# +class Role < ApplicationRecord + has_and_belongs_to_many :users + + validates :name, presence: true, uniqueness: true +end diff --git a/app/models/user.rb b/app/models/user.rb index 201c27e..9c33d5e 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -26,5 +26,7 @@ class User < ApplicationRecord :omniauthable, omniauth_providers: [:google_oauth2] + has_and_belongs_to_many :roles + validates :name, presence: true end diff --git a/db/migrate/20220721133408_create_roles.rb b/db/migrate/20220721133408_create_roles.rb new file mode 100644 index 0000000..1e43112 --- /dev/null +++ b/db/migrate/20220721133408_create_roles.rb @@ -0,0 +1,11 @@ +class CreateRoles < ActiveRecord::Migration[7.0] + def change + create_table :roles do |t| + t.string :name + + t.timestamps + end + + add_index(:roles, :name, unique: true) + end +end diff --git a/db/migrate/20220721133421_create_join_table_user_role.rb b/db/migrate/20220721133421_create_join_table_user_role.rb new file mode 100644 index 0000000..4a579bd --- /dev/null +++ b/db/migrate/20220721133421_create_join_table_user_role.rb @@ -0,0 +1,8 @@ +class CreateJoinTableUserRole < ActiveRecord::Migration[7.0] + def change + create_join_table(:users, :roles, unique: true) do |t| + t.index(%i[user_id role_id]) + t.index(%i[role_id user_id]) + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 66c6ca0..1946a39 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.0].define(version: 2022_07_21_132939) do +ActiveRecord::Schema[7.0].define(version: 2022_07_21_133421) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -118,6 +118,20 @@ ActiveRecord::Schema[7.0].define(version: 2022_07_21_132939) do t.index ["user_id"], name: "index_review_requests_on_user_id" end + create_table "roles", force: :cascade do |t| + t.string "name" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["name"], name: "index_roles_on_name", unique: true + end + + create_table "roles_users", id: false, force: :cascade do |t| + t.bigint "user_id", null: false + t.bigint "role_id", null: false + t.index ["role_id", "user_id"], name: "index_roles_users_on_role_id_and_user_id" + t.index ["user_id", "role_id"], name: "index_roles_users_on_user_id_and_role_id" + end + create_table "subjects", force: :cascade do |t| t.string "name" t.bigint "category_id", null: false diff --git a/erd.svg b/erd.svg index dcd14fb..5bba33c 100644 --- a/erd.svg +++ b/erd.svg @@ -4,12 +4,12 @@ - + ProgressTest - -Progress Test - Models + +Progress Test - Models m_ActiveAdmin::Comment @@ -94,41 +94,41 @@ m_Axis - -Axis - -name -string ∗ U + +Axis + +name +string ∗ U - + m_Subject - -Subject - -name -string ∗ U + +Subject + +name +string ∗ U m_Axis->m_Subject - - + + m_Category - -Category - -name -string ∗ U + +Category + +name +string ∗ U m_Category->m_Subject - - + + @@ -144,111 +144,127 @@ m_Question - -Question - -alternatives -jsonb ∗ -authorship -string -authorship_year -string -bloom_taxonomy -string -body -text -check_type -string -difficulty -string -explanation -text -instruction -text -intention -text -references -text -status -string ∗ -support -text + +Question + +alternatives +jsonb ∗ +authorship +string +authorship_year +string +bloom_taxonomy +string +body +text +check_type +string +difficulty +string +explanation +text +instruction +text +intention +text +references +text +status +string ∗ +support +text m_ReviewMessage - -ReviewMessage - -content -string ∗ -feedback_type -string + +ReviewMessage + +content +string ∗ +feedback_type +string m_Question->m_ReviewMessage - - + + m_ReviewRequest - -ReviewRequest - -answered -string + +ReviewRequest + +answered +string m_Question->m_ReviewRequest - - + + + + + +m_Role + +Role + +name +string ∗ U + + + +m_User + +User + +email +string ∗ U +encrypted_password +string ∗ +name +string ∗ +remember_created_at +datetime (6,0) +reset_password_sent_at +datetime (6,0) +reset_password_token +string + + + +m_Role->m_User + + + m_Subject->m_Question - - - - - -m_User - -User - -email -string ∗ U -encrypted_password -string ∗ -name -string ∗ -remember_created_at -datetime (6,0) -reset_password_sent_at -datetime (6,0) -reset_password_token -string + + m_User->m_Question - - + + m_User->m_ReviewMessage - - + + m_User->m_ReviewRequest - - - + + + diff --git a/spec/factories/roles.rb b/spec/factories/roles.rb new file mode 100644 index 0000000..d06efc0 --- /dev/null +++ b/spec/factories/roles.rb @@ -0,0 +1,18 @@ +# == Schema Information +# +# Table name: roles +# +# id :bigint not null, primary key +# name :string +# created_at :datetime not null +# updated_at :datetime not null +# +# Indexes +# +# index_roles_on_name (name) UNIQUE +# +FactoryBot.define do + factory :role do + + end +end diff --git a/spec/models/role_spec.rb b/spec/models/role_spec.rb new file mode 100644 index 0000000..6f65c40 --- /dev/null +++ b/spec/models/role_spec.rb @@ -0,0 +1,18 @@ +# == Schema Information +# +# Table name: roles +# +# id :bigint not null, primary key +# name :string +# created_at :datetime not null +# updated_at :datetime not null +# +# Indexes +# +# index_roles_on_name (name) UNIQUE +# +require 'rails_helper' + +RSpec.describe Role, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end