-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CRM457-2018: Migrate roles to a has-many relationship (#811)
## Description of change Migrate roles to a has-many relationship so that we can accommodate things like some users having edit but not analytics access, some having analytics but not edit, and some having both. [Link to relevant ticket](https://dsdmoj.atlassian.net/browse/CRM457-2018)
- Loading branch information
1 parent
d866928
commit 538302b
Showing
11 changed files
with
86 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class Role < ApplicationRecord | ||
ROLE_TYPES = [ | ||
CASEWORKER = 'caseworker'.freeze, | ||
SUPERVISOR = 'supervisor'.freeze, | ||
VIEWER = 'viewer'.freeze | ||
].freeze | ||
|
||
belongs_to :user | ||
|
||
validates :role_type, inclusion: { in: ROLE_TYPES } | ||
|
||
scope :caseworker, -> { where(role_type: CASEWORKER) } | ||
scope :supervisor, -> { where(role_type: SUPERVISOR) } | ||
scope :viewer, -> { where(role_type: VIEWER) } | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
class CreateRoles < ActiveRecord::Migration[7.1] | ||
def up | ||
create_table :roles do |t| | ||
t.references :user, null: false, foreign_key: true, type: :uuid | ||
t.string :role_type | ||
|
||
t.timestamps | ||
end | ||
|
||
User.find_each do |user| | ||
user.roles.create! role_type: user.role | ||
end | ||
|
||
remove_column :users, :role, :string | ||
end | ||
|
||
def down | ||
add_column :users, :role, :string | ||
|
||
User.find_each do |user| | ||
user.update!(role: user.roles.first.role_type) | ||
end | ||
|
||
drop_table :roles | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
FactoryBot.define do | ||
factory :role do | ||
user { nil } | ||
|
||
trait :supervisor do | ||
role_type { 'supervisor' } | ||
end | ||
|
||
trait :caseworker do | ||
role_type { 'caseworker' } | ||
end | ||
|
||
trait :viewer do | ||
role_type { 'viewer' } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters