Skip to content

Commit

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

def show
day_habit = DayHabit.find(day_habit_id)
render json: day_habit
end

private

def day_habit_id
id = params.require(:id)
fail ActionController::BadRequest.new('id param is not a valid day_habit id') unless DayHabit.all_ids.include?(id)
id
end
end
7 changes: 7 additions & 0 deletions backend/app/models/day_habit.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class DayHabit < RankedEnum
class << self
def all_ids
%w( lying_down sitting_down on_feet driving working_with_hands )
end
end
end
12 changes: 12 additions & 0 deletions backend/app/serializers/day_habit_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class DayHabitSerializer < RankedEnumSerializer
attributes :description

def name
I18n.t "#{key}.#{object.id}.name"
end

def description
I18n.t "#{key}.#{object.id}.description"
end

end
8 changes: 7 additions & 1 deletion backend/app/serializers/ranked_enum_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ class RankedEnumSerializer < ApplicationSerializer
attributes :id, :name, :rank

def name
I18n.t "#{object.class.name.underscore}.#{object.id}"
I18n.t "#{key}.#{object.id}"
end

protected

def key
object.class.name.underscore
end
end
17 changes: 17 additions & 0 deletions backend/config/locales/en.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
en:

day_habit:
lying_down:
name: "Lying down"
description: "bedridden"
sitting_down:
name: "Sitting down or at a computer"
description: "e.g. student, programmer, architect"
on_feet:
name: "On your feet"
description: "e.g. physician, retail salesperson, tour guide"
driving:
name: "Driving around"
description: "e.g. pharmaceutical representative, taxi driver, truck driver"
working_with_hands:
name: "Working with your hands"
description: "e.g. farmer, fishery worker, landscaper"

education_level:
less_than_hi_school: "Less than high school"
hi_school: "High school diploma"
Expand Down
5 changes: 5 additions & 0 deletions backend/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
#
resources :conditions, only: [:index, :show, :create]

#
# Day Habits
#
resources :day_habits, only: [:index, :show]

#
# Education Levels
#
Expand Down
46 changes: 46 additions & 0 deletions backend/spec/controllers/api/v1/day_habits_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
require 'rails_helper'

RSpec.describe Api::V1::DayHabitsController do
let(:test_day_habit_id) { 'lying_down' }
let(:test_day_habit_name_en) { 'Lying down' }
let(:test_day_habit_descr_en) { 'bedridden' }

describe 'index' do
let(:all_day_habits_count) { DayHabit.all.count }
it 'returns all day_habits' do
get :index
expect(response_body[:day_habits].size).to eq all_day_habits_count
end
context "with 'en' locale" do
before { I18n.default_locale = 'en' }
it "returns 'en' translations" do
get :index
test_day_habit = response_body[:day_habits].find { |c| c[:id].eql?(test_day_habit_id) }
expect(test_day_habit[:name]).to eq test_day_habit_name_en
expect(test_day_habit[:description]).to eq test_day_habit_descr_en
end
end
end

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

0 comments on commit 15531a6

Please sign in to comment.