Permalink
Browse files

First draft of user CRUD.

  • Loading branch information...
1 parent c1b81d5 commit 7ba9be7944e202fab0af7864b1d30e219a78ab9d Nicklas Ramhöj committed Sep 2, 2011
View
3 README.rdoc
@@ -84,8 +84,7 @@ Check them out to see some examples on how to use Bento in your application.
* All the gems that this gem depends on.
== TODO:
-* Make it possible to create new users
-* Make it possible to completely remove users
+* Make user CRUD production ready
* Add inline documentation
* Make is possible to have another user model then User.
View
33 app/controllers/bento/users_controller.rb
@@ -0,0 +1,33 @@
+class Bento::UsersController < Bento::BaseController
+ before_filter :fetch_account
+ before_filter :ignore_empty_password, :only => %w[update create]
+
+ create! do |success, failure|
+ success.html { redirect_to account_user_url(@account, @user) }
+ failure.html { render new_account_user_url(@account) }
+ end
+
+ update! do |success, failure|
+ success.html { redirect_to account_user_url(@account, @user) }
+ failure.html { render edit_account_user_url(@account) }
+ end
+
+ destroy! { account_url(@account) }
+
+ private
+
+ def build_resource
+ @user = User.new(params[:user])
+ @user.accounts << @account
+ @user
+ end
+
+ def fetch_account
+ @account ||= Account.find(params[:account_id])
+ end
+
+ def ignore_empty_password
+ params[:user].delete(:password) if params[:user][:password].blank?
+ params[:user].delete(:password_confirmation) if params[:user][:password_confirmation].blank?
+ end
+end
View
19 app/views/bento/accounts/show.html.erb
@@ -4,15 +4,16 @@
<%= show.attributes :name %>
<% end %>
-<ul>
- <% @account.users.each do |user| %>
- <li><%= user.email %></li>
+<%= table_for User, @account.users do |table| %>
+ <%= table.head :email, :edit, :delete %>
+ <%= table.body do |row| %>
+ <%= row.cell link_to(row.record.email, [@account, row.record]) %>
+ <%= row.cell link_to("Edit '#{row.record.email}'", edit_account_user_path(@account, row.record)) %>
+ <%= row.cell button_to("Delete '#{row.record.email}'", account_user_path(@account, row.record), :method => :delete) %>
<% end %>
-</ul>
-
-<p>
- <%= link_to("Edit", edit_account_path(@account)) %>
- <%= button_to('Delete', @account, :method => :delete) %>
-</p>
+ <%= table.foot do %>
+ <%= link_to "Add user", new_account_user_path(@account) %>
+ <% end %>
+<% end %>
<p><%= link_to "Back to accounts", accounts_path %></p>
View
8 app/views/bento/users/_form.html.erb
@@ -0,0 +1,8 @@
+<%= simple_form_for [@account, @user] do |form| -%>
+ <%= form.input :email %>
+ <%= form.input :password %>
+ <%= form.input :password_confirmation %>
+ <%= form.button :submit %>
+<% end -%>
+
+<p><%= link_to "Back to account", account_path(@account) %></p>
View
4 app/views/bento/users/edit.html.erb
@@ -0,0 +1,4 @@
+<h1>Edit user</h1>
+<%= render 'form' %>
+
+<p><%= link_to "Back to account", account_path(@account) %></p>
View
2 app/views/bento/users/new.html.erb
@@ -0,0 +1,2 @@
+<h1>New user</h1>
+<%= render 'form' %>
View
7 app/views/bento/users/show.html.erb
@@ -0,0 +1,7 @@
+<h1><%= @user.email %></h1>
+
+<%= show_for @user do |show| %>
+ <%= show.attributes :email %>
+<% end %>
+
+<p><%= link_to "Back to account", account_path(@user.account) %></p>
View
53 features/manage_users.feature
@@ -0,0 +1,53 @@
+Feature: Manage users
+ In order have users on accounts
+ As an admin
+ I want to create users
+
+ Background:
+ Given I am logged in as admin
+ And I am on the home page
+ And there is an account "Elabs"
+ And there is a user "olduser@elabs.se" belonging to the account "Elabs"
+ And I follow "Manage accounts"
+ And I follow "Elabs"
+
+ Scenario: Successfully creating a user
+ When I follow "Add user"
+ And I fill in "Email" with "newuser@elabs.se"
+ And I fill in "Password" with "test1234"
+ And I fill in "Password confirmation" with "test1234"
+ And I press "Create User"
+ Then I should see "User was successfully created"
+ And I should see "newuser@elabs.se"
+
+ Scenario: Unsuccessfully creating a user
+ When I follow "Add user"
+ And I fill in "Email" with ""
+ And I press "Create"
+ And the "Email" field should have the error message "can't be blank"
+ And the "Password" field should have the error message "can't be blank"
+ And I should see the header "New user"
+
+ Scenario: Successfully edit a user
+ When I follow "Edit 'olduser@elabs.se'"
+ And I fill in "Email" with "anders@elabs.se"
+ And I press "Update"
+ Then I should see "User was successfully updated"
+ And I should not see "newuser@elabs.se"
+ And I should see "anders@elabs.se"
+
+ Scenario: Unsuccessfully edit a user
+ When I follow "Edit 'olduser@elabs.se'"
+ And I fill in "Email" with ""
+ And I press "Update"
+ And the "Email" field should have the error message "can't be blank"
+ And I should see the header "Edit user"
+ When I follow "Back"
+ Then I should see "olduser@elabs.se"
+
+ Scenario: Deleting a user
+ When I press "Delete 'olduser@elabs.se'"
+ Then I should see "User was successfully destroyed"
+ And I should see the header "Elabs"
+ And I should see the header "Users"
+ And I should not see "olduser@elabs.see"
View
2 features/step_definitions/web_steps.rb
@@ -27,7 +27,7 @@
end
Then /^I should see the header "([^\"]*)"$/ do |text|
- page.should have_css("h1, h2", :text => text)
+ page.should have_css("h1, h2, caption", :text => text)
end
Then /^I should not see the header "([^\"]*)"$/ do |text|
View
1 lib/bento/rails/routes.rb
@@ -11,6 +11,7 @@ def bento_for(*resource_names, &block)
resources(resource_name, resource_options) do
collection { get :sign_up }
+ resources :users, :controller => "bento/users"
yield if block_given?
end
end

0 comments on commit 7ba9be7

Please sign in to comment.