Skip to content

Commit

Permalink
Implement basic user listing
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacob Atzen committed Feb 15, 2011
1 parent 141f038 commit d00c94c
Show file tree
Hide file tree
Showing 19 changed files with 360 additions and 1 deletion.
83 changes: 83 additions & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
class UsersController < ApplicationController
# GET /users
# GET /users.xml
def index
@users = User.all

respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @users }
end
end

# GET /users/1
# GET /users/1.xml
def show
@user = User.find(params[:id])

respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @user }
end
end

# GET /users/new
# GET /users/new.xml
def new
@user = User.new

respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @user }
end
end

# GET /users/1/edit
def edit
@user = User.find(params[:id])
end

# POST /users
# POST /users.xml
def create
@user = User.new(params[:user])

respond_to do |format|
if @user.save
format.html { redirect_to(@user, :notice => 'User was successfully created.') }
format.xml { render :xml => @user, :status => :created, :location => @user }
else
format.html { render :action => "new" }
format.xml { render :xml => @user.errors, :status => :unprocessable_entity }
end
end
end

# PUT /users/1
# PUT /users/1.xml
def update
@user = User.find(params[:id])

respond_to do |format|
if @user.update_attributes(params[:user])
format.html { redirect_to(@user, :notice => 'User was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @user.errors, :status => :unprocessable_entity }
end
end
end

# DELETE /users/1
# DELETE /users/1.xml
def destroy
@user = User.find(params[:id])
@user.destroy

respond_to do |format|
format.html { redirect_to(users_url) }
format.xml { head :ok }
end
end
end
6 changes: 6 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
module ApplicationHelper
def user_path_js(user_id)
user_path('$USERID').gsub('$USERID', user_id)
end
def edit_user_path_js(user_id)
user_path('$USERID').gsub('$USERID', user_id)
end
end
2 changes: 2 additions & 0 deletions app/helpers/users_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module UsersHelper
end
2 changes: 2 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class User < ActiveRecord::Base
end
3 changes: 2 additions & 1 deletion app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
<head>
<title>Knockoutonrails</title>
<%= stylesheet_link_tag :all %>
<%= javascript_include_tag :defaults %>
<%= javascript_include_tag 'https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js' %>
<%= javascript_include_tag 'rails', 'jquery.tmpl.min.js', 'knockout-1.1.2' %>
<%= csrf_meta_tag %>
</head>
<body>
Expand Down
25 changes: 25 additions & 0 deletions app/views/users/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<%= form_for(@user) do |f| %>
<% if @user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h2>

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

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

<%= render 'form' %>

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

<table>
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody data-bind='template: "userTemplate"'>
</tbody>
</table>

<br />

<%= link_to 'New User', new_user_path %>

<script type="text/html" charset="utf-8" id="userTemplate">
{{each filteredUsers}}
<tr>
<td>${ user.name }</td>
<td>${ user.email }</td>
<td><%= link_to 'Show', user_path_js('${user.id}') %></td>
<td><%= link_to 'Edit', edit_user_path_js('${user.id}') %></td>
<td><%= link_to 'Destroy', user_path_js('${user.id}'), :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
{{/each}}
</script>

<script type="text/javascript" charset="utf-8">
var users = <%= raw @users.to_json %>
var observable_users = $.map(users, function(elem) {
elem.user.hidden = ko.observable(false);
return elem;
});
var viewModel = {
users: ko.observableArray(observable_users),
userFilters: ko.observableArray([]),
}
viewModel.filteredUsers = ko.dependentObservable(function() {
return $.grep(viewModel.users(), function(element) {
return $.inArray(element.user.name, viewModel.userFilters()) < 0;
});
});
ko.applyBindings(viewModel);
</script>
5 changes: 5 additions & 0 deletions app/views/users/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<h1>New user</h1>

<%= render 'form' %>

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

<p>
<b>Name:</b>
<%= @user.name %>
</p>

<p>
<b>Email:</b>
<%= @user.email %>
</p>


<%= link_to 'Edit', edit_user_path(@user) %> |
<%= link_to 'Back', users_path %>
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
Knockoutonrails::Application.routes.draw do
resources :users

# The priority is based upon order of creation:
# first created -> highest priority.

Expand Down
14 changes: 14 additions & 0 deletions db/migrate/20110215154340_create_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class CreateUsers < ActiveRecord::Migration
def self.up
create_table :users do |t|
t.string :name
t.string :email

t.timestamps
end
end

def self.down
drop_table :users
end
end
22 changes: 22 additions & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
@@ -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 => 20110215154340) do

create_table "users", :force => true do |t|
t.string "name"
t.string "email"
t.datetime "created_at"
t.datetime "updated_at"
end

end
1 change: 1 addition & 0 deletions public/javascripts/jquery.tmpl.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 56 additions & 0 deletions public/stylesheets/scaffold.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
body { background-color: #fff; color: #333; }

body, 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; }
a:visited { color: #666; }
a:hover { color: #fff; background-color:#000; }

div.field, div.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;
}

#error_explanation 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;
}

#error_explanation ul li {
font-size: 12px;
list-style: square;
}
9 changes: 9 additions & 0 deletions test/fixtures/users.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html

one:
name: MyString
email: MyString

two:
name: MyString
email: MyString
49 changes: 49 additions & 0 deletions test/functional/users_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
require 'test_helper'

class UsersControllerTest < ActionController::TestCase
setup do
@user = users(:one)
end

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

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

test "should create user" do
assert_difference('User.count') do
post :create, :user => @user.attributes
end

assert_redirected_to user_path(assigns(:user))
end

test "should show user" do
get :show, :id => @user.to_param
assert_response :success
end

test "should get edit" do
get :edit, :id => @user.to_param
assert_response :success
end

test "should update user" do
put :update, :id => @user.to_param, :user => @user.attributes
assert_redirected_to user_path(assigns(:user))
end

test "should destroy user" do
assert_difference('User.count', -1) do
delete :destroy, :id => @user.to_param
end

assert_redirected_to users_path
end
end
4 changes: 4 additions & 0 deletions test/unit/helpers/users_helper_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require 'test_helper'

class UsersHelperTest < ActionView::TestCase
end
8 changes: 8 additions & 0 deletions test/unit/user_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require 'test_helper'

class UserTest < ActiveSupport::TestCase
# Replace this with your real tests.
test "the truth" do
assert true
end
end

0 comments on commit d00c94c

Please sign in to comment.