Permalink
Browse files

Finish user edit, update, index, and destroy actions

  • Loading branch information...
1 parent 518bd02 commit 726992d3ef9ca5a14fb64987b91aec5072eb155e Jason Kim committed Mar 17, 2012
View
@@ -3,7 +3,9 @@ source 'https://rubygems.org'
gem 'rails', '3.2.2'
gem 'bootstrap-sass', '2.0.0'
gem 'bcrypt-ruby', '3.0.1'
-
+gem 'faker', '1.0.1'
+gem 'will_paginate', '3.0.3'
+gem 'bootstrap-will_paginate', '0.0.5'
group :development, :test do
gem 'sqlite3', '1.3.5'
View
@@ -32,6 +32,8 @@ GEM
arel (3.0.2)
bcrypt-ruby (3.0.1)
bootstrap-sass (2.0.0)
+ bootstrap-will_paginate (0.0.5)
+ will_paginate
builder (3.0.0)
capybara (1.1.2)
mime-types (>= 1.16)
@@ -69,6 +71,8 @@ GEM
factory_girl_rails (1.4.0)
factory_girl (~> 2.3.0)
railties (>= 3.0.0)
+ faker (1.0.1)
+ i18n (~> 0.4)
ffi (1.0.11)
gherkin (2.9.0)
json (>= 1.4.6)
@@ -152,6 +156,7 @@ GEM
uglifier (1.2.3)
execjs (>= 0.3.0)
multi_json (>= 1.0.2)
+ will_paginate (3.0.3)
xpath (0.1.4)
nokogiri (~> 1.3)
@@ -162,15 +167,18 @@ DEPENDENCIES
annotate (~> 2.4.1.beta)
bcrypt-ruby (= 3.0.1)
bootstrap-sass (= 2.0.0)
+ bootstrap-will_paginate (= 0.0.5)
capybara (= 1.1.2)
coffee-rails (= 3.2.2)
cucumber-rails (= 1.2.1)
database_cleaner (= 0.7.0)
factory_girl_rails (= 1.4.0)
+ faker (= 1.0.1)
jquery-rails (= 2.0.0)
pg (= 0.12.2)
rails (= 3.2.2)
rspec-rails (= 2.8.1)
sass-rails (= 3.2.4)
sqlite3 (= 1.3.5)
uglifier (= 1.2.3)
+ will_paginate (= 3.0.3)
@@ -168,4 +168,19 @@ input, textarea, select, .uneditable-input {
.field_with_errors {
@extend .control-group;
@extend .error;
- }
+ }
+
+ /* users index */
+
+.users {
+ list-style: none;
+ margin: 0;
+ li {
+ overflow: auto;
+ padding: 10px 0;
+ border-top: 1px solid $grayLighter;
+ &:last-child {
+ border-bottom: 1px solid $grayLighter
+ }
+ }
+}
@@ -6,7 +6,7 @@ def create
user = User.find_by_email(params[:session][:email])
if user && user.authenticate(params[:session][:password])
sign_in user
- redirect_to user
+ redirect_back_or user
else
flash.now[:error] = 'Invalid email/password combination'
render 'new'
@@ -1,4 +1,13 @@
class UsersController < ApplicationController
+ before_filter :signed_in_user, only: [:index, :edit, :update]
+ before_filter :correct_user, only: [:edit, :update]
+ before_filter :admin_user, only: :destroy
+
+
+ def index
+ @users = User.paginate(page: params[:page])
+ end
+
def create
@user = User.new(params[:user])
if @user.save
@@ -18,5 +27,39 @@ def new
@user = User.new
end
+ def edit
+ end
+
+ def update
+ if @user.update_attributes(params[:user])
+ flash[:success] = "Profile updated"
+ sign_in @user
+ redirect_to @user
+ else
+ render 'edit'
+ end
+ end
+
+ def destroy
+ User.find(params[:id]).destroy
+ flash[:success] = "User destroyed."
+ redirect_to users_path
+ end
+ private
+ def signed_in_user
+ unless signed_in?
+ store_location
+ redirect_to signin_path, notice: "Please sign in."
+ end
+ 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
@@ -13,6 +13,10 @@ def current_user
@current_user ||= user_from_remember_token
end
+ def current_user?(user)
+ user == current_user
+ end
+
def signed_in?
!current_user.nil?
end
@@ -22,12 +26,24 @@ def sign_out
cookies.delete(:remember_token)
end
+ def redirect_back_or(default)
+ redirect_to(session[:return_to] || default)
+ clear_return_to
+ end
+
+ def store_location
+ session[:return_to] = request.fullpath
+ end
+
private
def user_from_remember_token
remember_token = cookies[:remember_token]
User.find_by_remember_token(remember_token) unless remember_token.nil?
-
+ end
+
+ def clear_return_to
+ session.delete(:return_to)
end
end
@@ -1,9 +1,10 @@
module UsersHelper
# Returns the Gravatar (http://gravatar.com/) for the given user.
- def gravatar_for(user)
- gravatar_id = Digest::MD5::hexdigest(user.email.downcase)
- gravatar_url = "http://gravatar.com/avatar/#{gravatar_id}"
+ def gravatar_for(user, options = { size: 50 })
+ gravatar_id = Digest::MD5::hexdigest(user.email.downcase)
+ size = options[:size]
+ gravatar_url = "http://gravatar.com/avatar/#{gravatar_id}.png?s=#{size}"
image_tag(gravatar_url, alt: user.name, class: "gravatar")
end
end
@@ -1,3 +1,4 @@
+<header>
<header class="navbar navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
@@ -7,14 +8,14 @@
<li><%= link_to "Home", root_path %></li>
<li><%= link_to "Help", help_path %></li>
<% if signed_in? %>
- <li><%= link_to "Users", '#' %></li>
+ <li><%= link_to "Users", users_path %></li>
<li id="fat-menu" class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
Account <b class="caret"></b>
</a>
<ul class="dropdown-menu">
<li><%= link_to "Profile", current_user %></li>
- <li><%= link_to "Settings", '#' %></li>
+ <li><%= link_to "Settings", edit_user_path(current_user) %></li>
<li class="divider"></li>
<li>
<%= link_to "Sign out", signout_path, method: "delete" %>
@@ -0,0 +1,7 @@
+<li>
+ <%= gravatar_for user, size: 52 %>
+ <%= link_to user.name, user %>
+ <% if current_user.admin? && !current_user?(user) %>
+ | <%= link_to "delete", user, method: :delete, confirm: "You sure?" %>
+ <% end %>
+</li>
@@ -0,0 +1,27 @@
+<% provide(:title, "Edit user") %>
+<h1>Update your profile</h1>
+
+<div class="row">
+ <div class="span6 offset3">
+ <%= form_for(@user) do |f| %>
+ <%= render 'shared/error_messages', :object => f.object %>
+
+ <%= f.label :name %>
+ <%= f.text_field :name %>
+
+ <%= f.label :email %>
+ <%= f.text_field :email %>
+
+ <%= f.label :password %>
+ <%= f.password_field :password %>
+
+ <%= f.label :password_confirmation, "Confirm Password" %>
+ <%= f.password_field :password_confirmation %>
+
+ <%= f.submit "Save changes", :class => "btn btn-large btn-primary" %>
+ <% end %>
+
+ <%= gravatar_for @user %>
+ <a href="http://gravatar.com/emails">change</a>
+ </div>
+</div>
@@ -0,0 +1,10 @@
+<% provide(:title, 'All users') %>
+<h1>All users</h1>
+
+<%= will_paginate %>
+
+<ul class="users">
+ <%= render @users %>
+</ul>
+
+<%= will_paginate %>
View
@@ -62,4 +62,4 @@ class Application < Rails::Application
# Version of your assets, change this if you want to expire all your assets
config.assets.version = '1.0'
end
-end
+end
@@ -0,0 +1,5 @@
+class AddAdminToUsers < ActiveRecord::Migration
+ def change
+ add_column :users, :admin, :boolean, default: false
+ end
+end
View
@@ -11,15 +11,16 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20120315044928) do
+ActiveRecord::Schema.define(:version => 20120317061724) do
create_table "users", :force => true do |t|
t.string "name"
t.string "email"
- t.datetime "created_at", :null => false
- t.datetime "updated_at", :null => false
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
t.string "password_digest"
t.string "remember_token"
+ t.boolean "admin", :default => false
end
add_index "users", ["email"], :name => "index_users_on_email", :unique => true
View
@@ -0,0 +1,19 @@
+namespace :db do
+ desc "Fill database with sample data"
+ task populate: :environment do
+ 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
View
@@ -1,8 +1,12 @@
FactoryGirl.define do
factory :user do
- name "Michael Hartl"
- email "michael@example.com"
- password "foobar"
+ sequence(:name) { |n| "Person #{n}" }
+ sequence(:email) { |n| "person_#{n}@example.com"}
+ password "foobar"
password_confirmation "foobar"
+
+ factory :admin do
+ admin true
+ end
end
end
View
@@ -25,8 +25,17 @@
it { should respond_to(:remember_token) }
it { should respond_to(:authenticate) }
-
+ it { should respond_to(:admin) }
+ it { should respond_to(:authenticate) }
+
it { should be_valid }
+ it { should_not be_admin }
+
+ describe "with admin attribute set to 'true'" do
+ before { @user.toggle!(:admin) }
+
+ it { should be_admin }
+ end
describe "when name is not present" do
before { @user.name = " " }
Oops, something went wrong.

0 comments on commit 726992d

Please sign in to comment.