Skip to content

Commit 557db68

Browse files
committed
Finish user signup
1 parent ac62804 commit 557db68

File tree

13 files changed

+207
-6
lines changed

13 files changed

+207
-6
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ gem 'bcrypt-ruby', '3.0.1'
77
group :development, :test do
88
gem 'sqlite3', '1.3.5'
99
gem 'rspec-rails', '2.10.0'
10+
gem 'factory_girl_rails', '1.4.0'
1011
end
1112

1213
gem 'annotate', '~> 2.4.1.beta', group: :development

Gemfile.lock

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ GEM
3131
addressable (2.2.8)
3232
annotate (2.4.1.beta1)
3333
arel (3.0.2)
34+
bcrypt-ruby (3.0.1)
3435
bcrypt-ruby (3.0.1-x86-mingw32)
3536
bootstrap-sass (2.0.0)
3637
builder (3.0.0)
@@ -54,6 +55,11 @@ GEM
5455
erubis (2.7.0)
5556
execjs (1.4.0)
5657
multi_json (~> 1.0)
58+
factory_girl (2.3.2)
59+
activesupport
60+
factory_girl_rails (1.4.0)
61+
factory_girl (~> 2.3.0)
62+
railties (>= 3.0.0)
5763
ffi (1.0.11)
5864
hike (1.2.1)
5965
i18n (0.6.0)
@@ -151,6 +157,7 @@ DEPENDENCIES
151157
bootstrap-sass (= 2.0.0)
152158
capybara (= 1.1.2)
153159
coffee-rails (= 3.2.2)
160+
factory_girl_rails (= 1.4.0)
154161
jquery-rails (= 2.0.0)
155162
pg (= 0.12.2)
156163
rails (= 3.2.6)

app/assets/stylesheets/custom.css.scss

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44

55
$grayMediumLight: #eaeaea;
66

7+
@mixin box_sizing {
8+
-moz-box-sizing: border-box;
9+
-webkit-box-sizing: border-box;
10+
box-sizing: border-box;
11+
}
12+
713
/* universal */
814

915
html {
@@ -99,4 +105,67 @@ footer {
99105
margin-left: 10px;
100106
}
101107
}
108+
109+
/* miscellaneous */
110+
111+
.debug_dump {
112+
clear: both;
113+
float: left;
114+
width: 100%;
115+
margin-top: 45px;
116+
@include box_sizing;
117+
}
118+
119+
/* sidebar */
120+
121+
aside {
122+
section {
123+
padding: 10px 0;
124+
border-top: 1px solid $grayLighter;
125+
&:first-child {
126+
border: 0;
127+
padding-top: 0;
128+
}
129+
span {
130+
display: block;
131+
margin-bottom: 3px;
132+
line-height: 1;
133+
}
134+
h1 {
135+
font-size: 1.6em;
136+
text-align: left;
137+
letter-spacing: -1px;
138+
margin-bottom: 3px;
139+
}
140+
}
141+
}
142+
143+
.gravatar {
144+
float: left;
145+
margin-right: 10px;
146+
}
147+
148+
/* forms */
149+
150+
input, textarea, select, .uneditable-input {
151+
border: 1px solid #bbb;
152+
width: 100%;
153+
padding: 10px;
154+
height: auto;
155+
margin-bottom: 15px;
156+
@include box_sizing;
157+
}
158+
159+
#error_explanation {
160+
color: #f00;
161+
ul {
162+
list-style: none;
163+
margin: 0 0 18px 0;
164+
}
165+
}
166+
167+
.field_with_errors {
168+
@extend .control-group;
169+
@extend .error;
170+
}
102171
}

app/controllers/users_controller.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,21 @@
11
class UsersController < ApplicationController
2+
def show
3+
@user = User.find(params[:id])
4+
end
5+
26
def new
7+
@user = User.new
8+
end
9+
10+
def create
11+
@user = User.new(params[:user])
12+
if @user.save
13+
# Handle a successful save.
14+
flash[:success] = "Welcome to the Sample App!"
15+
redirect_to @user
16+
else
17+
render 'new'
18+
end
319
end
20+
421
end

app/helpers/users_helper.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
11
module UsersHelper
2+
3+
# Returns the Gravatar (http://gravatar.com/) for the given user.
4+
def gravatar_for(user)
5+
gravatar_id = Digest::MD5::hexdigest(user.email.downcase)
6+
gravatar_url = "https://secure.gravatar.com/avatar/#{gravatar_id}"
7+
image_tag(gravatar_url, alt: user.name, class: "gravatar")
8+
end
29
end

