Changeset 271

Show
Ignore:
Timestamp:
03/10/08 23:05:24 (7 months ago)
Author:
gdagley
Message:

correct user fixture passwords
add display_flash helper
add edit user

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • incubator/jumpstart/app/controllers/users_controller.rb

    r269 r271  
    11class UsersController < ApplicationController 
     2  before_filter :login_required, :only => [:edit, :update] 
     3  before_filter :find_user, :only => [:edit, :update] 
     4   
    25  # render new.rhtml 
    36  def new 
     7    @user = flash[:user] || User.new 
    48  end 
    59 
     
    1115    # reset_session 
    1216    @user = User.new(params[:user]) 
    13     @user.save 
    14     if @user.errors.empty? 
     17    if @user.save 
    1518      self.current_user = @user 
    1619      redirect_back_or_default('/') 
    1720      flash[:notice] = "Thanks for signing up!" 
    1821    else 
    19       render :action => 'new' 
     22      flash[:user] = @user  
     23      redirect_to :action => 'new' 
    2024    end 
    2125  end 
     
    3236    end 
    3337  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 
    3464end 
  • incubator/jumpstart/app/helpers/application_helper.rb

    r214 r271  
    11# Methods added to this helper will be available to all templates in the application. 
    22module 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 
    314end 
  • incubator/jumpstart/app/views/layouts/application.html.erb

    r251 r271  
    77  <body> 
    88    <%= breadcrumb_trail @crumbs %> 
     9     
     10    <%= display_flash %> 
     11     
    912    <%= yield %> 
    1013  </body> 
  • incubator/jumpstart/app/views/sessions/new.html.erb

    r270 r271  
    2222  <p> 
    2323    <%= submit_tag 'Log in' %> 
     24    <%= link_to 'Cancel', root_path %> 
    2425  </p> 
    2526<% end -%> 
  • incubator/jumpstart/app/views/users/new.html.erb

    r270 r271  
    66<%= error_messages_for :user %> 
    77 
    8 <% form_for :user, :url => users_path do |f| -%> 
     8<% form_for @user do |f| -%> 
    99  <p> 
    1010    <label for="username">Username</label><br/> 
     
    2929  <p> 
    3030    <%= submit_tag 'Sign up' %> 
     31    <%= link_to 'Cancel', root_path %> 
    3132  </p> 
    3233<% end -%> 
  • incubator/jumpstart/app/views/welcome/index.html.erb

    r251 r271  
    1010 
    1111<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 %> 
    1419</p> 
  • incubator/jumpstart/db/migrate/001_create_users.rb

    r270 r271  
    77      t.string    :remember_token 
    88      t.datetime  :remember_token_expires_at 
    9       t.string    :identity_url 
    109      t.timestamps 
    1110    end 
  • incubator/jumpstart/db/schema.rb

    r270 r271  
    1818    t.string   "remember_token" 
    1919    t.datetime "remember_token_expires_at" 
    20     t.string   "identity_url" 
    2120    t.datetime "created_at" 
    2221    t.datetime "updated_at" 
  • incubator/jumpstart/doc/README_FOR_APP

    r231 r271  
    11Welcome to Jumpstart 
     2 
     3required gems 
     4  bcrypt 
     5  ruby-openid 
     6  test-spec 
     7  mocha 
    28 
    39Getting Started 
     
    1824 
    1925* Sessions - login/logout 
    20 * Users - user creation, activation, and password reset 
    21 * Welcome - this page 
     26* Users - user creation, password reset, user maintenance 
     27* Welcome 
    2228 
    2329Plugins - All plugins are maintained using svn:external 
     
    2632* test_spec_on_rails - BDD using test/spec 
    2733* fixture_replacement2 
     34* seo_helper 
  • incubator/jumpstart/test/fixtures/users.yml

    r270 r271  
    33  username: quentin 
    44  email: quentin@example.com 
    5   crypted_password: 00742970dc9e6319f8019fd54864d3ea740f04b1 # test 
     5  crypted_password: $2a$10$uO6IK8AkWyCpCSbwUerQAu6eoYrF/AWsM9yndBdaQgQcpZd8hXiX6 # password 
    66  created_at: <%= 5.days.ago.to_s :db %> 
    7   activation_code: 8f24789ae988411ccf33ab0c30fe9106fab32e9b  
    8   activated_at: <%= 5.days.ago.to_s :db %>  
    97 
    108aaron: 
     
    1210  username: aaron 
    1311  email: aaron@example.com 
    14   crypted_password: 00742970dc9e6319f8019fd54864d3ea740f04b1 # test 
     12  crypted_password: $2a$10$uO6IK8AkWyCpCSbwUerQAu6eoYrF/AWsM9yndBdaQgQcpZd8hXiX6 # password 
    1513  created_at: <%= 1.days.ago.to_s :db %> 
    16   activation_code: 8f24789ae988411ccf33ab0c30fe9106fab32e9a  
    1714 
  • incubator/jumpstart/test/functional/users_controller_test.rb

    r269 r271  
    33describe "Signing up (GET /signup)", ActionController::TestCase  do 
    44  tests UsersController 
     5 
     6  it "should load a new user" do 
     7    get :new 
     8    assigns(:user).should.not.be.nil 
     9  end 
    510 
    611  it "should use the 'new' template" do 
     
    1823  end 
    1924   
     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 
    2037  it "should create user" do 
    2138    post :create, :user => { } 
     
    5471  end   
    5572end 
     73 
     74describe "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 
     97end 
     98 
     99describe "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     
     134end 
     135