Skip to content

Commit

Permalink
Done with user edit/update, index and destroy actions
Browse files Browse the repository at this point in the history
  • Loading branch information
simozstudios committed May 9, 2011
1 parent 4994008 commit 98bcb47
Show file tree
Hide file tree
Showing 20 changed files with 389 additions and 7 deletions.
3 changes: 2 additions & 1 deletion Gemfile
Expand Up @@ -6,12 +6,13 @@ gem 'rails', '3.0.0'
# gem 'rails', :git => 'git://github.com/rails/rails.git'

gem 'sqlite3-ruby', :require => 'sqlite3'

gem 'gravatar_image_tag', '0.1.0'
gem 'will_paginate', '3.0.pre2'

group :development do
gem 'rspec-rails', '2.0.1'
gem 'annotate-models', '1.0.4'
gem 'faker', '0.3.1'
end

group :test do
Expand Down
4 changes: 4 additions & 0 deletions Gemfile.lock
Expand Up @@ -39,6 +39,7 @@ GEM
factory_girl_rails (1.0)
factory_girl (~> 1.3)
rails (>= 3.0.0.beta4)
faker (0.3.1)
gravatar_image_tag (0.1.0)
i18n (0.4.2)
mail (2.2.15)
Expand Down Expand Up @@ -91,16 +92,19 @@ GEM
nokogiri (>= 1.2.0)
rack (>= 1.0)
rack-test (>= 0.5.3)
will_paginate (3.0.pre2)

PLATFORMS
ruby

DEPENDENCIES
annotate-models (= 1.0.4)
factory_girl_rails (= 1.0)
faker (= 0.3.1)
gravatar_image_tag (= 0.1.0)
rails (= 3.0.0)
rspec (= 2.0.1)
rspec-rails (= 2.0.1)
sqlite3-ruby
webrat (= 0.7.1)
will_paginate (= 3.0.pre2)
2 changes: 1 addition & 1 deletion app/controllers/sessions_controller.rb
Expand Up @@ -12,7 +12,7 @@ def create
render 'new'
else
sign_in user
redirect_to user
redirect_back_or user
end
end

Expand Down
49 changes: 48 additions & 1 deletion app/controllers/users_controller.rb
@@ -1,9 +1,18 @@
class UsersController < ApplicationController
before_filter :authenticate, :only => [:index, :edit, :update, :destroy]
before_filter :correct_user, :only => [:edit, :update]
before_filter :admin_user, :only => :destroy

def new
@user = User.new
@title = "Sign up"
end

def index
@title = "All users"
@users = User.paginate(:page => params[:page])
end

def show
@user = User.find(params[:id])
@title = @user.name
Expand All @@ -19,5 +28,43 @@ def create
@title = "Sign up"
render 'new'
end
end
end

def edit
#@user = User.find(params[:id])
@title = "Edit user"
end

def update
#@user = User.find(params[:id])
if @user.update_attributes(params[:user])
flash[:success] = "Profile updated."
redirect_to @user
else
@title = "Edit user"
render 'edit'
end
end

def destroy
User.find(params[:id]).destroy
flash[:success] = "User destroyed."
redirect_to users_path
end

private

def authenticate
deny_access unless signed_in?
end

def correct_user
@user = User.find(params[:id])
redirect_to(root_path) unless current_user?(@user)
end

def admin_user
redirect_to(root_path) unless current_user.admin?
end

end
22 changes: 22 additions & 0 deletions app/helpers/sessions_helper.rb
Expand Up @@ -22,6 +22,20 @@ def sign_out
current_user = nil
end

def current_user?(user)
user == current_user
end

def deny_access
store_location
redirect_to signin_path, :notice => "Please sign in to access this page."
end

def redirect_back_or(default)
redirect_to(session[:return_to] || default)
clear_return_to
end

private

def user_from_remember_token
Expand All @@ -31,4 +45,12 @@ def user_from_remember_token
def remember_token
cookies.signed[:remember_token] || [nil, nil]
end

def store_location
session[:return_to] = request.fullpath
end

