Skip to content

Commit

Permalink
Added carbs
Browse files Browse the repository at this point in the history
Now I have another problem :/
  • Loading branch information
pikesley committed Jan 29, 2016
1 parent ec1c091 commit eb93591
Show file tree
Hide file tree
Showing 17 changed files with 221 additions and 5 deletions.
3 changes: 3 additions & 0 deletions app/assets/javascripts/carbohydrate_intake.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/carbohydrate_intake.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the CarbohydrateIntake controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
11 changes: 11 additions & 0 deletions app/controllers/carbohydrate_intakes_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CarbohydrateIntakesController < GenericController
private

def metrics_path
carbohydrate_intakes_path
end

def acceptable_params
params.require(:carbohydrate_intake).permit(:datetime, :weight, :description)
end
end
2 changes: 2 additions & 0 deletions app/helpers/carbohydrate_intake_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module CarbohydrateIntakeHelper
end
45 changes: 45 additions & 0 deletions app/views/carbohydrate_intakes/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<%= form_for @metric do |f| %>
<% if @metric.errors.any? %>
<div id="error_explanation">
<h2>
<%= pluralize(@metric.errors.count, "error") %> prohibited
this measurement from being saved:
</h2>
<ul>
<% @metric.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>

<p>
<%= f.label :datetime, 'Date and time' %><br>
<%= f.text_field :datetime, id: 'carbohydrate_intake_datetime', class: 'form-control' %>
</p>

<p>
<%= f.label :weight %><br>
<%= f.text_field :weight, class: 'form-control' %>
</p>

<p>
<%= f.label :description %><br>
<%= f.text_field :description, class: 'form-control' %>
</p>

<p>
<%= f.submit class: 'btn btn-success' %>

</p>
<% end %>

<script type="text/javascript">
$(function () {
$('#carbohydrate_intake_datetime').datetimepicker({
format: 'YYYY-MM-DD HH:mm',
defaultDate: moment()
});
});
</script>
12 changes: 12 additions & 0 deletions app/views/carbohydrate_intakes/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<h1>Carbs Intake</h1>

<%= render 'form' %>
<%= link_to 'Delete', carbohydrate_intake_path(@metric),
class: 'btn btn-warning',
method: :delete,
data: {
confirm: 'Are you sure?'
} %>
<%= link_to 'Back', carbohydrate_intakes_path, class: 'btn btn-default' %>
3 changes: 3 additions & 0 deletions app/views/carbohydrate_intakes/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<%= render partial: 'shared/index', locals: {
title: 'carbs'
} %>
5 changes: 5 additions & 0 deletions app/views/carbohydrate_intakes/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<h1>New Carbohydrate Intake</h1>

<%= render 'form' %>
<%= link_to 'Back', carbohydrate_intakes_path, class: 'btn btn-default' %>
4 changes: 4 additions & 0 deletions app/views/carbohydrate_intakes/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<h1>Datetime: <%= @metric.datetime %></h1>
<h2>Weight: <%= @metric.value %></h2>

<%= link_to 'Back', carbohydrate_intakes_path %>
1 change: 1 addition & 0 deletions app/views/shared/_nav.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<ul class='nav navbar-nav' role='tablist'>
<% {
'Glucose' => '/glucose',
'Carbs' => '/carbs',
'Meds' => '/meds'
}.each_pair do |label, url| %>
<li>
Expand Down
1 change: 1 addition & 0 deletions app/views/welcome/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
<%= link_to 'Glucose', glucose_measurements_path, class: 'btn btn-success' %>
<%= link_to 'Carbs', carbohydrate_intakes_path, class: 'btn btn-success' %>
<%= link_to 'Meds', medication_events_path, class: 'btn btn-success' %>
122 changes: 122 additions & 0 deletions spec/controllers/carbohydrate_intake_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
describe CarbohydrateIntakesController, type: :controller do
before :each do
sign_in
end

describe 'GET #index' do
it 'populates an array of measurements' do
carbohydrate_intake = create(:carbohydrate_intake)
get :index
expect(assigns(:metrics)).to eq [carbohydrate_intake]
end

it 'renders the #index view' do
get :index
expect(response).to render_template :index
end
end

describe 'GET #show' do
it 'assigns the requested measurement to @metric' do
carbohydrate_intake = create(:carbohydrate_intake)
get :show, id: carbohydrate_intake
expect(assigns(:metric)).to eq carbohydrate_intake
end

