diff --git a/app/controllers/api/v1/users_controller.rb b/app/controllers/api/v1/users_controller.rb index 0b6607ad..267a59ae 100644 --- a/app/controllers/api/v1/users_controller.rb +++ b/app/controllers/api/v1/users_controller.rb @@ -79,6 +79,7 @@ def user_params :company_name, :education_level, :scholarship_info, + :role_id, interests: [] ) end diff --git a/app/models/role.rb b/app/models/role.rb new file mode 100644 index 00000000..e8677c20 --- /dev/null +++ b/app/models/role.rb @@ -0,0 +1,5 @@ +class Role < ApplicationRecord + has_many :users + + validates :title, presence: true, uniqueness: true +end diff --git a/app/models/user.rb b/app/models/user.rb index 7ef49771..6249f795 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 diff --git a/app/policies/request_policy.rb b/app/policies/request_policy.rb index 0fa6b3b0..8fbd6b74 100644 --- a/app/policies/request_policy.rb +++ b/app/policies/request_policy.rb @@ -13,5 +13,4 @@ def index? def show? user.mentor? end - end diff --git a/app/serializers/user_serializer.rb b/app/serializers/user_serializer.rb index d6cdcf69..23ddbfff 100644 --- a/app/serializers/user_serializer.rb +++ b/app/serializers/user_serializer.rb @@ -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 diff --git a/db/migrate/20180401034441_create_roles.rb b/db/migrate/20180401034441_create_roles.rb new file mode 100644 index 00000000..45748efc --- /dev/null +++ b/db/migrate/20180401034441_create_roles.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index b5e8abd1..dde05ddc 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.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" @@ -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" @@ -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| diff --git a/db/seeds.rb b/db/seeds.rb index fe867a86..59113101 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -10,6 +10,7 @@ User.destroy_all Service.destroy_all TeamMember.destroy_all +AdminUser.destroy_all FactoryGirl.create(:user) FactoryGirl.create(:user) @@ -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" diff --git a/lib/tasks/roles.rake b/lib/tasks/roles.rake new file mode 100644 index 00000000..b8a7225e --- /dev/null +++ b/lib/tasks/roles.rake @@ -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 diff --git a/test/cassettes/user_test/role_gets_the_role_with_an_id_of_user_role_id_if_there_is_one.yml b/test/cassettes/user_test/role_gets_the_role_with_an_id_of_user_role_id_if_there_is_one.yml new file mode 100644 index 00000000..29f0cd57 --- /dev/null +++ b/test/cassettes/user_test/role_gets_the_role_with_an_id_of_user_role_id_if_there_is_one.yml @@ -0,0 +1,113 @@ +--- +http_interactions: +- request: + method: get + uri: http://maps.googleapis.com/maps/api/geocode/json?address=97201&language=en&sensor=false + body: + encoding: US-ASCII + string: '' + headers: + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: + - Ruby + response: + status: + code: 200 + message: OK + headers: + Content-Type: + - application/json; charset=UTF-8 + Date: + - Sat, 07 Apr 2018 19:35:47 GMT + Expires: + - Sun, 08 Apr 2018 19:35:47 GMT + Cache-Control: + - public, max-age=86400 + Access-Control-Allow-Origin: + - "*" + Server: + - mafe + Content-Length: + - '495' + X-Xss-Protection: + - 1; mode=block + X-Frame-Options: + - SAMEORIGIN + body: + encoding: ASCII-8BIT + string: | + { + "results" : [ + { + "address_components" : [ + { + "long_name" : "97201", + "short_name" : "97201", + "types" : [ "postal_code" ] + }, + { + "long_name" : "Southwest Portland", + "short_name" : "Southwest Portland", + "types" : [ "neighborhood", "political" ] + }, + { + "long_name" : "Portland", + "short_name" : "Portland", + "types" : [ "locality", "political" ] + }, + { + "long_name" : "Multnomah County", + "short_name" : "Multnomah County", + "types" : [ "administrative_area_level_2", "political" ] + }, + { + "long_name" : "Oregon", + "short_name" : "OR", + "types" : [ "administrative_area_level_1", "political" ] + }, + { + "long_name" : "United States", + "short_name" : "US", + "types" : [ "country", "political" ] + } + ], + "formatted_address" : "Portland, OR 97201, USA", + "geometry" : { + "bounds" : { + "northeast" : { + "lat" : 45.519965, + "lng" : -122.665103 + }, + "southwest" : { + "lat" : 45.497957, + "lng" : -122.7193571 + } + }, + "location" : { + "lat" : 45.505603, + "lng" : -122.6882145 + }, + "location_type" : "APPROXIMATE", + "viewport" : { + "northeast" : { + "lat" : 45.519965, + "lng" : -122.665103 + }, + "southwest" : { + "lat" : 45.497957, + "lng" : -122.7193571 + } + } + }, + "place_id" : "ChIJi8MjqD8KlVQRsyr5ZtaBLMI", + "types" : [ "postal_code" ] + } + ], + "status" : "OK" + } + http_version: + recorded_at: Sat, 07 Apr 2018 19:35:47 GMT +recorded_with: VCR 3.0.3 diff --git a/test/factories/roles.rb b/test/factories/roles.rb new file mode 100644 index 00000000..970404c4 --- /dev/null +++ b/test/factories/roles.rb @@ -0,0 +1,5 @@ +FactoryGirl.define do + factory :role do + title "standard_user" + end +end diff --git a/test/models/role_test.rb b/test/models/role_test.rb new file mode 100644 index 00000000..3fb99689 --- /dev/null +++ b/test/models/role_test.rb @@ -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' + duplicate_role.save + + refute duplicate_role.valid? + assert duplicate_role.errors.full_messages.include? "Title has already been taken" + end +end diff --git a/test/models/user_test.rb b/test/models/user_test.rb index 4f6b6426..572e06c8 100644 --- a/test/models/user_test.rb +++ b/test/models/user_test.rb @@ -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?