Skip to content

Commit

Permalink
Add Pins Scaffold
Browse files Browse the repository at this point in the history
  • Loading branch information
Neil Gehani committed Sep 17, 2013
1 parent 846b504 commit 22e9a27
Show file tree
Hide file tree
Showing 20 changed files with 233 additions and 2 deletions.
3 changes: 3 additions & 0 deletions app/assets/javascripts/pins.js.coffee
Original file line number Diff line number Diff line change
@@ -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/
74 changes: 74 additions & 0 deletions app/controllers/pins_controller.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions app/helpers/pins_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module PinsHelper
end
5 changes: 5 additions & 0 deletions app/models/pin.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Pin < ActiveRecord::Base
attr_accessible :description

validates :description, presence: true
end
2 changes: 1 addition & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
@@ -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]

Expand Down
9 changes: 9 additions & 0 deletions app/views/pins/_form.html.erb
Original file line number Diff line number Diff line change
@@ -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" } %>

<div class="form-actions">
<%= f.button :submit, class: "btn btn-primary" %>
</div>
<% end %>
6 changes: 6 additions & 0 deletions app/views/pins/_pin.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<tr>
<td><%= pin.description %></td>
<td><%= link_to 'Show', pin %></td>
<td><%= link_to 'Edit', edit_pin_path(pin) %></td>
<td><%= link_to 'Destroy', pin, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
6 changes: 6 additions & 0 deletions app/views/pins/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<h1>Editing pin</h1>

<%= render 'form' %>

<%= link_to 'Show', @pin %> |
<%= link_to 'Back', pins_path %>
20 changes: 20 additions & 0 deletions app/views/pins/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<h1>Listing pins</h1>

<table class="table table-striped">
<thead>
<tr>
<th>Description</th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>

<tbody>
<%= render @pins %>
</tbody>
</table>

<br />

<%= link_to 'New Pin', new_pin_path %>
4 changes: 4 additions & 0 deletions app/views/pins/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
json.array!(@pins) do |pin|
json.extract! pin, :description
json.url pin_url(pin, format: :json)
end
5 changes: 5 additions & 0 deletions app/views/pins/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<h1>New pin</h1>

<%= render 'form' %>

<%= link_to 'Back', pins_path %>
12 changes: 12 additions & 0 deletions app/views/pins/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<div class="row">
<div class="span6 offset3">
<%# This puts the grey box around content below %>
<div class="well">
<p>
<%= @pin.description %>
</p>
<%= link_to 'Edit', edit_pin_path(@pin) %> |
<%= link_to 'Back', pins_path %>
</div>
</div>
</div>
1 change: 1 addition & 0 deletions app/views/pins/show.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.extract! @pin, :description, :created_at, :updated_at
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
Omrails::Application.routes.draw do
resources :pins

devise_for :users
get 'about' => 'pages#about'
root :to => 'pages#home'
Expand Down
9 changes: 9 additions & 0 deletions db/migrate/20130917193701_create_pins.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class CreatePins < ActiveRecord::Migration
def change
create_table :pins do |t|
t.string :description

t.timestamps
end
end
end
8 changes: 7 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
49 changes: 49 additions & 0 deletions test/controllers/pins_controller_test.rb
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions test/fixtures/pins.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html

one:
description: MyString

two:
description: MyString
4 changes: 4 additions & 0 deletions test/helpers/pins_helper_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require 'test_helper'

class PinsHelperTest < ActionView::TestCase
end
7 changes: 7 additions & 0 deletions test/models/pin_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class PinTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

0 comments on commit 22e9a27

Please sign in to comment.