From 22e9a27fb8a2f524820400a86def1b29d8033340 Mon Sep 17 00:00:00 2001 From: Neil Gehani Date: Tue, 17 Sep 2013 13:08:35 -0700 Subject: [PATCH] Add Pins Scaffold --- app/assets/javascripts/pins.js.coffee | 3 + app/controllers/pins_controller.rb | 74 ++++++++++++++++++++++++ app/helpers/pins_helper.rb | 2 + app/models/pin.rb | 5 ++ app/models/user.rb | 2 +- app/views/pins/_form.html.erb | 9 +++ app/views/pins/_pin.html.erb | 6 ++ app/views/pins/edit.html.erb | 6 ++ app/views/pins/index.html.erb | 20 +++++++ app/views/pins/index.json.jbuilder | 4 ++ app/views/pins/new.html.erb | 5 ++ app/views/pins/show.html.erb | 12 ++++ app/views/pins/show.json.jbuilder | 1 + config/routes.rb | 2 + db/migrate/20130917193701_create_pins.rb | 9 +++ db/schema.rb | 8 ++- test/controllers/pins_controller_test.rb | 49 ++++++++++++++++ test/fixtures/pins.yml | 7 +++ test/helpers/pins_helper_test.rb | 4 ++ test/models/pin_test.rb | 7 +++ 20 files changed, 233 insertions(+), 2 deletions(-) create mode 100644 app/assets/javascripts/pins.js.coffee create mode 100644 app/controllers/pins_controller.rb create mode 100644 app/helpers/pins_helper.rb create mode 100644 app/models/pin.rb create mode 100644 app/views/pins/_form.html.erb create mode 100644 app/views/pins/_pin.html.erb create mode 100644 app/views/pins/edit.html.erb create mode 100644 app/views/pins/index.html.erb create mode 100644 app/views/pins/index.json.jbuilder create mode 100644 app/views/pins/new.html.erb create mode 100644 app/views/pins/show.html.erb create mode 100644 app/views/pins/show.json.jbuilder create mode 100644 db/migrate/20130917193701_create_pins.rb create mode 100644 test/controllers/pins_controller_test.rb create mode 100644 test/fixtures/pins.yml create mode 100644 test/helpers/pins_helper_test.rb create mode 100644 test/models/pin_test.rb diff --git a/app/assets/javascripts/pins.js.coffee b/app/assets/javascripts/pins.js.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/app/assets/javascripts/pins.js.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/controllers/pins_controller.rb b/app/controllers/pins_controller.rb new file mode 100644 index 0000000..8404554 --- /dev/null +++ b/app/controllers/pins_controller.rb @@ -0,0 +1,74 @@ +class PinsController < ApplicationController + before_action :set_pin, only: [:show, :edit, :update, :destroy] + + # GET /pins + # GET /pins.json + def index + @pins = Pin.all + end + + # GET /pins/1 + # GET /pins/1.json + def show + end + + # GET /pins/new + def new + @pin = Pin.new + end + + # GET /pins/1/edit + def edit + end + + # POST /pins + # POST /pins.json + def create + @pin = Pin.new(pin_params) + + respond_to do |format| + if @pin.save + format.html { redirect_to @pin, notice: 'Pin was successfully created.' } + format.json { render action: 'show', status: :created, location: @pin } + else + format.html { render action: 'new' } + format.json { render json: @pin.errors, status: :unprocessable_entity } + end + end + end + + # PATCH/PUT /pins/1 + # PATCH/PUT /pins/1.json + def update + respond_to do |format| + if @pin.update(pin_params) + format.html { redirect_to @pin, notice: 'Pin was successfully updated.' } + format.json { head :no_content } + else + format.html { render action: 'edit' } + format.json { render json: @pin.errors, status: :unprocessable_entity } + end + end + end + + # DELETE /pins/1 + # DELETE /pins/1.json + def destroy + @pin.destroy + respond_to do |format| + format.html { redirect_to pins_url } + format.json { head :no_content } + end + end + + private + # Use callbacks to share common setup or constraints between actions. + def set_pin + @pin = Pin.find(params[:id]) + end + + # Never trust parameters from the scary internet, only allow the white list through. + def pin_params + params.require(:pin).permit(:description) + end +end diff --git a/app/helpers/pins_helper.rb b/app/helpers/pins_helper.rb new file mode 100644 index 0000000..0625367 --- /dev/null +++ b/app/helpers/pins_helper.rb @@ -0,0 +1,2 @@ +module PinsHelper +end diff --git a/app/models/pin.rb b/app/models/pin.rb new file mode 100644 index 0000000..d246c90 --- /dev/null +++ b/app/models/pin.rb @@ -0,0 +1,5 @@ +class Pin < ActiveRecord::Base + attr_accessible :description + + validates :description, presence: true +end diff --git a/app/models/user.rb b/app/models/user.rb index b4aa622..5d12968 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,7 +1,7 @@ class User < ActiveRecord::Base # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable and :omniauthable - devise :database_authenticatable, :registerable, #:recoverable + devise :database_authenticatable, :registerable, #:recoverable, :rememberable, :trackable, :validatable attr_accessible :email, :password, :password_confirmation, :remember_me, :first_name, :last_name, :provider, :uid, :as => [:default, :admin] diff --git a/app/views/pins/_form.html.erb b/app/views/pins/_form.html.erb new file mode 100644 index 0000000..e5cea9d --- /dev/null +++ b/app/views/pins/_form.html.erb @@ -0,0 +1,9 @@ +<%= simple_form_for(@pin, html: { class: "form-horizontal"}) do |f| %> + <%= f.error_notification %> + + <%= f.input :description, as: :text, input_html: { rows: "3" } %> + +
+ <%= f.button :submit, class: "btn btn-primary" %> +
+<% end %> \ No newline at end of file diff --git a/app/views/pins/_pin.html.erb b/app/views/pins/_pin.html.erb new file mode 100644 index 0000000..6305c30 --- /dev/null +++ b/app/views/pins/_pin.html.erb @@ -0,0 +1,6 @@ + + <%= pin.description %> + <%= link_to 'Show', pin %> + <%= link_to 'Edit', edit_pin_path(pin) %> + <%= link_to 'Destroy', pin, method: :delete, data: { confirm: 'Are you sure?' } %> + \ No newline at end of file diff --git a/app/views/pins/edit.html.erb b/app/views/pins/edit.html.erb new file mode 100644 index 0000000..aed7ca9 --- /dev/null +++ b/app/views/pins/edit.html.erb @@ -0,0 +1,6 @@ +

