Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

First draft of user CRUD.

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

0 comments on commit 7ba9be7

Please sign in to comment.
Something went wrong with that request. Please try again.