Commit 44c11c4d

mo khan <mo@mokhan.ca>
2014-11-22 16:07:46
add tutorials to api v2.
1 parent e64d486
Changed files (5)
app
controllers
views
api
config
spec
app/controllers/api/v2/tutorials_controller.rb
@@ -0,0 +1,9 @@
+module Api
+  module V2
+    class TutorialsController < ApplicationController
+      def index
+        @tutorials = Tutorial.page(page).per(per_page)
+      end
+    end
+  end
+end
app/views/api/v2/tutorials/index.json.jbuilder
@@ -0,0 +1,7 @@
+json.tutorials @tutorials do |tutorial|
+  json.heading tutorial.heading
+  json.description tutorial.description
+  json.url tutorial.url
+  json.image_url tutorial.image_url
+  json.submitter tutorial.user.id
+end
config/routes.rb
@@ -72,6 +72,7 @@ Cake::Application.routes.draw do
       resources :photos, only: [:index, :show]
       resources :users, only: [:index, :show]
       resources :categories, only: [:index, :show]
+      resources :tutorials, only: [:index]
     end
   end
 
spec/controllers/api/v2/tutorials_controller_spec.rb
@@ -0,0 +1,30 @@
+require 'rails_helper'
+
+module Api
+  module V2
+    describe TutorialsController do
+      render_views
+      describe "#index" do
+        let!(:tutorial) { create(:tutorial) }
+
+        before :each do
+          xhr :get, :index
+        end
+
+        it 'loads all the tutorials' do
+          expect(assigns(:tutorials)).to match_array([tutorial])
+        end
+
+        it 'responds with json' do
+          json = JSON.parse(response.body)
+          expect(json['tutorials'].count).to eql(1)
+          expect(json['tutorials'][0]['heading']).to eql(tutorial.heading)
+          expect(json['tutorials'][0]['description']).to eql(tutorial.description)
+          expect(json['tutorials'][0]['url']).to eql(tutorial.url)
+          expect(json['tutorials'][0]['submitter']).to eql(tutorial.user.id)
+          expect(json['tutorials'][0]['image_url']).to eql(tutorial.image_url)
+        end
+      end
+    end
+  end
+end
spec/factories.rb
@@ -56,6 +56,7 @@ FactoryGirl.define do
     image_url { Faker::Internet.http_url }
     author { Faker::Name.name }
     author_url { Faker::Internet.http_url }
+    association :user
   end
 
   factory :user, class: User do