Skip to content

Commit

Permalink
EducationLevel model and APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
algodave committed Feb 24, 2016
1 parent 683f8c0 commit 69f1b2d
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 0 deletions.
18 changes: 18 additions & 0 deletions backend/app/controllers/api/v1/education_levels_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Api::V1::EducationLevelsController < Api::BaseController
def index
render json: EducationLevel.all
end

def show
education_level = EducationLevel.find(education_level_id)
render json: education_level
end

private

def education_level_id
id = params.require(:id)
fail ActionController::BadRequest.new('id param is not a valid education_level id') unless EducationLevel.all_ids.include?(id)
id
end
end
8 changes: 8 additions & 0 deletions backend/app/models/education_level.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class EducationLevel < RankedEnum
class << self
def all_ids
%w( less_than_hi_school hi_school college_degree bachelors_degree advanced_degree )
end
end
end

2 changes: 2 additions & 0 deletions backend/app/serializers/education_level_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class EducationLevelSerializer < RankedEnumSerializer
end
7 changes: 7 additions & 0 deletions backend/config/locales/en.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
en:

education_level:
less_than_hi_school: "Less than high school"
hi_school: "High school diploma"
college_degree: "Some college or associate's degree"
bachelors_degree: "Bachelor's degree"
advanced_degree: "Advanced degree"

ethnicity:
latino: "Latino or Hispanic"
white: "European or White"
Expand Down
4 changes: 4 additions & 0 deletions backend/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
resources :conditions, only: [:index, :show, :create]

#
# Education Levels
#
resources :education_levels, only: [:index, :show]

#
# Ethnicities
#
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require 'rails_helper'

RSpec.describe Api::V1::EducationLevelsController do
let(:test_education_level_id) { 'bachelors_degree' }
let(:test_education_level_name_en) { "Bachelor's degree" }

describe 'index' do
let(:all_education_levels_count) { EducationLevel.all.count }
it 'returns all education_levels' do
get :index
expect(response_body[:education_levels].size).to eq all_education_levels_count
end
context "with 'en' locale" do
before { I18n.default_locale = 'en' }
it "returns 'en' translations" do
get :index
test_education_level = response_body[:education_levels].find { |c| c[:id].eql?(test_education_level_id) }
expect(test_education_level[:name]).to eq test_education_level_name_en
end
end
end

describe 'show' do
it 'returns test education_level' do
get :show, id: test_education_level_id
expect(response_body[:education_level][:id]).to eq test_education_level_id
end
context "with 'en' locale" do
before { I18n.default_locale = 'en' }
it "returns 'en' translation" do
get :show, id: test_education_level_id
expect(response_body[:education_level][:name]).to eq test_education_level_name_en
end
end
context 'with invalid education_level id' do
let(:invalid_education_level_id) { 'blah' }
it 'returns 400 (Bad Request)' do
get :show, id: invalid_education_level_id
expect(response.status).to eq 400
end
end
end
end

0 comments on commit 69f1b2d

Please sign in to comment.