Changeset 271
- Timestamp:
- 03/10/08 23:05:24 (7 months ago)
- Files:
-
- incubator/jumpstart/app/controllers/users_controller.rb (modified) (3 diffs)
- incubator/jumpstart/app/helpers/application_helper.rb (modified) (1 diff)
- incubator/jumpstart/app/views/layouts/application.html.erb (modified) (1 diff)
- incubator/jumpstart/app/views/sessions/new.html.erb (modified) (1 diff)
- incubator/jumpstart/app/views/users/edit.html.erb (added)
- incubator/jumpstart/app/views/users/new.html.erb (modified) (2 diffs)
- incubator/jumpstart/app/views/welcome/index.html.erb (modified) (1 diff)
- incubator/jumpstart/db/migrate/001_create_users.rb (modified) (1 diff)
- incubator/jumpstart/db/schema.rb (modified) (1 diff)
- incubator/jumpstart/doc/README_FOR_APP (modified) (3 diffs)
- incubator/jumpstart/test/fixtures/users.yml (modified) (2 diffs)
- incubator/jumpstart/test/functional/users_controller_test.rb (modified) (3 diffs)
- incubator/jumpstart/test/unit/application_helper_test.rb (added)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
incubator/jumpstart/app/controllers/users_controller.rb
r269 r271 1 1 class UsersController < ApplicationController 2 before_filter :login_required, :only => [:edit, :update] 3 before_filter :find_user, :only => [:edit, :update] 4 2 5 # render new.rhtml 3 6 def new 7 @user = flash[:user] || User.new 4 8 end 5 9 … … 11 15 # reset_session 12 16 @user = User.new(params[:user]) 13 @user.save 14 if @user.errors.empty? 17 if @user.save 15 18 self.current_user = @user 16 19 redirect_back_or_default('/') 17 20 flash[:notice] = "Thanks for signing up!" 18 21 else 19 render :action => 'new' 22 flash[:user] = @user 23 redirect_to :action => 'new' 20 24 end 21 25 end … … 32 36 end 33 37 end 38 39 def edit 40 @user = flash[:user] || current_user 41 end 42 43 def update 44 @user = current_user 45 @user.update_attributes(params[:user]) 46 if @user.save 47 flash[:notice] = "Your account has been updated." 48 redirect_to root_path 49 else 50 flash[:user] = @user 51 redirect_to edit_user_path @user 52 end 53 end 54 55 protected 56 def find_user 57 # we don't want people trying to edit others records 58 unless current_user.id == params[:id].to_i 59 flash[:warning] = "We couldn't find what you were looking for." 60 puts 'redirecting' 61 redirect_to root_path 62 end 63 end 34 64 end incubator/jumpstart/app/helpers/application_helper.rb
r214 r271 1 1 # Methods added to this helper will be available to all templates in the application. 2 2 module ApplicationHelper 3 def display_flash 4 # Provide an array of allowable flash types in environment.rb for a global defn. Otherwise it will use these. 5 # List these flash types in order of precedence 6 flash_types = [:error, :warning, :notice ] 7 8 # This will only display the first, most important message 9 flash_type = flash_types.detect{ |a| flash.keys.include?(a) } 10 11 # Return the HTML to render 12 flash_type ? content_tag(:div, flash[flash_type], :id => "flash", :class => "flash #{flash_type.to_s}") : "" 13 end 3 14 end incubator/jumpstart/app/views/layouts/application.html.erb
r251 r271 7 7 <body> 8 8 <%= breadcrumb_trail @crumbs %> 9 10 <%= display_flash %> 11 9 12 <%= yield %> 10 13 </body> incubator/jumpstart/app/views/sessions/new.html.erb
r270 r271 22 22 <p> 23 23 <%= submit_tag 'Log in' %> 24 <%= link_to 'Cancel', root_path %> 24 25 </p> 25 26 <% end -%> incubator/jumpstart/app/views/users/new.html.erb
r270 r271 6 6 <%= error_messages_for :user %> 7 7 8 <% form_for :user, :url => users_pathdo |f| -%>8 <% form_for @user do |f| -%> 9 9 <p> 10 10 <label for="username">Username</label><br/> … … 29 29 <p> 30 30 <%= submit_tag 'Sign up' %> 31 <%= link_to 'Cancel', root_path %> 31 32 </p> 32 33 <% end -%> incubator/jumpstart/app/views/welcome/index.html.erb
r251 r271 10 10 11 11 <p> 12 Need an account? <%= link_to 'Sign up now', signup_path %><br /> 13 Already have an account? <%= link_to 'Login now', login_path %><br /> 12 <% if logged_in? %> 13 <%= link_to 'Edit your account', edit_user_path(current_user) %><br /> 14 <%= link_to 'Logout', logout_path %> 15 <% else %> 16 Need an account? <%= link_to 'Sign up now', signup_path %><br /> 17 Already have an account? <%= link_to 'Login now', login_path %> 18 <% end %> 14 19 </p> incubator/jumpstart/db/migrate/001_create_users.rb
r270 r271 7 7 t.string :remember_token 8 8 t.datetime :remember_token_expires_at 9 t.string :identity_url10 9 t.timestamps 11 10 end incubator/jumpstart/db/schema.rb
r270 r271 18 18 t.string "remember_token" 19 19 t.datetime "remember_token_expires_at" 20 t.string "identity_url"21 20 t.datetime "created_at" 22 21 t.datetime "updated_at" incubator/jumpstart/doc/README_FOR_APP
r231 r271 1 1 Welcome to Jumpstart 2 3 required gems 4 bcrypt 5 ruby-openid 6 test-spec 7 mocha 2 8 3 9 Getting Started … … 18 24 19 25 * Sessions - login/logout 20 * Users - user creation, activation, and password reset21 * Welcome - this page26 * Users - user creation, password reset, user maintenance 27 * Welcome 22 28 23 29 Plugins - All plugins are maintained using svn:external … … 26 32 * test_spec_on_rails - BDD using test/spec 27 33 * fixture_replacement2 34 * seo_helper incubator/jumpstart/test/fixtures/users.yml
r270 r271 3 3 username: quentin 4 4 email: quentin@example.com 5 crypted_password: 00742970dc9e6319f8019fd54864d3ea740f04b1 # test5 crypted_password: $2a$10$uO6IK8AkWyCpCSbwUerQAu6eoYrF/AWsM9yndBdaQgQcpZd8hXiX6 # password 6 6 created_at: <%= 5.days.ago.to_s :db %> 7 activation_code: 8f24789ae988411ccf33ab0c30fe9106fab32e9b8 activated_at: <%= 5.days.ago.to_s :db %>9 7 10 8 aaron: … … 12 10 username: aaron 13 11 email: aaron@example.com 14 crypted_password: 00742970dc9e6319f8019fd54864d3ea740f04b1 # test12 crypted_password: $2a$10$uO6IK8AkWyCpCSbwUerQAu6eoYrF/AWsM9yndBdaQgQcpZd8hXiX6 # password 15 13 created_at: <%= 1.days.ago.to_s :db %> 16 activation_code: 8f24789ae988411ccf33ab0c30fe9106fab32e9a17 14 incubator/jumpstart/test/functional/users_controller_test.rb
r269 r271 3 3 describe "Signing up (GET /signup)", ActionController::TestCase do 4 4 tests UsersController 5 6 it "should load a new user" do 7 get :new 8 assigns(:user).should.not.be.nil 9 end 5 10 6 11 it "should use the 'new' template" do … … 18 23 end 19 24 25 it "should redirect to signup when user cannot be saved" do 26 User.any_instance.stubs(:save).returns(false) 27 post :create, :user => { } 28 should.redirect_to signup_path 29 end 30 31 it "should include user in flash when user cannot be saved" do 32 User.any_instance.stubs(:save).returns(false) 33 post :create, :user => { } 34 flash[:user].should.not.be.nil 35 end 36 20 37 it "should create user" do 21 38 post :create, :user => { } … … 54 71 end 55 72 end 73 74 describe "Editing account (GET /users/:id/edit)", ActionController::TestCase do 75 tests UsersController 76 77 it "should require login" do 78 get :edit, :id => 1 79 should.redirect_to login_url 80 end 81 82 it "should load the logged in user" do 83 user = create_user 84 login_as user 85 86 get :edit, :id => user.id 87 assigns(:user).should == user 88 end 89 90 it "should use the 'edit' template" do 91 user = create_user 92 login_as user 93 94 get :edit, :id => user.id 95 template.should.be('edit') 96 end 97 end 98 99 describe "Updating an account (PUT /users/:id)", ActionController::TestCase do 100 tests UsersController 101 102 it "should require login" do 103 put :update, :id => 1 104 should.redirect_to login_url 105 end 106 107 it "should redirect to edit when the user cannot be saved" do 108 User.any_instance.stubs(:save).returns(false) 109 user = create_user 110 login_as user 111 112 put :update, :id => user.id 113 should.redirect_to edit_user_path user 114 end 115 116 it "should include user in flash when the user cannot be saved" do 117 User.any_instance.stubs(:save).returns(false) 118 user = create_user 119 login_as user 120 121 put :update, :id => user.id 122 flash[:user].should.not.be.nil 123 end 124 125 it "should redirect to the root path when the user is updated" do 126 User.any_instance.stubs(:save).returns(true) 127 user = create_user 128 login_as user 129 130 put :update, :id => user.id 131 should.redirect_to root_path 132 end 133 134 end 135
