Commit b7a8c1d
Changed files (7)
app
controllers
views
api
config
spec
controllers
support
app/controllers/api/controller.rb
@@ -2,4 +2,10 @@ class Api::Controller < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :null_session
+
+ def current_session
+ end
+
+ def current_user
+ end
end
app/controllers/api/workouts_controller.rb
@@ -0,0 +1,5 @@
+class Api::WorkoutsController < Api::Controller
+ def index
+ @workouts = current_user.workouts
+ end
+end
app/views/api/workouts/_workout.json.jbuilder
@@ -0,0 +1,3 @@
+json.id workout.id
+json.body_weight workout.body_weight
+json.routine_name workout.routine.name
app/views/api/workouts/index.json.jbuilder
@@ -0,0 +1,3 @@
+json.workouts @workouts do |workout|
+ json.partial! 'workout', workout: workout
+end
config/routes.rb
@@ -17,5 +17,6 @@ Rails.application.routes.draw do
namespace :api, defaults: { format: 'json' } do
resources :sessions, only: [:create]
+ resources :workouts, only: [:index]
end
end
spec/controllers/api/workouts_controller_spec.rb
@@ -0,0 +1,22 @@
+require "rails_helper"
+
+describe Api::WorkoutsController do
+ describe "#index" do
+ render_views
+ let(:user) { create(:user) }
+
+ before :each do
+ api_login(user)
+ end
+
+ it "returns each workout" do
+ workout = create(:workout, user: user)
+
+ get :index, format: :json
+
+ expect(response).to have_http_status(:ok)
+ json = JSON.parse(response.body, symbolize_names: true)
+ expect(json[:workouts].count).to eql(1)
+ end
+ end
+end
spec/support/http_authentication.rb
@@ -5,4 +5,8 @@ module HttpAuthentication
allow(controller).to receive(:current_session).and_return(user_session)
session[:user_id] = user_session.id
end
+
+ def api_login(user)
+ http_login(user)
+ end
end