app/views/layouts/application.html.erb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,15 @@
99
</head>
1010
<body>
1111
<%= render 'layouts/header' %>
12-
<div class="container">
12+
<div class="container">
13+
<% flash.each do |key, value| %>
14+
<div class="alert alert-<%= key %>"><%= value %></div>
15+
<% end %>
1316
<%= yield %>
1417
<%= render 'layouts/footer' %>
1518
</div>
16-
</body>
19+
<div class="container">
20+
<%= debug(params) if Rails.env.development? %>
21+
</div>
22+
</body>
1723
</html>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<% if @user.errors.any? %>
2+
<div id="error_explanation">
3+
<div class="alert alert-error">
4+
The form contains <%= pluralize(@user.errors.count, "error") %>.
5+
</div>
6+
<ul>
7+
<% @user.errors.full_messages.each do |msg| %>
8+
<li>* <%= msg %></li>
9+
<% end %>
10+
</ul>
11+
</div>
12+
<% end %>

app/views/users/new.html.erb

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
11
<% provide(:title, 'Sign up') %>
22
<h1>Sign up</h1>
3-
<p>Find me in app/views/users/new.html.erb</p>
3+
4+
<div class="row">
5+
<div class="span6 offset3">
6+
<%= form_for(@user) do |f| %>
7+
<%= render 'shared/error_messages' %>
8+
9+
<%= f.label :name %>
10+
<%= f.text_field :name %>
11+
12+
<%= f.label :email %>
13+
<%= f.text_field :email %>
14+
15+
<%= f.label :password %>
16+
<%= f.password_field :password %>
17+
18+
<%= f.label :password_confirmation, "Confirmation" %>
19+
<%= f.password_field :password_confirmation %>
20+
21+
<%= f.submit "Create my account", class: "btn btn-large btn-primary" %>
22+
<% end %>
23+
</div>
24+
</div>

app/views/users/show.html.erb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<% provide(:title, @user.name) %>
2+
<div class="row">
3+
<aside class="span4">
4+
<section>
5+
<h1>
6+
<%= gravatar_for @user %>
7+
<%= @user.name %>
8+
</h1>
9+
</section>
10+
</aside>
11+
</div>

config/environments/test.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,11 @@
3434

3535
# Print deprecation notices to the stderr
3636
config.active_support.deprecation = :stderr
37+
38+
# Speed up tests by lowering BCrypt's cost function.
39+
require 'bcrypt'
40+
silence_warnings do
41+
BCrypt::Engine::DEFAULT_COST = BCrypt::Engine::MIN_COST
42+
end
43+
3744
end

config/routes.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
SampleApp::Application.routes.draw do
2-
get "users/new"
3-
2+
resources :users
3+
44
root to: 'static_pages#home'
55

66
match '/signup', to: 'users#new'

spec/factories.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
FactoryGirl.define do
2+
factory :user do
3+
name "Michael Hartl"
4+
5+
password "foobar"
6+
password_confirmation "foobar"
7+
end
8+
end

spec/requests/user_pages_spec.rb

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,41 @@
88
before { visit signup_path }
99

1010
it { should have_selector('h1', text: 'Sign up') }
11-
it { should have_selector('title', text: full_title('Sign up')) }
11+
it { should have_selector('title', text: 'Sign up') }
12+
end
13+
14+
describe "profile page" do
15+
# Code to make a user variable
16+
let(:user) { FactoryGirl.create(:user) }
17+
before { visit user_path(user) }
18+
19+
it { should have_selector('h1', text: user.name) }
20+
it { should have_selector('title', text: user.name) }
21+
end
22+
23+
describe "signup" do
24+
25+
before { visit signup_path }
26+
27+
let(:submit) { "Create my account" }
28+
29+
describe "with invalid information" do
30+
it "should not create a user" do
31+
expect { click_button submit }.not_to change(User, :count)
32+
end
33+
end
34+
35+
describe "with valid information" do
36+
before do
37+
fill_in "Name", with: "Example User"
38+
fill_in "Email", with: "[email protected]"
39+
fill_in "Password", with: "foobar"
40+
fill_in "Confirmation", with: "foobar"
41+
end
42+
43+
it "should create a user" do
44+
expect { click_button submit }.to change(User, :count).by(1)
45+
end
46+
end
1247
end
1348
end

0 commit comments

Comments
 (0)