-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
backend/app/controllers/api/v1/education_levels_controller.rb
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,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 |
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,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 | ||
|
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,2 @@ | ||
class EducationLevelSerializer < RankedEnumSerializer | ||
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
43 changes: 43 additions & 0 deletions
43
backend/spec/controllers/api/v1/education_levels_controller_spec.rb
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,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 |