Skip to content

Commit

Permalink
generated scaffolding for widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
nellshamrell committed Dec 23, 2014
1 parent cda68c4 commit 1707c74
Show file tree
Hide file tree
Showing 22 changed files with 346 additions and 29 deletions.
29 changes: 2 additions & 27 deletions README.rdoc
Original file line number Diff line number Diff line change
@@ -1,28 +1,3 @@
== README
== Widget World

This README would normally document whatever steps are necessary to get the
application up and running.

Things you may want to cover:

* Ruby version

* System dependencies

* Configuration

* Database creation

* Database initialization

* How to run the test suite

* Services (job queues, cache servers, search engines, etc.)

* Deployment instructions

* ...


Please feel free to use a different markup language if you do not plan to run
<tt>rake doc:app</tt>.
This is a very simple sample Rails application that uses Postgresql as it's DB.
3 changes: 3 additions & 0 deletions app/assets/javascripts/widgets.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/
69 changes: 69 additions & 0 deletions app/assets/stylesheets/scaffolds.css.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
body {
background-color: #fff;
color: #333;
font-family: verdana, arial, helvetica, sans-serif;
font-size: 13px;
line-height: 18px;
}

p, ol, ul, td {
font-family: verdana, arial, helvetica, sans-serif;
font-size: 13px;
line-height: 18px;
}

pre {
background-color: #eee;
padding: 10px;
font-size: 11px;
}

a {
color: #000;
&:visited {
color: #666;
}
&:hover {
color: #fff;
background-color: #000;
}
}

div {
&.field, &.actions {
margin-bottom: 10px;
}
}

#notice {
color: green;
}

.field_with_errors {
padding: 2px;
background-color: red;
display: table;
}

#error_explanation {
width: 450px;
border: 2px solid red;
padding: 7px;
padding-bottom: 0;
margin-bottom: 20px;
background-color: #f0f0f0;
h2 {
text-align: left;
font-weight: bold;
padding: 5px 5px 5px 15px;
font-size: 12px;
margin: -7px;
margin-bottom: 0px;
background-color: #c00;
color: #fff;
}
ul li {
font-size: 12px;
list-style: square;
}
}
3 changes: 3 additions & 0 deletions app/assets/stylesheets/widgets.css.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the Widgets controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
74 changes: 74 additions & 0 deletions app/controllers/widgets_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
class WidgetsController < ApplicationController
before_action :set_widget, only: [:show, :edit, :update, :destroy]

# GET /widgets
# GET /widgets.json
def index
@widgets = Widget.all
end

# GET /widgets/1
# GET /widgets/1.json
def show
end

# GET /widgets/new
def new
@widget = Widget.new
end

# GET /widgets/1/edit
def edit
end

# POST /widgets
# POST /widgets.json
def create
@widget = Widget.new(widget_params)

respond_to do |format|
if @widget.save
format.html { redirect_to @widget, notice: 'Widget was successfully created.' }
format.json { render :show, status: :created, location: @widget }
else
format.html { render :new }
format.json { render json: @widget.errors, status: :unprocessable_entity }
end
end
end

# PATCH/PUT /widgets/1
# PATCH/PUT /widgets/1.json
def update
respond_to do |format|
if @widget.update(widget_params)
format.html { redirect_to @widget, notice: 'Widget was successfully updated.' }
format.json { render :show, status: :ok, location: @widget }
else
format.html { render :edit }
format.json { render json: @widget.errors, status: :unprocessable_entity }
end
end
end

# DELETE /widgets/1
# DELETE /widgets/1.json
def destroy
@widget.destroy
respond_to do |format|
format.html { redirect_to widgets_url, notice: 'Widget was successfully destroyed.' }
format.json { head :no_content }
end
end

private
# Use callbacks to share common setup or constraints between actions.
def set_widget
@widget = Widget.find(params[:id])
end

# Never trust parameters from the scary internet, only allow the white list through.
def widget_params
params.require(:widget).permit(:name, :part_num)
end
end
2 changes: 2 additions & 0 deletions app/helpers/widgets_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module WidgetsHelper
end
2 changes: 2 additions & 0 deletions app/models/widget.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class Widget < ActiveRecord::Base
end
25 changes: 25 additions & 0 deletions app/views/widgets/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<%= form_for(@widget) do |f| %>
<% if @widget.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@widget.errors.count, "error") %> prohibited this widget from being saved:</h2>

