-
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
7 changed files
with
112 additions
and
1 deletion.
There are no files selected for viewing
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::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 |
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,7 @@ | ||
class DayHabit < RankedEnum | ||
class << self | ||
def all_ids | ||
%w( lying_down sitting_down on_feet driving working_with_hands ) | ||
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,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 |
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
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
46 changes: 46 additions & 0 deletions
46
backend/spec/controllers/api/v1/day_habits_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,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 |