Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Setup nifty_scaffold and used it to generate Person model

  • Loading branch information...
commit 4916d04ef7f75e8387854f6ba03a0efc7bce61e7 1 parent 11b6be9
@jcasimir jcasimir authored
View
2  Gemfile
@@ -8,4 +8,6 @@ group :development, :test do
gem 'jquery-rails'
gem 'heroku'
gem 'unicorn'
+ gem 'nifty-generators'
+ gem "mocha"
end
View
4 Gemfile.lock
@@ -51,6 +51,8 @@ GEM
mime-types (~> 1.16)
treetop (~> 1.4.8)
mime-types (1.16)
+ mocha (0.9.12)
+ nifty-generators (0.4.6)
polyglot (0.3.1)
rack (1.2.2)
rack-mount (0.6.14)
@@ -101,6 +103,8 @@ PLATFORMS
DEPENDENCIES
heroku
jquery-rails
+ mocha
+ nifty-generators
rails (= 3.0.6)
rspec-rails
sqlite3
View
41 app/controllers/people_controller.rb
@@ -0,0 +1,41 @@
+class PeopleController < ApplicationController
+ def index
+ @people = Person.all
+ end
+
+ def show
+ @person = Person.find(params[:id])
+ end
+
+ def new
+ @person = Person.new
+ end
+
+ def create
+ @person = Person.new(params[:person])
+ if @person.save
+ redirect_to @person, :notice => "Successfully created person."
+ else
+ render :action => 'new'
+ end
+ end
+
+ def edit
+ @person = Person.find(params[:id])
+ end
+
+ def update
+ @person = Person.find(params[:id])
+ if @person.update_attributes(params[:person])
+ redirect_to @person, :notice => "Successfully updated person."
+ else
+ render :action => 'edit'
+ end
+ end
+
+ def destroy
+ @person = Person.find(params[:id])
+ @person.destroy
+ redirect_to people_url, :notice => "Successfully destroyed person."
+ end
+end
View
23 app/helpers/error_messages_helper.rb
@@ -0,0 +1,23 @@
+module ErrorMessagesHelper
+ # Render error messages for the given objects. The :message and :header_message options are allowed.
+ def error_messages_for(*objects)
+ options = objects.extract_options!
+ options[:header_message] ||= I18n.t(:"activerecord.errors.header", :default => "Invalid Fields")
+ options[:message] ||= I18n.t(:"activerecord.errors.message", :default => "Correct the following errors and try again.")
+ messages = objects.compact.map { |o| o.errors.full_messages }.flatten
+ unless messages.empty?
+ content_tag(:div, :class => "error_messages") do
+ list_items = messages.map { |msg| content_tag(:li, msg) }
+ content_tag(:h2, options[:header_message]) + content_tag(:p, options[:message]) + content_tag(:ul, list_items.join.html_safe)
+ end
+ end
+ end
+
+ module FormBuilderAdditions
+ def error_messages(options = {})
+ @template.error_messages_for(@object, options)
+ end
+ end
+end
+
+ActionView::Helpers::FormBuilder.send(:include, ErrorMessagesHelper::FormBuilderAdditions)
View
22 app/helpers/layout_helper.rb
@@ -0,0 +1,22 @@
+# These helper methods can be called in your template to set variables to be used in the layout
+# This module should be included in all views globally,
+# to do so you may need to add this line to your ApplicationController
+# helper :layout
+module LayoutHelper
+ def title(page_title, show_title = true)
+ content_for(:title) { h(page_title.to_s) }
+ @show_title = show_title
+ end
+
+ def show_title?
+ @show_title
+ end
+
+ def stylesheet(*args)
+ content_for(:head) { stylesheet_link_tag(*args) }
+ end
+
+ def javascript(*args)
+ content_for(:head) { javascript_include_tag(*args) }
+ end
+end
View
2  app/helpers/people_helper.rb
@@ -0,0 +1,2 @@
+module PeopleHelper
+end
View
3  app/models/person.rb
@@ -0,0 +1,3 @@
+class Person < ActiveRecord::Base
+ attr_accessible :first_name, :last_name
+end
View
27 app/views/layouts/application.html.erb
@@ -1,14 +1,19 @@
<!DOCTYPE html>
<html>
-<head>
- <title>JSContact</title>
- <%= stylesheet_link_tag :all %>
- <%= javascript_include_tag :defaults %>
- <%= csrf_meta_tag %>
-</head>
-<body>
-
-<%= yield %>
-
-</body>
+ <head>
+ <title><%= content_for?(:title) ? yield(:title) : "Untitled" %></title>
+ <%= stylesheet_link_tag "application" %>
+ <%= javascript_include_tag :defaults %>
+ <%= csrf_meta_tag %>
+ <%= yield(:head) %>
+ </head>
+ <body>
+ <div id="container">
+ <% flash.each do |name, msg| %>
+ <%= content_tag :div, msg, :id => "flash_#{name}" %>
+ <% end %>
+ <%= content_tag :h1, yield(:title) if show_title? %>
+ <%= yield %>
+ </div>
+ </body>
</html>
View
12 app/views/people/_form.html.erb
@@ -0,0 +1,12 @@
+<%= form_for @person do |f| %>
+ <%= f.error_messages %>
+ <p>
+ <%= f.label :first_name %><br />
+ <%= f.text_field :first_name %>
+ </p>
+ <p>
+ <%= f.label :last_name %><br />
+ <%= f.text_field :last_name %>
+ </p>
+ <p><%= f.submit %></p>
+<% end %>
View
8 app/views/people/edit.html.erb
@@ -0,0 +1,8 @@
+<% title "Edit Person" %>
+
+<%= render 'form' %>
+
+<p>
+ <%= link_to "Show", @person %> |
+ <%= link_to "View All", people_path %>
+</p>
View
19 app/views/people/index.html.erb
@@ -0,0 +1,19 @@
+<% title "People" %>
+
+<table>
+ <tr>
+ <th>First Name</th>
+ <th>Last Name</th>
+ </tr>
+ <% for person in @people %>
+ <tr>
+ <td><%= person.first_name %></td>
+ <td><%= person.last_name %></td>
+ <td><%= link_to "Show", person %></td>
+ <td><%= link_to "Edit", edit_person_path(person) %></td>
+ <td><%= link_to "Destroy", person, :confirm => 'Are you sure?', :method => :delete %></td>
+ </tr>
+ <% end %>
+</table>
+
+<p><%= link_to "New Person", new_person_path %></p>
View
5 app/views/people/new.html.erb
@@ -0,0 +1,5 @@
+<% title "New Person" %>
+
+<%= render 'form' %>
+
+<p><%= link_to "Back to List", people_path %></p>
View
16 app/views/people/show.html.erb
@@ -0,0 +1,16 @@
+<% title "Person" %>
+
+<p>
+ <strong>First Name:</strong>
+ <%= @person.first_name %>
+</p>
+<p>
+ <strong>Last Name:</strong>
+ <%= @person.last_name %>
+</p>
+
+<p>
+ <%= link_to "Edit", edit_person_path(@person) %> |
+ <%= link_to "Destroy", @person, :confirm => 'Are you sure?', :method => :delete %> |
+ <%= link_to "View All", people_path %>
+</p>
View
2  config/routes.rb
@@ -1,4 +1,6 @@
JSContact::Application.routes.draw do
+ resources :people
+
# The priority is based upon order of creation:
# first created -> highest priority.
View
13 db/migrate/20110417205708_create_people.rb
@@ -0,0 +1,13 @@
+class CreatePeople < ActiveRecord::Migration
+ def self.up
+ create_table :people do |t|
+ t.string :first_name
+ t.string :last_name
+ t.timestamps
+ end
+ end
+
+ def self.down
+ drop_table :people
+ end
+end
View
22 db/schema.rb
@@ -0,0 +1,22 @@
+# 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 to check this file into your version control system.
+
+ActiveRecord::Schema.define(:version => 20110417205708) do
+
+ create_table "people", :force => true do |t|
+ t.string "first_name"
+ t.string "last_name"
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ end
+
+end
View
75 public/stylesheets/application.css
@@ -0,0 +1,75 @@
+body {
+ background-color: #4B7399;
+ font-family: Verdana, Helvetica, Arial;
+ font-size: 14px;
+}
+
+a img {
+ border: none;
+}
+
+a {
+ color: #0000FF;
+}
+
+.clear {
+ clear: both;
+ height: 0;
+ overflow: hidden;
+}
+
+#container {
+ width: 75%;
+ margin: 0 auto;
+ background-color: #FFF;
+ padding: 20px 40px;
+ border: solid 1px black;
+ margin-top: 20px;
+}
+
+#flash_notice, #flash_error, #flash_alert {
+ padding: 5px 8px;
+ margin: 10px 0;
+}
+
+#flash_notice {
+ background-color: #CFC;
+ border: solid 1px #6C6;
+}
+
+#flash_error, #flash_alert {
+ background-color: #FCC;
+ border: solid 1px #C66;
+}
+
+.fieldWithErrors {
+ display: inline;
+}
+
+.error_messages {
+ width: 400px;
+ border: 2px solid #CF0000;
+ padding: 0px;
+ padding-bottom: 12px;
+ margin-bottom: 20px;
+ background-color: #f0f0f0;
+ font-size: 12px;
+}
+
+.error_messages h2 {
+ text-align: left;
+ font-weight: bold;
+ padding: 5px 10px;
+ font-size: 12px;
+ margin: 0;
+ background-color: #c00;
+ color: #fff;
+}
+
+.error_messages p {
+ margin: 8px 10px;
+}
+
+.error_messages ul {
+ margin: 0;
+}
View
57 spec/controllers/people_controller_spec.rb
@@ -0,0 +1,57 @@
+require File.dirname(__FILE__) + '/../spec_helper'
+
+describe PeopleController do
+ fixtures :all
+ render_views
+
+ it "index action should render index template" do
+ get :index
+ response.should render_template(:index)
+ end
+
+ it "show action should render show template" do
+ get :show, :id => Person.first
+ response.should render_template(:show)
+ end
+
+ it "new action should render new template" do
+ get :new
+ response.should render_template(:new)
+ end
+
+ it "create action should render new template when model is invalid" do
+ Person.any_instance.stubs(:valid?).returns(false)
+ post :create
+ response.should render_template(:new)
+ end
+
+ it "create action should redirect when model is valid" do
+ Person.any_instance.stubs(:valid?).returns(true)
+ post :create
+ response.should redirect_to(person_url(assigns[:person]))
+ end
+
+ it "edit action should render edit template" do
+ get :edit, :id => Person.first
+ response.should render_template(:edit)
+ end
+
+ it "update action should render edit template when model is invalid" do
+ Person.any_instance.stubs(:valid?).returns(false)
+ put :update, :id => Person.first
+ response.should render_template(:edit)
+ end
+
+ it "update action should redirect when model is valid" do
+ Person.any_instance.stubs(:valid?).returns(true)
+ put :update, :id => Person.first
+ response.should redirect_to(person_url(assigns[:person]))
+ end
+
+ it "destroy action should destroy model and redirect to index action" do
+ person = Person.first
+ delete :destroy, :id => person
+ response.should redirect_to(people_url)
+ Person.exists?(person.id).should be_false
+ end
+end
View
7 spec/fixtures/people.yml
@@ -0,0 +1,7 @@
+one:
+ first_name: MyString
+ last_name: MyString
+
+two:
+ first_name: MyString
+ last_name: MyString
View
7 spec/models/person_spec.rb
@@ -0,0 +1,7 @@
+require File.dirname(__FILE__) + '/../spec_helper'
+
+describe Person do
+ it "should be valid" do
+ Person.new.should be_valid
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.