Permalink
Browse files

after lunch

  • Loading branch information...
1 parent bad9929 commit 58fae61ed1e7dcb9df4c7476c76dab47fdea1d5f Jason Draper committed Jun 22, 2012
@@ -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://jashkenas.github.com/coffee-script/
@@ -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://jashkenas.github.com/coffee-script/
@@ -0,0 +1,3 @@
+// Place all the styles related to the sessions controller here.
+// They will automatically be included in application.css.
+// You can use Sass (SCSS) here: http://sass-lang.com/
@@ -0,0 +1,3 @@
+// Place all the styles related to the Users controller here.
+// They will automatically be included in application.css.
+// You can use Sass (SCSS) here: http://sass-lang.com/
@@ -1,3 +1,29 @@
class ApplicationController < ActionController::Base
protect_from_forgery
+ helper_method :current_user
+ before_filter :require_current_user
+
+ protected
+
+ def require_current_user
+ unless current_user
+ flash[:notice] = "You must be signed in to view this page"
+ redirect_to new_sessions_path
+ end
+ end
+
+ def current_user
+ if session[:user_id]
+ @current_user ||= User.find(session[:user_id])
+ end
+ end
+
+ def set_current_user(user_id)
+ session[:user_id] = user_id
+ User.find(session[:user_id]).set_logged_in
+ end
+
+ def logout_current_user
+ session[:user_id] = nil
+ end
end
@@ -0,0 +1,24 @@
+class SessionsController < ApplicationController
+ skip_before_filter :require_current_user
+
+ def new
+
+ end
+
+ def create
+ user = User.find_by_email(params[:email])
+ if user
+ flash[:notice] = "Logged in!"
+ set_current_user(user.id)
+ else
+ flash[:notice] = "No user found with that email"
+ end
+ redirect_to root_path
+ end
+
+ def destroy
+ logout_current_user
+ flash[:notice] = "Logged out"
+ redirect_to new_sessions_path
+ end
+end
@@ -1,6 +1,6 @@
class TasksController < ApplicationController
def index
- @tasks = Task.completed_first.newest_first
+ @lists = List.all(:include => :tasks)
@task = Task.new
end
@@ -11,17 +11,20 @@ def create
redirect_to tasks_path
else
@tasks = Task.all
+ @lists = List.all
flash[:notice] = "Could not save"
render 'index'
end
end
def edit
@task = Task.find(params[:id])
+ @lists = List.all
end
def update
@task = Task.find(params[:id])
+ @lists = List.all
if @task.update_attributes(params[:task])
flash[:notice] = "Updated task!"
redirect_to tasks_path
@@ -0,0 +1,83 @@
+class UsersController < ApplicationController
+ # GET /users
+ # GET /users.json
+ def index
+ @users = User.all
+
+ respond_to do |format|
+ format.html # index.html.erb
+ format.json { render json: @users }
+ end
+ end
+
+ # GET /users/1
+ # GET /users/1.json
+ def show
+ @user = User.find(params[:id])
+
+ respond_to do |format|
+ format.html # show.html.erb
+ format.json { render json: @user }
+ end
+ end
+
+ # GET /users/new
+ # GET /users/new.json
+ def new
+ @user = User.new
+
+ respond_to do |format|
+ format.html # new.html.erb
+ format.json { render json: @user }
+ end
+ end
+
+ # GET /users/1/edit
+ def edit
+ @user = User.find(params[:id])
+ end
+
+ # POST /users
+ # POST /users.json
+ 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.json { render json: @user, status: :created, location: @user }
+ else
+ format.html { render action: "new" }
+ format.json { render json: @user.errors, status: :unprocessable_entity }
+ end
+ end
+ end
+
+ # PUT /users/1
+ # PUT /users/1.json
+ 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.json { head :ok }
+ else
+ format.html { render action: "edit" }
+ format.json { render json: @user.errors, status: :unprocessable_entity }
+ end
+ end
+ end
+
+ # DELETE /users/1
+ # DELETE /users/1.json
+ def destroy
+ @user = User.find(params[:id])
+ @user.destroy
+
+ respond_to do |format|
+ format.html { redirect_to users_url }
+ format.json { head :ok }
+ end
+ end
+end
@@ -0,0 +1,2 @@
+module SessionsHelper
+end
@@ -0,0 +1,2 @@
+module UsersHelper
+end
View
@@ -3,6 +3,7 @@ class Task < ActiveRecord::Base
attr_accessible :name, :due_at, :list_id
validates :name, presence: true
+ validates :list, presence: true
scope :complete, where(:completed => true)
scope :incomplete, where(:completed => false)
View
@@ -0,0 +1,15 @@
+class User < ActiveRecord::Base
+ attr_accessible :first_name, :last_name, :email
+
+ def set_logged_in
+ update_attribute(:last_login_at, Time.now)
+ end
+
+ def full_name
+ "#{first_name} #{last_name}"
+ end
+
+ def to_s
+ full_name
+ end
+end
@@ -7,8 +7,12 @@
<%= csrf_meta_tags %>
</head>
<body>
-<p><%= flash[:notice] %></p>
-<%= yield %>
+ <% if current_user %>
+ Welcome, <%= current_user %> <%= current_user.last_name %>
+ <%= link_to "Logout", sessions_path, method: :delete %>
+ <% end %>
+ <p><%= flash[:notice] %></p>
+ <%= yield %>
</body>
</html>
@@ -1,5 +1,3 @@
-<p id="notice"><%= notice %></p>
-
<p>
<b>Name:</b>
<%= @list.name %>
@@ -0,0 +1,4 @@
+<%= form_tag sessions_path do |form| %>
+ <%= text_field_tag "email" %>
+ <%= submit_tag "Login" %>
+<% end %>
@@ -8,5 +8,6 @@
<%= form_for @task do |form| %>
<div>Name: <%= form.text_field :name %></div>
<div>Due Date: <%= form.datetime_select :due_at %></div>
+ <div>List: <%= form.collection_select :list_id, @lists, :id, :name, :prompt => true %>
<%= form.submit %>
<% end %>
@@ -1,8 +1,11 @@
<h2>Tasks</h2>
-<strong>Total tasks: <%= @tasks.count %></strong>
-<ul>
- <%= render @tasks %>
-</ul>
+
+<% @lists.each do |list| %>
+ <strong><%= list.name %>: <%= list.tasks.count %></strong>
+ <ul>
+ <%= render list.tasks %>
+ </ul>
+<% end %>
<h2>New Task</h2>
@@ -0,0 +1,29 @@
+<%= 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 :first_name %><br />
+ <%= f.text_field :first_name %>
+ </div>
+ <div class="field">
+ <%= f.label :last_name %><br />
+ <%= f.text_field :last_name %>
+ </div>
+ <div class="field">
+ <%= f.label :email %><br />
+ <%= f.text_field :email %>
+ </div>
+ <div class="actions">
+ <%= f.submit %>
+ </div>
+<% end %>
@@ -0,0 +1,6 @@
+<h1>Editing user</h1>
+
+<%= render 'form' %>
+
+<%= link_to 'Show', @user %> |
+<%= link_to 'Back', users_path %>
@@ -0,0 +1,29 @@
+<h1>Listing users</h1>
+
+<table>
+ <tr>
+ <th>First name</th>
+ <th>Last name</th>
+ <th>Email</th>
+ <th>Last login at</th>
+ <th></th>
+ <th></th>
+ <th></th>
+ </tr>
+
+<% @users.each do |user| %>
+ <tr>
+ <td><%= user.first_name %></td>
+ <td><%= user.last_name %></td>
+ <td><%= user.email %></td>
+ <td><%= time_ago_in_words(user.last_login_at) %></td>
+ <td><%= link_to 'Show', user %></td>
+ <td><%= link_to 'Edit', edit_user_path(user) %></td>
+ <td><%= link_to 'Destroy', user, confirm: 'Are you sure?', method: :delete %></td>
+ </tr>
+<% end %>
+</table>
+
+<br />
+
+<%= link_to 'New User', new_user_path %>
@@ -0,0 +1,5 @@
+<h1>New user</h1>
+
+<%= render 'form' %>
+
+<%= link_to 'Back', users_path %>
@@ -0,0 +1,25 @@
+<p id="notice"><%= notice %></p>
+
+<p>
+ <b>First name:</b>
+ <%= @user.first_name %>
+</p>
+
+<p>
+ <b>Last name:</b>
+ <%= @user.last_name %>
+</p>
+
+<p>
+ <b>Email:</b>
+ <%= @user.email %>
+</p>
+
+<p>
+ <b>Last login at:</b>
+ <%= @user.last_login_at %>
+</p>
+
+
+<%= link_to 'Edit', edit_user_path(@user) %> |
+<%= link_to 'Back', users_path %>
View
@@ -1,8 +1,9 @@
Todo::Application.routes.draw do
+ resources :users
resources :lists
-
resources :tasks
resources :completed_tasks, only: [:create]
+ resource :sessions
# The priority is based upon order of creation:
# first created -> highest priority.
@@ -0,0 +1,12 @@
+class CreateUsers < ActiveRecord::Migration
+ def change
+ create_table :users do |t|
+ t.string :first_name
+ t.string :last_name
+ t.string :email
+ t.timestamp :last_login_at
+
+ t.timestamps
+ end
+ end
+end
Oops, something went wrong. Retry.

0 comments on commit 58fae61

Please sign in to comment.