diff options
| author | mo khan <mo@mokhan.ca> | 2014-11-22 09:07:46 -0700 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2014-11-22 09:07:46 -0700 |
| commit | 44c11c4d9cc2a0e0d5e11c2abe5682d23875e656 (patch) | |
| tree | bf327277f1a2394e3888f300877ff14fbe29af78 | |
| parent | e64d48698e3d0143be1a345846aaa96969e0efeb (diff) | |
add tutorials to api v2.
| -rw-r--r-- | app/controllers/api/v2/tutorials_controller.rb | 9 | ||||
| -rw-r--r-- | app/views/api/v2/tutorials/index.json.jbuilder | 7 | ||||
| -rw-r--r-- | config/routes.rb | 1 | ||||
| -rw-r--r-- | spec/controllers/api/v2/tutorials_controller_spec.rb | 30 | ||||
| -rw-r--r-- | spec/factories.rb | 1 |
5 files changed, 48 insertions, 0 deletions
diff --git a/app/controllers/api/v2/tutorials_controller.rb b/app/controllers/api/v2/tutorials_controller.rb new file mode 100644 index 00000000..6b4279b6 --- /dev/null +++ b/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 diff --git a/app/views/api/v2/tutorials/index.json.jbuilder b/app/views/api/v2/tutorials/index.json.jbuilder new file mode 100644 index 00000000..b9e7ee36 --- /dev/null +++ b/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 diff --git a/config/routes.rb b/config/routes.rb index 1d1782a8..5a8c19fa 100644 --- a/config/routes.rb +++ b/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 diff --git a/spec/controllers/api/v2/tutorials_controller_spec.rb b/spec/controllers/api/v2/tutorials_controller_spec.rb new file mode 100644 index 00000000..46077b2d --- /dev/null +++ b/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 diff --git a/spec/factories.rb b/spec/factories.rb index 272f2618..2f4acaee 100644 --- a/spec/factories.rb +++ b/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 |
