Repository navigation
Adds roles for varied access #290
Changes from all commits
c21be9d
30dfffe
79679de
81f520e
1c97fa6
79c8619
d63a26a
d85c8e8
6228db4
8246f28
065b5b7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -79,6 +79,7 @@ def user_params | |
| :company_name, | ||
| :education_level, | ||
| :scholarship_info, | ||
| :role_id, | ||
| interests: [] | ||
| ) | ||
| end | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| class Role < ApplicationRecord | ||
| has_many :users | ||
|
|
||
| validates :title, presence: true, uniqueness: true | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,5 +13,4 @@ def index? | |
| def show? | ||
| user.mentor? | ||
| end | ||
|
|
||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,9 @@ | ||
| class UserSerializer < ActiveModel::Serializer | ||
| attributes :id, :email, :zip, :first_name, :last_name, | ||
| :slack_name, :mentor, :verified | ||
| attributes :role | ||
|
|
||
| def role | ||
| object.role&.title | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| class CreateRoles < ActiveRecord::Migration[5.0] | ||
| def change | ||
| create_table :roles do |t| | ||
| t.string :title | ||
|
|
||
| t.timestamps | ||
| end | ||
|
|
||
| add_reference :users, :role, index: true | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| namespace :roles do | ||
| desc "Creates the initial set of roles" | ||
| task initial_set: :environment do | ||
| ['super_admin', 'admin', 'board_member'].each do |role| | ||
| begin | ||
| Role.find_or_create_by!(title: role) | ||
| rescue ActiveRecord::RecordInvalid => e | ||
| p e.record.errors | ||
| end | ||
| end | ||
| end | ||
| end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| FactoryGirl.define do | ||
| factory :role do | ||
| title "standard_user" | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| require 'test_helper' | ||
|
|
||
| class RoleTest < ActiveSupport::TestCase | ||
| test 'role requires a title' do | ||
| role = build(:role, title: '') | ||
|
|
||
| refute role.valid? | ||
| end | ||
|
|
||
| test 'title must be unique' do | ||
| standard_role = create :role, title: 'standard' | ||
| assert standard_role.valid? | ||
|
|
||
| duplicate_role = build :role, title: 'standard' | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of using build and then save, can you do this in one step and use create? Is there a benefit to doing one over the other?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good question. If you were to use By calling |
||
| duplicate_role.save | ||
|
|
||
| refute duplicate_role.valid? | ||
| assert duplicate_role.errors.full_messages.include? "Title has already been taken" | ||
| end | ||
| end | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why isn't :role added to the attributes already listed?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question. With
ActiveModel::Serializer, if the attribute is listed in the long list of attributes, it will directly call the method from the model. In this case,roleis an association`.So if it is left in the long list, it would return an object of the whole associated role, in the
rolekey.In this case, we just want the associated role's
title. To do that, you create a method with the desired name (i.e.role), and have that method return whatever you want (i.e. just thetitle).