Skip to content

Commit

Permalink
Finish user edit, update, index, and destroy actions
Browse files Browse the repository at this point in the history
  • Loading branch information
lijay committed Jan 10, 2014
1 parent b8358ff commit 91f7dff
Show file tree
Hide file tree
Showing 24 changed files with 398 additions and 20 deletions.
Binary file modified .DS_Store
Binary file not shown.
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ end

# Use ActiveModel has_secure_password
gem 'bcrypt-ruby', '~> 3.0.0'
gem 'faker', '1.1.2'
gem 'will_paginate', '3.0.4'
gem 'bootstrap-will_paginate', '0.0.9'

# Use unicorn as the app server
# gem 'unicorn'
Expand Down
8 changes: 8 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ GEM
bcrypt-ruby (3.0.1)
bootstrap-sass (2.3.2.0)
sass (~> 3.2)
bootstrap-will_paginate (0.0.9)
will_paginate
builder (3.1.4)
capybara (2.1.0)
mime-types (>= 1.16)
Expand Down Expand Up @@ -57,6 +59,8 @@ GEM
factory_girl_rails (4.2.1)
factory_girl (~> 4.2.0)
railties (>= 3.0.0)
faker (1.1.2)
i18n (~> 0.5)
ffi (1.9.0)
formatador (0.2.4)
guard (2.1.1)
Expand Down Expand Up @@ -187,6 +191,7 @@ GEM
execjs (>= 0.3.0)
multi_json (~> 1.0, >= 1.0.2)
websocket (1.0.7)
will_paginate (3.0.4)
xpath (2.0.0)
nokogiri (~> 1.3)

Expand All @@ -196,10 +201,12 @@ PLATFORMS
DEPENDENCIES
bcrypt-ruby (~> 3.0.0)
bootstrap-sass (= 2.3.2.0)
bootstrap-will_paginate (= 0.0.9)
capybara (= 2.1.0)
childprocess (= 0.3.6)
coffee-rails (~> 4.0.0)
factory_girl_rails (= 4.2.1)
faker (= 1.1.2)
guard-rspec (= 2.5.0)
guard-spork (= 1.5.0)
jbuilder (= 1.0.2)
Expand All @@ -215,3 +222,4 @@ DEPENDENCIES
sqlite3 (= 1.3.8)
turbolinks (= 1.1.1)
uglifier (= 2.1.1)
will_paginate (= 3.0.4)
Binary file modified app/.DS_Store
Binary file not shown.
15 changes: 15 additions & 0 deletions app/assets/stylesheets/custom.css.scss
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,19 @@ 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;
}

}
}
4 changes: 3 additions & 1 deletion app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def create
if user && user.authenticate(params[:session][:password])
#sign in
sign_in user
redirect_to user
redirect_back_or user
else
flash.now[:error] = 'Invalid email/password combination'
render 'new'
Expand All @@ -16,5 +16,7 @@ def create
end

def destroy
sign_out
redirect_to root_url
end
end
45 changes: 42 additions & 3 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
class UsersController < ApplicationController
before_action :signed_in_user, only: [:index, :edit, :update]
before_action :correct_user, only: [:edit, :update]
before_action :admin_user, only: :destroy

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

def show
@user = User.find(params[:id])
end
Expand All @@ -18,14 +26,45 @@ def create
end
end

def edit
end

def update
if @user.update_attributes(user_params)
#Handle a successful update.
flash[:success] = "Profile updated"
redirect_to @user
else
render 'edit'
end
end

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

private

def user_params
params.require(:user).permit(:name, :email, :password, :password_confirmation)
end

def destroy
sign_out
redirect_to root_url
def signed_in_user
unless signed_in?
store_location
redirect_to signin_url, notice: "Please sign in."
end
end

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

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

end
16 changes: 15 additions & 1 deletion app/helpers/sessions_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,24 @@ def current_user
@current_user ||= User.find_by(remember_token: remember_token)
end

def current_user?(user)
user == current_user
end

def sign_out
current_user.update_attribute(:new_remember_token,
current_user.update_attribute(:remember_token,
User.encrypt(User.new_remember_token))
cookies.delete(:remember_token)
self.current_user = nil
end


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

def store_location
session[:return_to] = request.url if request.get?
end
end
Binary file added app/models/.DS_Store
Binary file not shown.
Binary file added app/views/.DS_Store
Binary file not shown.
4 changes: 2 additions & 2 deletions app/views/layouts/_header.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
<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" %>
Expand Down
1 change: 1 addition & 0 deletions app/views/sessions/debug.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
session:<%= debug session[:return_to] %>
8 changes: 8 additions & 0 deletions app/views/users/_user.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<li>
<%= gravatar_for user, size: 52 %>
<%= link_to user.name, user %>
<% if current_user.admin? && !current_user?(user) %>
| <%= link_to "delete", user, method: :delete,
data: { confirm: "You sure?" } %>
<% end %>
</li>
26 changes: 26 additions & 0 deletions app/views/users/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<% 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' %>

<%= 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>
9 changes: 9 additions & 0 deletions app/views/users/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<% provide(:title, 'All users') %>
<h1>All users</h1>
<%= will_paginate %>
<ul class="users">
<% @users.each do |user| %>
<%= render user %>
<% end %>
</ul>
<%= will_paginate %>
5 changes: 5 additions & 0 deletions db/migrate/20140106063543_add_admin_to_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddAdminToUsers < ActiveRecord::Migration
def change
add_column :users, :admin, :boolean, default: false
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20131130175757) do
ActiveRecord::Schema.define(version: 20140106063543) do

create_table "users", force: true do |t|
t.string "name"
Expand All @@ -20,6 +20,7 @@
t.datetime "updated_at"
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
Expand Down
19 changes: 19 additions & 0 deletions lib/tasks/sample_data.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace :db do
desc "Fill database with sample data"
task populate: :environment do
User.create!(name: "Example User",
email: "[email protected]",
password: "foobar",
password_confirmation: "foobar",
admin: true)
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
Binary file added spec/.DS_Store
Binary file not shown.
8 changes: 6 additions & 2 deletions spec/factories.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
FactoryGirl.define do
factory :user do
name "Michael Hartl"
email "michael@example.com"
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
12 changes: 12 additions & 0 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,20 @@
it { should respond_to(:password_confirmation) }
it { should respond_to(:remember_token) }
it { should respond_to(:authenticate) }
it { should respond_to(:admin) }

it { should be_valid }
it { should_not be_admin }

describe "with admin attribute set to 'true'" do
before do
@user.save!
@user.toggle!(:admin)
end

it { should be_admin }
end


describe "when name is not present" do
before { @user.name = " " }
Expand Down
Loading

0 comments on commit 91f7dff

Please sign in to comment.