Skip to content
This repository was archived by the owner on Dec 3, 2019. It is now read-only.
1 change: 1 addition & 0 deletions app/controllers/api/v1/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ def user_params
:company_name,
:education_level,
:scholarship_info,
:role_id,
interests: []
)
end
Expand Down
5 changes: 5 additions & 0 deletions app/models/role.rb
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
10 changes: 8 additions & 2 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,18 @@ def has_tag?(tag)
tag_list.include? tag
end

def role
return if role_id.blank?

Role.find_by id: role_id
end

private

def zip_code_exists
return if longitude && latitude
return if longitude && latitude

errors.add(:zip_code, 'not found')
errors.add(:zip_code, 'not found')
end

def upcase_state
Expand Down
1 change: 0 additions & 1 deletion app/policies/request_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,4 @@ def index?
def show?
user.mentor?
end

end
5 changes: 5 additions & 0 deletions app/serializers/user_serializer.rb
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

Copy link
Copy Markdown
Collaborator

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?

@hpjaj hpjaj Apr 7, 2018 •

Copy link
Copy Markdown
Collaborator Author

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, role is an association`.

So if it is left in the long list, it would return an object of the whole associated role, in the role key.

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 the title).


def role
object.role&.title
end
end
11 changes: 11 additions & 0 deletions db/migrate/20180401034441_create_roles.rb
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
10 changes: 9 additions & 1 deletion 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.define(version: 20180204235116) do
ActiveRecord::Schema.define(version: 20180401034441) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -152,6 +152,12 @@
t.datetime "updated_at", null: false
end

create_table "roles", force: :cascade do |t|
t.string "title"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "scholarship_applications", force: :cascade do |t|
t.text "reason"
t.boolean "terms_accepted"
Expand Down Expand Up @@ -258,8 +264,10 @@
t.string "education_level"
t.string "interests"
t.boolean "scholarship_info", default: false
t.integer "role_id"
t.index ["email"], name: "index_users_on_email", unique: true, using: :btree
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree
t.index ["role_id"], name: "index_users_on_role_id", using: :btree
end

create_table "votes", force: :cascade do |t|
Expand Down
14 changes: 14 additions & 0 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
User.destroy_all
Service.destroy_all
TeamMember.destroy_all
AdminUser.destroy_all

FactoryGirl.create(:user)
FactoryGirl.create(:user)
Expand All @@ -32,3 +33,16 @@

# Create Admin (development only)
AdminUser.create!(email: 'admin@example.com', password: 'password', password_confirmation: 'password') if Rails.env.development?

users = User.count
requests = Request.count
services = Service.count
team_members = TeamMember.count
admin_users = AdminUser.count

puts 'Seeding complete. Created:'
p "#{users} users"
p "#{requests} requests"
p "#{services} services"
p "#{team_members} team members"
p "#{admin_users} admin users"
12 changes: 12 additions & 0 deletions lib/tasks/roles.rake
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.

5 changes: 5 additions & 0 deletions test/factories/roles.rb
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
20 changes: 20 additions & 0 deletions test/models/role_test.rb
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'

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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?
duplicate_role = create :role, title: 'standard'
refute duplicate_role.valid?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question. If you were to use create it would error silently, b/c validations are not met, and the test would fail.

By calling build then save, you are able to check for errors and validity, and the test continues to run its course.

duplicate_role.save

refute duplicate_role.valid?
assert duplicate_role.errors.full_messages.include? "Title has already been taken"
end
end
10 changes: 10 additions & 0 deletions test/models/user_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ class UserTest < ActiveSupport::TestCase
assert_equal 'update_email@example.com', u.email
end

test "role gets the Role with an id of user#role_id, if there is one" do
user = create :user
assert user.valid?
assert user.role.nil?

some_role = create :role
user.update! role_id: some_role.id
assert user.role == some_role
end

test 'doesnt geocode until we save' do
u = build(:user, latitude: nil, longitude: nil)
assert u.valid?
Expand Down