Skip to content

Commit

Permalink
adding edit profile page to nifty:authentication - closes ryanb#54
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanb committed Oct 15, 2010
1 parent 7a512f6 commit 94404ef
Show file tree
Hide file tree
Showing 14 changed files with 176 additions and 50 deletions.
14 changes: 9 additions & 5 deletions features/nifty_authentication.feature
Expand Up @@ -11,6 +11,7 @@ Feature: Nifty Authentication Generator
| app/controllers/users_controller.rb |
| app/helpers/users_helper.rb |
| app/views/users/new.html.erb |
| app/views/users/edit.html.erb |
| app/controllers/sessions_controller.rb |
| app/helpers/sessions_helper.rb |
| app/views/sessions/new.html.erb |
Expand All @@ -26,6 +27,7 @@ Feature: Nifty Authentication Generator
| match 'login' => 'sessions#new', :as => :login |
| match 'logout' => 'sessions#destroy', :as => :logout |
| match 'signup' => 'users#new', :as => :signup |
| match 'user/edit' => 'users#edit', :as => :edit_user |
And I should see "include ControllerAuthentication" in file "app/controllers/application_controller.rb"
And I should see "gem 'mocha', :group => :test" in file "Gemfile"
And I should see "gem 'bcrypt-ruby', :require => 'bcrypt'" in file "Gemfile"
Expand All @@ -41,6 +43,7 @@ Feature: Nifty Authentication Generator
| app/controllers/accounts_controller.rb |
| app/helpers/accounts_helper.rb |
| app/views/accounts/new.html.erb |
| app/views/accounts/edit.html.erb |
| app/controllers/current_sessions_controller.rb |
| app/helpers/current_sessions_helper.rb |
| app/views/current_sessions/new.html.erb |
Expand All @@ -49,11 +52,12 @@ Feature: Nifty Authentication Generator
| test/functional/accounts_controller_test.rb |
| test/functional/current_sessions_controller_test.rb |
And I should see the following in file "config/routes.rb"
| resources :current_sessions |
| resources :accounts |
| match 'login' => 'current_sessions#new', :as => :login |
| match 'logout' => 'current_sessions#destroy', :as => :logout |
| match 'signup' => 'accounts#new', :as => :signup |
| resources :current_sessions |
| resources :accounts |
| match 'login' => 'current_sessions#new', :as => :login |
| match 'logout' => 'current_sessions#destroy', :as => :logout |
| match 'signup' => 'accounts#new', :as => :signup |
| match 'account/edit' => 'accounts#edit', :as => :edit_account |
When I run "rails g nifty:layout -f"
And I run "rake db:migrate"
Then I should successfully run "rake test"
2 changes: 1 addition & 1 deletion features/nifty_scaffold.feature
Expand Up @@ -21,7 +21,7 @@ Feature: Nifty Scaffold Generator
And I should successfully run "rails g nifty:scaffold Project -f"
And I add "gem 'mocha', :group => :test" to file "Gemfile"
Then I should successfully run "rake test"
@focus

Scenario: Generate scaffold with rspec tests
Given a new Rails app
When I run "rails g nifty:scaffold Project name:string --rspec"
Expand Down
Expand Up @@ -37,6 +37,8 @@ def create_helper_files

def create_view_files
template "views/#{view_language}/signup.html.#{view_language}", "app/views/#{user_plural_name}/new.html.#{view_language}"
template "views/#{view_language}/edit.html.#{view_language}", "app/views/#{user_plural_name}/edit.html.#{view_language}"
template "views/#{view_language}/_form.html.#{view_language}", "app/views/#{user_plural_name}/_form.html.#{view_language}"
template "views/#{view_language}/login.html.#{view_language}", "app/views/#{session_plural_name}/new.html.#{view_language}"
end

Expand All @@ -50,6 +52,7 @@ def create_routes
route "match 'login' => '#{session_plural_name}#new', :as => :login"
route "match 'logout' => '#{session_plural_name}#destroy', :as => :logout"
route "match 'signup' => '#{user_plural_name}#new', :as => :signup"
route "match '#{user_singular_name}/edit' => '#{user_plural_name}#edit', :as => :edit_#{user_singular_name}"
end

