From c636f067db0f6918872193c8feee86b6ed5dc417 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Fri, 23 Jan 2015 19:45:41 -0500 Subject: Generate Note model --- app/models/note.rb | 2 ++ db/migrate/20150124004310_create_notes.rb | 10 ++++++++++ test/fixtures/notes.yml | 9 +++++++++ test/models/note_test.rb | 7 +++++++ 4 files changed, 28 insertions(+) create mode 100644 app/models/note.rb create mode 100644 db/migrate/20150124004310_create_notes.rb create mode 100644 test/fixtures/notes.yml create mode 100644 test/models/note_test.rb diff --git a/app/models/note.rb b/app/models/note.rb new file mode 100644 index 0000000..1b03bd6 --- /dev/null +++ b/app/models/note.rb @@ -0,0 +1,2 @@ +class Note < ActiveRecord::Base +end diff --git a/db/migrate/20150124004310_create_notes.rb b/db/migrate/20150124004310_create_notes.rb new file mode 100644 index 0000000..41fe6cb --- /dev/null +++ b/db/migrate/20150124004310_create_notes.rb @@ -0,0 +1,10 @@ +class CreateNotes < ActiveRecord::Migration + def change + create_table :notes do |t| + t.string :title + t.text :body + + t.timestamps null: false + end + end +end diff --git a/test/fixtures/notes.yml b/test/fixtures/notes.yml new file mode 100644 index 0000000..e192dee --- /dev/null +++ b/test/fixtures/notes.yml @@ -0,0 +1,9 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + title: MyString + body: MyText + +two: + title: MyString + body: MyText diff --git a/test/models/note_test.rb b/test/models/note_test.rb new file mode 100644 index 0000000..7bbab53 --- /dev/null +++ b/test/models/note_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class NoteTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end -- cgit v1.2.3 From b968245150055582423262699c2e9ba290fd70fb Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Fri, 23 Jan 2015 19:46:21 -0500 Subject: Migrate Note Initial Note migration --- db/schema.rb | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 db/schema.rb diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 0000000..30e0d1a --- /dev/null +++ b/db/schema.rb @@ -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: 20150124004310) do + + create_table "notes", force: :cascade do |t| + t.string "title" + t.text "body" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + +end -- cgit v1.2.3 From 1d8d8322f0595dcf13f65e13c6ad4ec4bb2e410f Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Fri, 23 Jan 2015 19:47:16 -0500 Subject: Generate NotesController --- app/assets/javascripts/notes.coffee | 3 +++ app/assets/stylesheets/notes.scss | 3 +++ app/controllers/notes_controller.rb | 2 ++ app/helpers/notes_helper.rb | 2 ++ test/controllers/notes_controller_test.rb | 7 +++++++ 5 files changed, 17 insertions(+) create mode 100644 app/assets/javascripts/notes.coffee create mode 100644 app/assets/stylesheets/notes.scss create mode 100644 app/controllers/notes_controller.rb create mode 100644 app/helpers/notes_helper.rb create mode 100644 test/controllers/notes_controller_test.rb diff --git a/app/assets/javascripts/notes.coffee b/app/assets/javascripts/notes.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/app/assets/javascripts/notes.coffee @@ -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/ diff --git a/app/assets/stylesheets/notes.scss b/app/assets/stylesheets/notes.scss new file mode 100644 index 0000000..56d31f5 --- /dev/null +++ b/app/assets/stylesheets/notes.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the notes controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/notes_controller.rb b/app/controllers/notes_controller.rb new file mode 100644 index 0000000..c94d2e3 --- /dev/null +++ b/app/controllers/notes_controller.rb @@ -0,0 +1,2 @@ +class NotesController < ApplicationController +end diff --git a/app/helpers/notes_helper.rb b/app/helpers/notes_helper.rb new file mode 100644 index 0000000..8078f73 --- /dev/null +++ b/app/helpers/notes_helper.rb @@ -0,0 +1,2 @@ +module NotesHelper +end diff --git a/test/controllers/notes_controller_test.rb b/test/controllers/notes_controller_test.rb new file mode 100644 index 0000000..2a3800c --- /dev/null +++ b/test/controllers/notes_controller_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class NotesControllerTest < ActionController::TestCase + # test "the truth" do + # assert true + # end +end -- cgit v1.2.3 From 3ea39ca725e28f83fbea4a6ebaa037697cb8dde9 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Fri, 23 Jan 2015 20:53:19 -0500 Subject: Add actions to NotesController * Create routes for notes actions * Add actions to NotesController allowing us to interact with it through a JSON API --- app/controllers/notes_controller.rb | 32 +++++++++++++++++++++ config/routes.rb | 55 +------------------------------------ 2 files changed, 33 insertions(+), 54 deletions(-) diff --git a/app/controllers/notes_controller.rb b/app/controllers/notes_controller.rb index c94d2e3..7b9e4eb 100644 --- a/app/controllers/notes_controller.rb +++ b/app/controllers/notes_controller.rb @@ -1,2 +1,34 @@ class NotesController < ApplicationController + protect_from_forgery with: :null_session + + def index + render :json => Note.all.order(:created_at => :desc) + end + + def show + render :json => Note.find(params[:id]) + end + + def create + note = Note.create(note_params) + render :json => note + end + + def update + note = Note.find(params[:id]) + note.update(note_params) + render :json => note + end + + def destroy + note = Note.find(params[:id]) + note.destroy + render :json => note + end + + private + + def note_params + params.require(:note).permit(:title, :body) + end end diff --git a/config/routes.rb b/config/routes.rb index 3f66539..c5c7483 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,56 +1,3 @@ Rails.application.routes.draw do - # The priority is based upon order of creation: first created -> highest priority. - # See how all your routes lay out with "rake routes". - - # You can have the root of your site routed with "root" - # root 'welcome#index' - - # Example of regular route: - # get 'products/:id' => 'catalog#view' - - # Example of named route that can be invoked with purchase_url(id: product.id) - # get 'products/:id/purchase' => 'catalog#purchase', as: :purchase - - # Example resource route (maps HTTP verbs to controller actions automatically): - # resources :products - - # Example resource route with options: - # resources :products do - # member do - # get 'short' - # post 'toggle' - # end - # - # collection do - # get 'sold' - # end - # end - - # Example resource route with sub-resources: - # resources :products do - # resources :comments, :sales - # resource :seller - # end - - # Example resource route with more complex sub-resources: - # resources :products do - # resources :comments - # resources :sales do - # get 'recent', on: :collection - # end - # end - - # Example resource route with concerns: - # concern :toggleable do - # post 'toggle' - # end - # resources :posts, concerns: :toggleable - # resources :photos, concerns: :toggleable - - # Example resource route within a namespace: - # namespace :admin do - # # Directs /admin/products/* to Admin::ProductsController - # # (app/controllers/admin/products_controller.rb) - # resources :products - # end + resources :notes, :except => [:new, :edit] end -- cgit v1.2.3