Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

started user model

  • Loading branch information...
commit 72c9b79b3b8359a43cfd83aff573001793bd7d71 1 parent cfa052d
@flaginboot authored
View
5 app/controllers/users_controller.rb
@@ -1,7 +1,10 @@
class UsersController < ApplicationController
+ def show
+ @user = User.find(params[:id])
+ end
+
def new
@title = 'Sign up'
end
-
end
View
12 app/models/user.rb
@@ -0,0 +1,12 @@
+class User < ActiveRecord::Base
+ attr_accessible :name, :email
+
+ email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
+
+ validates :name, presence: true,
+ length: { :maximum => 50 }
+ validates :email, presence: true,
+ format: { :with => email_regex },
+ uniqueness: { case_sensitive: false }
+end
+
View
2  app/views/layouts/application.html.erb
@@ -18,6 +18,8 @@
</section>
<%= render 'layouts/footer' %>
+
+ <%= debug(params) if Rails.env.development? %>
</div>
</body>
</html>
View
2  app/views/users/show.html.erb
@@ -0,0 +1,2 @@
+<%= @user.name %>, <%= @user.email %>
+
View
2  config/routes.rb
@@ -3,7 +3,7 @@
match '/about', to: 'pages#about'
match '/help', to: 'pages#help'
- get 'users/new'
+ resources :users
match '/signup', to: 'users#new'
root to: 'pages#home'
View
10 db/migrate/20110907041243_create_users.rb
@@ -0,0 +1,10 @@
+class CreateUsers < ActiveRecord::Migration
+ def change
+ create_table :users do |t|
+ t.string :name
+ t.string :email
+
+ t.timestamps
+ end
+ end
+end
View
10 db/migrate/20110907055613_add_email_uniqueness_index.rb
@@ -0,0 +1,10 @@
+class AddEmailUniquenessIndex < ActiveRecord::Migration
+ def up
+ add_index :users, :email, unique: true
+ end
+
+ def down
+ remove_index :users, :email
+ end
+end
+
View
11 db/schema.rb
@@ -11,6 +11,15 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 0) do
+ActiveRecord::Schema.define(:version => 20110907055613) do
+
+ create_table "users", :force => true do |t|
+ t.string "name"
+ t.string "email"
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ end
+
+ add_index "users", ["email"], :name => "index_users_on_email", :unique => true
end
View
10 test/fixtures/users.yml
@@ -0,0 +1,10 @@
+# Read about fixtures at http://api.rubyonrails.org/classes/Fixtures.html
+
+travis:
+ name: Travis Wetmore
+ email: traviswetmore@gmail.com
+
+travis_duped:
+ name: Bob
+# email: traviswetmore@gmail.com
+
View
58 test/unit/user_test.rb
@@ -0,0 +1,58 @@
+require 'test_helper'
+
+class UserTest < ActiveSupport::TestCase
+ def setup
+ @user = users(:travis)
+ end
+
+ test 'should require a name' do
+ @user.name = nil
+ refute @user.valid?
+ refute @user.errors[:name].empty?
+ end
+
+ test 'reject names that are too long' do
+ @user.name = 'a' * 51
+ refute @user.valid?
+ refute @user.errors[:name].empty?
+ end
+
+ test 'should require an email address' do
+ @user.email = nil
+ refute @user.valid?
+ refute @user.errors[:email].empty?
+ end
+
+ test 'accept valid email addresses' do
+ addresses = %w[user@foo.com THE_USER@foo.bar.org first.last@foo.jp]
+ addresses.each do |address|
+ @user.email = address
+ assert @user.valid?
+ assert@user.errors[:email].empty?
+ end
+ end
+
+ test 'reject valid email addresses' do
+ addresses = %w[user@foo,com user_at_foo.org example.user@foo.]
+ addresses.each do |address|
+ @user.email = address
+ refute @user.valid?
+ refute @user.errors[:email].empty?
+ end
+ end
+
+ test 'reject duplicate email addresses' do
+ @duplicate_user = users(:travis_duped)
+ @duplicate_user.email = 'traviswetmore@gmail.com'
+ refute @duplicate_user.valid?, 'valid?'
+ refute @duplicate_user.errors[:email].empty?, 'empty?'
+ end
+
+ test "reject email addresses identical up to case" do
+ @upcase_email_user = users(:travis_duped)
+ @upcase_email_user.email = 'traviswetmore@gmail.com'.upcase
+ refute @upcase_email_user.valid?, 'valid?'
+ refute @upcase_email_user.errors[:email].empty?, 'empty?'
+ end
+end
+
Please sign in to comment.
Something went wrong with that request. Please try again.