Editing pin

+ +<%= render 'form' %> + +<%= link_to 'Show', @pin %> | +<%= link_to 'Back', pins_path %> diff --git a/app/views/pins/index.html.erb b/app/views/pins/index.html.erb new file mode 100644 index 0000000..6acfe7c --- /dev/null +++ b/app/views/pins/index.html.erb @@ -0,0 +1,20 @@ +

Listing pins

+ + + + + + + + + + + + + <%= render @pins %> + +
Description
+ +
+ + <%= link_to 'New Pin', new_pin_path %> \ No newline at end of file diff --git a/app/views/pins/index.json.jbuilder b/app/views/pins/index.json.jbuilder new file mode 100644 index 0000000..6453bf6 --- /dev/null +++ b/app/views/pins/index.json.jbuilder @@ -0,0 +1,4 @@ +json.array!(@pins) do |pin| + json.extract! pin, :description + json.url pin_url(pin, format: :json) +end \ No newline at end of file diff --git a/app/views/pins/new.html.erb b/app/views/pins/new.html.erb new file mode 100644 index 0000000..7113b77 --- /dev/null +++ b/app/views/pins/new.html.erb @@ -0,0 +1,5 @@ +

New pin

+ +<%= render 'form' %> + +<%= link_to 'Back', pins_path %> diff --git a/app/views/pins/show.html.erb b/app/views/pins/show.html.erb new file mode 100644 index 0000000..06724f4 --- /dev/null +++ b/app/views/pins/show.html.erb @@ -0,0 +1,12 @@ +
+
+ <%# This puts the grey box around content below %> +
+

+ <%= @pin.description %> +

+ <%= link_to 'Edit', edit_pin_path(@pin) %> | + <%= link_to 'Back', pins_path %> +
+
+
\ No newline at end of file diff --git a/app/views/pins/show.json.jbuilder b/app/views/pins/show.json.jbuilder new file mode 100644 index 0000000..60b7892 --- /dev/null +++ b/app/views/pins/show.json.jbuilder @@ -0,0 +1 @@ +json.extract! @pin, :description, :created_at, :updated_at diff --git a/config/routes.rb b/config/routes.rb index 7212e6f..3f2445d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,6 @@ Omrails::Application.routes.draw do + resources :pins + devise_for :users get 'about' => 'pages#about' root :to => 'pages#home' diff --git a/db/migrate/20130917193701_create_pins.rb b/db/migrate/20130917193701_create_pins.rb new file mode 100644 index 0000000..6f918b2 --- /dev/null +++ b/db/migrate/20130917193701_create_pins.rb @@ -0,0 +1,9 @@ +class CreatePins < ActiveRecord::Migration + def change + create_table :pins do |t| + t.string :description + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 78e6495..69c6c86 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,13 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20130917165202) do +ActiveRecord::Schema.define(version: 20130917193701) do + + create_table "pins", force: true do |t| + t.string "description" + t.datetime "created_at" + t.datetime "updated_at" + end create_table "users", force: true do |t| t.string "email", default: "", null: false diff --git a/test/controllers/pins_controller_test.rb b/test/controllers/pins_controller_test.rb new file mode 100644 index 0000000..df118ca --- /dev/null +++ b/test/controllers/pins_controller_test.rb @@ -0,0 +1,49 @@ +require 'test_helper' + +class PinsControllerTest < ActionController::TestCase + setup do + @pin = pins(:one) + end + + test "should get index" do + get :index + assert_response :success + assert_not_nil assigns(:pins) + end + + test "should get new" do + get :new + assert_response :success + end + + test "should create pin" do + assert_difference('Pin.count') do + post :create, pin: { description: @pin.description } + end + + assert_redirected_to pin_path(assigns(:pin)) + end + + test "should show pin" do + get :show, id: @pin + assert_response :success + end + + test "should get edit" do + get :edit, id: @pin + assert_response :success + end + + test "should update pin" do + patch :update, id: @pin, pin: { description: @pin.description } + assert_redirected_to pin_path(assigns(:pin)) + end + + test "should destroy pin" do + assert_difference('Pin.count', -1) do + delete :destroy, id: @pin + end + + assert_redirected_to pins_path + end +end diff --git a/test/fixtures/pins.yml b/test/fixtures/pins.yml new file mode 100644 index 0000000..183345c --- /dev/null +++ b/test/fixtures/pins.yml @@ -0,0 +1,7 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html + +one: + description: MyString + +two: + description: MyString diff --git a/test/helpers/pins_helper_test.rb b/test/helpers/pins_helper_test.rb new file mode 100644 index 0000000..93785da --- /dev/null +++ b/test/helpers/pins_helper_test.rb @@ -0,0 +1,4 @@ +require 'test_helper' + +class PinsHelperTest < ActionView::TestCase +end diff --git a/test/models/pin_test.rb b/test/models/pin_test.rb new file mode 100644 index 0000000..9df2549 --- /dev/null +++ b/test/models/pin_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class PinTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end