def create_migration
Expand Down
Expand Up @@ -3,7 +3,8 @@
# common example you might add to your application layout file.
#
# <%% if logged_in? %>
# Welcome <%%= current_<%= user_singular_name %>.username %>! Not you?
# Welcome <%%= current_<%= user_singular_name %>.username %>.
# <%%= link_to "Edit profile", edit_<%= user_singular_name %>_path %> or
# <%%= link_to "Log out", logout_path %>
# <%% else %>
# <%%= link_to "Sign up", signup_path %> or
Expand Down Expand Up @@ -55,6 +56,6 @@ def redirect_to_target_or_default(default)
private

def store_target_location
session[:return_to] = request.request_uri
session[:return_to] = request.url
end
end
Expand Up @@ -23,4 +23,34 @@
session['<%= user_singular_name %>_id'].should == assigns['<%= user_singular_name %>'].id
<%- end -%>
end

it "edit action should redirect when not logged in" do
get :edit, :id => "ignored"
response.should redirect_to(login_url)
end

it "edit action should render edit template" do
@controller.stubs(:current_<%= user_singular_name %>).returns(<%= user_class_name %>.first)
get :edit, :id => "ignored"
response.should render_template(:edit)
end

it "update action should redirect when not logged in" do
put :update, :id => "ignored"
response.should redirect_to(login_url)
end

it "update action should render edit template when <%= user_singular_name %> is invalid" do
@controller.stubs(:current_<%= user_singular_name %>).returns(<%= user_class_name %>.first)
<%= user_class_name %>.any_instance.stubs(:valid?).returns(false)
put :update, :id => "ignored"
response.should render_template(:edit)
end

it "update action should redirect when <%= user_singular_name %> is valid" do
@controller.stubs(:current_<%= user_singular_name %>).returns(<%= user_class_name %>.first)
<%= user_class_name %>.any_instance.stubs(:valid?).returns(true)
put :update, :id => "ignored"
response.should redirect_to("/")
end
end
Expand Up @@ -9,13 +9,13 @@ class <%= user_plural_class_name %>ControllerTest < ActionController::TestCase
end
context "create action" do
should "render new template when model is invalid" do
should "render new template when <%= user_singular_name %> is invalid" do
<%= user_class_name %>.any_instance.stubs(:valid?).returns(false)
post :create
assert_template 'new'
end
should "redirect when model is valid" do
should "redirect when <%= user_singular_name %> is valid" do
<%= user_class_name %>.any_instance.stubs(:valid?).returns(true)
post :create
assert_redirected_to "/"
Expand All @@ -24,4 +24,38 @@ class <%= user_plural_class_name %>ControllerTest < ActionController::TestCase
<%- end -%>
end
end

context "edit action" do
should "redirect when not logged in" do
get :edit, :id => "ignored"
assert_redirected_to login_url
end

should "render edit template" do
@controller.stubs(:current_<%= user_singular_name %>).returns(<%= user_class_name %>.first)
get :edit, :id => "ignored"
assert_template 'edit'
end
end

context "update action" do
should "redirect when not logged in" do
put :update, :id => "ignored"
assert_redirected_to login_url
end

should "render edit template when <%= user_singular_name %> is invalid" do
@controller.stubs(:current_<%= user_singular_name %>).returns(<%= user_class_name %>.first)
<%= user_class_name %>.any_instance.stubs(:valid?).returns(false)
put :update, :id => "ignored"
assert_template 'edit'
end

should "redirect when <%= user_singular_name %> is valid" do
@controller.stubs(:current_<%= user_singular_name %>).returns(<%= user_class_name %>.first)
<%= user_class_name %>.any_instance.stubs(:valid?).returns(true)
put :update, :id => "ignored"
assert_redirected_to "/"
end
end
end
Expand Up @@ -20,4 +20,34 @@ def test_create_valid
assert_equal assigns['<%= user_singular_name %>'].id, session['<%= user_singular_name %>_id']
<%- end -%>
end
def test_edit_without_user
get :edit, :id => "ignored"
assert_redirected_to login_url
end

def test_edit
@controller.stubs(:current_<%= user_singular_name %>).returns(<%= user_class_name %>.first)
get :edit, :id => "ignored"
assert_template 'edit'
end