it 'renders the #show view' do
get :show, id: create(:carbohydrate_intake)
expect(response).to render_template :show
end
end

describe 'POST #create' do
context 'with valid attributes' do
it 'creates a new measurement' do
expect {
post :create, carbohydrate_intake: attributes_for(:carbohydrate_intake)
}.to change(CarbohydrateIntake, :count).by 1
end

it 'redirects to the index page' do
post :create, carbohydrate_intake: attributes_for(:carbohydrate_intake)
expect(response).to redirect_to carbohydrate_intakes_url
end
end

context 'with invalid attributes' do
it 'does not save the new contact' do
expect {
post :create, carbohydrate_intake: attributes_for(:invalid_carbohydrate_intake)
}.to_not change(CarbohydrateIntake, :count)
end

it 're-renders the #new view' do
post :create, carbohydrate_intake: attributes_for(:invalid_carbohydrate_intake)
expect(response).to render_template :new
end
end
end

describe 'PUT #update' do
let (:test_carbs) { create :carbohydrate_intake, datetime: '1974-06-15 12:00:00', weight: 50.0 }

context 'valid attributes' do
it 'locates the requested measurement' do
put :update, id: test_carbs, carbohydrate_intake: attributes_for(:carbohydrate_intake)
expect(assigns :metric).to eq test_carbs
end

it 'changes test_carbs` attributes' do
put :update, id: test_carbs,
carbohydrate_intake: attributes_for(:carbohydrate_intake,
datetime: '1975-06-15 13:00:00',
weight: 65)
test_carbs.reload
expect(test_carbs.datetime).to eq '1975-06-15 13:00:00'
expect(test_carbs.weight).to eq 65
end

it 'redirects to the index page' do
put :update, id: test_carbs, carbohydrate_intake: attributes_for(:glucose_measurement)
expect(assigns :metric).to eq test_carbs
expect(response).to redirect_to carbohydrate_intakes_url
end
end

context 'invalid attributes' do
it 'locates the requested measurement' do
put :update, id: test_carbs, carbohydrate_intake: attributes_for(:invalid_carbohydrate_intake)
expect(assigns(:metric)).to eq test_carbs
end

it 'does not change the test_carbs attributes' do
put :update, id: test_carbs,
carbohydrate_intake: attributes_for(:carbohydrate_intake,
datetime: nil,
weight: 40)
test_carbs.reload
expect(test_carbs.weight).to_not eq 40
expect(test_carbs.datetime).to eq '1974-06-15 12:00:00'
end

it 're-renders the #edit view' do
put :update, id: test_carbs, carbohydrate_intake: attributes_for(:invalid_carbohydrate_intake)
expect(response).to render_template :edit
end
end
end

describe 'DELETE #destroy' do
let! (:test_carbs) { create :carbohydrate_intake }
it 'deletes the carbs' do
expect {
delete :destroy, id: test_carbs
}.to change(CarbohydrateIntake, :count).by -1
end

it 'redirects to #index' do
delete :destroy, id: test_carbs
expect(response).to redirect_to carbohydrate_intakes_url
end
end
end
4 changes: 1 addition & 3 deletions spec/controllers/generic_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require 'rails_helper'

RSpec.describe GenericController, type: :controller do
describe GenericController, type: :controller do

end
2 changes: 1 addition & 1 deletion spec/controllers/glucose_measurements_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
expect(test_measurement.value).to eq 12.0
end

it 'redirects to the updated measurement' do
it 'redirects to the index page' do
put :update, id: test_measurement, glucose_measurement: attributes_for(:glucose_measurement)
expect(assigns :metric).to eq test_measurement
expect(response).to redirect_to glucose_measurements_url
Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/medication_events_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
expect(test_meds.amount).to eq 16.0
end

it 'redirects to the updated measurement' do
it 'redirects to the index page' do
put :update, id: test_meds, medication_event: attributes_for(:medication_event)
expect(assigns :metric).to eq test_meds
expect(response).to redirect_to medication_events_url
Expand Down
4 changes: 4 additions & 0 deletions spec/factories/carbohydrate_intakes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@
f.weight 70
f.description 'pasta bolognese'
end

factory :invalid_carbohydrate_intake, parent: :carbohydrate_intake do |f|
f.weight -10
end
end
2 changes: 2 additions & 0 deletions spec/helpers/carbohydrate_intake_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
describe CarbohydrateIntakeHelper, type: :helper do
end

0 comments on commit eb93591

Please sign in to comment.