Permalink
Browse files

Bonus Track: Replacing The Fixture Adapter with ActiveModelSerializer…

… Adapter
  • Loading branch information...
1 parent 47fa16f commit 743ce485571b2b39ebb5ee2d459d595b0d5b8465 @ursm committed Dec 3, 2013
@@ -11,4 +11,9 @@
window.Todos = Ember.Application.create()
-Todos.ApplicationAdapter = DS.FixtureAdapter.extend()
+Todos.ApplicationAdapter = DS.ActiveModelAdapter.extend
+ namespace: 'api'
+
+Todos.TodoSerializer = DS.ActiveModelSerializer.extend
+ attrs:
+ isCompleted: 'completed'
@@ -0,0 +1,30 @@
+class Api::TodosController < ApplicationController
+ skip_before_action :verify_authenticity_token
+
+ def index
+ render json: Todo.all
+ end
+
+ def create
+ render json: Todo.create!(todo_params)
+ end
+
+ def update
+ todo = Todo.find(params[:id])
+ todo.update! todo_params
+
+ render json: todo
+ end
+
+ def destroy
+ Todo.destroy params[:id]
+
+ head :ok
+ end
+
+ private
+
+ def todo_params
+ params.require(:todo).permit(:title, :completed)
+ end
+end
View
@@ -0,0 +1,2 @@
+class Todo < ActiveRecord::Base
+end
@@ -0,0 +1,3 @@
+class TodoSerializer < ActiveModel::Serializer
+ attributes :id, :title, :completed
+end
View
@@ -1,3 +1,7 @@
TodoMVC::Application.routes.draw do
root to: 'todo#show'
+
+ namespace :api do
+ resources :todos
+ end
end
@@ -0,0 +1,10 @@
+class CreateTodos < ActiveRecord::Migration
+ def change
+ create_table :todos do |t|
+ t.string :title, null: false
+ t.boolean :completed, null: false
+
+ t.timestamps
+ end
+ end
+end
View
@@ -0,0 +1,23 @@
+# encoding: UTF-8
+# This file is auto-generated from the current state of the database. Instead
+# of editing this file, please use the migrations feature of Active Record to
+# incrementally modify your database, and then regenerate this schema definition.
+#
+# Note that this schema.rb definition is the authoritative source for your
+# database schema. If you need to create the application database on another
+# system, you should be using db:schema:load, not running all the migrations
+# from scratch. The latter is a flawed and unsustainable approach (the more migrations
+# you'll amass, the slower it'll run and the greater likelihood for issues).
+#
+# It's strongly recommended that you check this file into your version control system.
+
+ActiveRecord::Schema.define(version: 20131203130831) do
+
+ create_table "todos", force: true do |t|
+ t.string "title", null: false
+ t.boolean "completed", null: false
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ end
+
+end

0 comments on commit 743ce48

Please sign in to comment.