Skip to content

Commit

Permalink
Adding in Roles
Browse files Browse the repository at this point in the history
  • Loading branch information
alan-at-work committed Oct 22, 2024
1 parent e195997 commit 43f0350
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 1 deletion.
2 changes: 2 additions & 0 deletions cosmetics-web/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,5 @@ gem "database_cleaner-active_record"

gem "graphiql-rails", group: :development
gem "graphql", "~> 2.3"

gem "rolify"
2 changes: 2 additions & 0 deletions cosmetics-web/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,7 @@ GEM
reverse_markdown (2.1.1)
nokogiri
rexml (3.3.8)
rolify (6.0.1)
rotp (6.3.0)
rqrcode (2.2.0)
chunky_png (~> 1.0)
Expand Down Expand Up @@ -690,6 +691,7 @@ DEPENDENCIES
redis (~> 5.2)
request_store (~> 1.6)
rest-client (~> 2.1)
rolify
rotp (~> 6.2)
rqrcode (~> 2.1)
rspec-collection_matchers
Expand Down
13 changes: 13 additions & 0 deletions cosmetics-web/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
1 change: 1 addition & 0 deletions cosmetics-web/app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class User < ApplicationRecord
rolify
NAME_MAX_LENGTH = 50
NEW_EMAIL_TOKEN_VALID_FOR = 600 # 10 minutes
SECONDARY_AUTHENTICATION_METHODS = %w[app sms].freeze
Expand Down
10 changes: 10 additions & 0 deletions cosmetics-web/config/initializers/rolify.rb
Original file line number Diff line number Diff line change
@@ -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
18 changes: 18 additions & 0 deletions cosmetics-web/db/migrate/20241021154308_rolify_create_roles.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
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, %i[name resource_type resource_id])
add_index(:users_roles, %i[user_id role_id])
end
end
20 changes: 19 additions & 1 deletion cosmetics-web/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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_10_08_153814) do
ActiveRecord::Schema[7.1].define(version: 2024_10_21_154308) do
# These are extensions that must be enabled in order to support this database
enable_extension "citext"
enable_extension "pg_trgm"
Expand Down Expand Up @@ -312,6 +312,16 @@
t.datetime "updated_at", precision: nil, null: false
end

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 ["resource_type", "resource_id"], name: "index_roles_on_resource"
end

create_table "search_histories", force: :cascade do |t|
t.string "query"
t.integer "results"
Expand Down Expand Up @@ -396,6 +406,14 @@
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

create_table "versions", force: :cascade do |t|
t.string "item_type", null: false
t.bigint "item_id", null: false
Expand Down
4 changes: 4 additions & 0 deletions cosmetics-web/spec/factories/roles.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FactoryBot.define do
factory :role do
end
end
5 changes: 5 additions & 0 deletions cosmetics-web/spec/models/role_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require "rails_helper"

RSpec.describe Role, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end

0 comments on commit 43f0350

Please sign in to comment.