def test_update_without_user
put :update, :id => "ignored"
assert_redirected_to login_url
end

def test_update_invalid
@controller.stubs(:current_<%= user_singular_name %>).returns(<%= user_class_name %>.first)
<%= user_class_name %>.any_instance.stubs(:valid?).returns(false)
put :update, :id => "ignored"
assert_template 'edit'
end

def test_update_valid
@controller.stubs(:current_<%= user_singular_name %>).returns(<%= user_class_name %>.first)
<%= user_class_name %>.any_instance.stubs(:valid?).returns(true)
put :update, :id => "ignored"
assert_redirected_to "/"
end
end
16 changes: 16 additions & 0 deletions lib/generators/nifty/authentication/templates/users_controller.rb
@@ -1,4 +1,6 @@
class <%= user_plural_class_name %>Controller < ApplicationController
before_filter :login_required, :except => [:new, :create]

def new
@<%= user_singular_name %> = <%= user_class_name %>.new
end
Expand All @@ -15,4 +17,18 @@ def create
render :action => 'new'
end
end

def edit
@<%= user_singular_name %> = current_<%= user_singular_name %>
end

def update
@<%= user_singular_name %> = current_<%= user_singular_name %>
if @<%= user_singular_name %>.update_attributes(params[:<%= user_singular_name %>])
flash[:notice] = "Your profile has been updated."
redirect_to "/"
else
render :action => 'edit'
end
end
end
@@ -0,0 +1,20 @@
<%%= form_for @<%= user_singular_name %> do |f| %>
<%%= f.error_messages %>
<p>
<%%= f.label :username %><br />
<%%= f.text_field :username %>
</p>
<p>
<%%= f.label :email, "Email Address" %><br />
<%%= f.text_field :email %>
</p>
<p>
<%%= f.label :password %><br />
<%%= f.password_field :password %>
</p>
<p>
<%%= f.label :password_confirmation, "Confirm Password" %><br />
<%%= f.password_field :password_confirmation %>
</p>
<p><%%= f.submit (@<%= user_singular_name %>.new_record? ? "Sign up" : "Update") %></p>
<%% end %>
@@ -0,0 +1,3 @@
<%% title "Update Profile" %>

<%%= render 'form' %>
Expand Up @@ -2,23 +2,4 @@

<p>Already have an account? <%%= link_to "Log in", login_path %>.</p>

<%%= form_for @<%= user_singular_name %> do |f| %>
<%%= f.error_messages %>
<p>
<%%= f.label :username %><br />
<%%= f.text_field :username %>
</p>
<p>
<%%= f.label :email, "Email Address" %><br />
<%%= f.text_field :email %>
</p>
<p>
<%%= f.label :password %><br />
<%%= f.password_field :password %>
</p>
<p>
<%%= f.label :password_confirmation, "Confirm Password" %><br />
<%%= f.password_field :password_confirmation %>
</p>
<p><%%= f.submit "Sign up" %></p>
<%% end %>
<%%= render 'form' %>
@@ -0,0 +1,20 @@
- form_for @<%= user_singular_name %> do |f|
= f.error_messages
%p
= f.label :username
%br
= f.text_field :username
%p
= f.label :email, "Email Address"
%br
= f.text_field :email
%p
= f.label :password
%br
= f.password_field :password
%p
= f.label :password_confirmation, "Confirm Password"
%br
= f.password_field :password_confirmation
%p
= f.submit (@<%= user_singular_name %>.new_record? ? "Sign up" : "Update")
@@ -0,0 +1,3 @@
- title "Sign up"

= render 'form'
Expand Up @@ -2,23 +2,4 @@

%p== Already have an account? #{link_to "Log in", login_path}.

- form_for @<%= user_singular_name %> do |f|
= f.error_messages
%p
= f.label :username
%br
= f.text_field :username
%p
= f.label :email, "Email Address"
%br
= f.text_field :email
%p
= f.label :password
%br
= f.password_field :password
%p
= f.label :password_confirmation, "Confirm Password"
%br
= f.password_field :password_confirmation
%p
= f.submit "Sign up"
= render 'form'

0 comments on commit 94404ef

Please sign in to comment.