<ul>
<% @widget.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>

<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :part_num %><br>
<%= f.number_field :part_num %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
6 changes: 6 additions & 0 deletions app/views/widgets/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<h1>Editing widget</h1>

<%= render 'form' %>

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

<table>
<thead>
<tr>
<th>Name</th>
<th>Part num</th>
<th colspan="3"></th>
</tr>
</thead>

<tbody>
<% @widgets.each do |widget| %>
<tr>
<td><%= widget.name %></td>
<td><%= widget.part_num %></td>
<td><%= link_to 'Show', widget %></td>
<td><%= link_to 'Edit', edit_widget_path(widget) %></td>
<td><%= link_to 'Destroy', widget, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>

<br>

<%= link_to 'New Widget', new_widget_path %>
4 changes: 4 additions & 0 deletions app/views/widgets/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
json.array!(@widgets) do |widget|
json.extract! widget, :id, :name, :part_num
json.url widget_url(widget, format: :json)
end
5 changes: 5 additions & 0 deletions app/views/widgets/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<h1>New widget</h1>

<%= render 'form' %>

<%= link_to 'Back', widgets_path %>
14 changes: 14 additions & 0 deletions app/views/widgets/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<p id="notice"><%= notice %></p>

<p>
<strong>Name:</strong>
<%= @widget.name %>
</p>

<p>
<strong>Part num:</strong>
<%= @widget.part_num %>
</p>

<%= link_to 'Edit', edit_widget_path(@widget) %> |
<%= link_to 'Back', widgets_path %>
1 change: 1 addition & 0 deletions app/views/widgets/show.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.extract! @widget, :id, :name, :part_num, :created_at, :updated_at
2 changes: 1 addition & 1 deletion config/database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ development:
# To create additional roles in postgres see `$ createuser --help`.
# When left blank, postgres will use the default role. This is
# the same name as the operating system user that initialized the database.
#username: widgetworld
username: nellshamrell

# The password associated with the postgres role (username).
#password:
Expand Down
4 changes: 3 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
Rails.application.routes.draw do
resources :widgets

# 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'
root 'widgets#index'

# Example of regular route:
# get 'products/:id' => 'catalog#view'
Expand Down
10 changes: 10 additions & 0 deletions db/migrate/20141223043443_create_widgets.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateWidgets < ActiveRecord::Migration
def change
create_table :widgets do |t|
t.string :name
t.integer :part_num

t.timestamps
end
end
end
26 changes: 26 additions & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# 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: 20141223043443) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

create_table "widgets", force: true do |t|
t.string "name"
t.integer "part_num"
t.datetime "created_at"
t.datetime "updated_at"
end

end
49 changes: 49 additions & 0 deletions test/controllers/widgets_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
require 'test_helper'

class WidgetsControllerTest < ActionController::TestCase
setup do
@widget = widgets(:one)
end

test "should get index" do
get :index
assert_response :success
assert_not_nil assigns(:widgets)
end

test "should get new" do
get :new
assert_response :success
end

test "should create widget" do
assert_difference('Widget.count') do
post :create, widget: { name: @widget.name, part_num: @widget.part_num }
end

assert_redirected_to widget_path(assigns(:widget))
end

test "should show widget" do
get :show, id: @widget
assert_response :success
end

test "should get edit" do
get :edit, id: @widget
assert_response :success
end

test "should update widget" do
patch :update, id: @widget, widget: { name: @widget.name, part_num: @widget.part_num }
assert_redirected_to widget_path(assigns(:widget))
end

test "should destroy widget" do
assert_difference('Widget.count', -1) do
delete :destroy, id: @widget
end

assert_redirected_to widgets_path
end
end
9 changes: 9 additions & 0 deletions test/fixtures/widgets.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

one:
name: MyString
part_num: 1

two:
name: MyString
part_num: 1
Loading

0 comments on commit 1707c74

Please sign in to comment.