def clear_return_to
session[:return_to] = nil
end
end
2 changes: 2 additions & 0 deletions app/views/layouts/_header.html.erb
Expand Up @@ -4,7 +4,9 @@
<ul>
<li><%= link_to "Home", root_path %></li>
<% if signed_in? %>
<li><%= link_to "Users", users_path %></li>
<li><%= link_to "Profile", current_user %></li>
<li><%= link_to "Settings", edit_user_path(current_user) %></li>
<% end %>
<li><%= link_to "Help", help_path %></li>
<% if signed_in? %>
Expand Down
1 change: 1 addition & 0 deletions app/views/layouts/application.html.erb
Expand Up @@ -4,6 +4,7 @@
<title><%= title %></title>
<%= csrf_meta_tag %>
<%= render 'layouts/stylesheets' %>
<%= javascript_include_tag :defaults %>
</head>
<body>
<div class="container">
Expand Down
3 changes: 2 additions & 1 deletion app/views/shared/_error_messages.html.erb
@@ -1,7 +1,8 @@
<% if @user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@user.errors.count, "error") %>
prohibited this user from being saved:</h2>
prohibited this <%= object.class.to_s.underscore.humanize.downcase %>
from being saved:</h2>
<p>There were problems with the following fields:</p>
<ul>
<% @user.errors.full_messages.each do |msg| %>
Expand Down
8 changes: 8 additions & 0 deletions app/views/users/_user.html.erb
@@ -0,0 +1,8 @@
<li>
<%= gravatar_for user, :size => 30 %>
<%= link_to user.name, user %>
<% if current_user.admin? %>
| <%= link_to "delete", user, :method => :delete, :confirm => "You sure?",
:title => "Delete #{user.name}" %>
<% end %>
</li>
28 changes: 28 additions & 0 deletions app/views/users/edit.html.erb
@@ -0,0 +1,28 @@
<h1>Edit user</h1>

<%= form_for(@user) do |f| %>
<%= render 'shared/error_messages', :object => f.object %>
<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="field">
<%= f.label :password %><br />
<%= f.password_field :password %>
</div>
<div class="field">
<%= f.label :password_confirmation, "Confirmation" %><br />
<%= f.password_field :password_confirmation %>
</div>
<div class="actions">
<%= f.submit "Update" %>
</div>
<% end %>
<div>
<%= gravatar_for @user %>
<a href="http://gravatar.com/emails">change</a>
</div>
9 changes: 9 additions & 0 deletions app/views/users/index.html.erb
@@ -0,0 +1,9 @@
<h1>All users</h1>

<%= will_paginate %>

<ul class="users">
<%= render @users %>
</ul>

<%= will_paginate %>
2 changes: 1 addition & 1 deletion app/views/users/new.html.erb
@@ -1,7 +1,7 @@
<h1>Sign up</h1>

<%= form_for(@user) do |f| %>
<%= render 'shared/error_messages' %>
<%= render 'shared/error_messages', :object => f.object %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
Expand Down
9 changes: 9 additions & 0 deletions db/migrate/20110508235604_add_admin_to_users.rb
@@ -0,0 +1,9 @@
class AddAdminToUsers < ActiveRecord::Migration
def self.up
add_column :users, :admin, :boolean, :default => false
end

def self.down
remove_column :users, :admin
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20110422040429) do
ActiveRecord::Schema.define(:version => 20110508235604) do

create_table "users", :force => true do |t|
t.string "name"
Expand All @@ -19,6 +19,7 @@
t.datetime "updated_at"
t.string "encrypted_password"
t.string "salt"
t.boolean "admin", :default => false
end

add_index "users", ["email"], :name => "index_users_on_email", :unique => true
Expand Down
22 changes: 22 additions & 0 deletions lib/tasks/sample_data.rake
@@ -0,0 +1,22 @@
require 'faker'

namespace :db do
desc "Fill database with sample data"
task :populate => :environment do
Rake::Task['db:reset'].invoke
admin = User.create!(:name => "Example User",
:email => "example@railstutorial.org",
:password => "foobar",
:password_confirmation => "foobar")
admin.toggle!(:admin)
99.times do |n|
name = Faker::Name.name
email = "example-#{n+1}@railstutorial.org"
password = "password"
User.create!(:name => name,
:email => email,
:password => password,
:password_confirmation => password)
end
end
end
10 changes: 10 additions & 0 deletions public/stylesheets/custom.css
Expand Up @@ -146,6 +146,16 @@ div.field, div.actions {
margin-bottom: 10px;
}

/* User index page */

ul.users {
margin-top: 1em;
}

.users li {
list-style: none;
}

/* Error Messages */


Expand Down

0 comments on commit 98bcb47

Please sign in to comment.