Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Finished first cut of user model

  • Loading branch information...
commit 6b30651f6eeeeccfefa0e7d54a2c178dc1cfff60 1 parent 7d3a4a0
Ryan Maclear authored
1  Gemfile
@@ -9,6 +9,7 @@ gem 'sqlite3-ruby', '1.3.1', :require => 'sqlite3'
9 9
10 10 group :development do
11 11 gem 'rspec-rails', '2.0.1'
  12 + gem 'annotate-models', '1.0.4'
12 13 end
13 14
14 15 group :test do
2  Gemfile.lock
@@ -28,6 +28,7 @@ GEM
28 28 activemodel (= 3.0.1)
29 29 activesupport (= 3.0.1)
30 30 activesupport (3.0.1)
  31 + annotate-models (1.0.4)
31 32 arel (1.0.1)
32 33 activesupport (~> 3.0.0)
33 34 builder (2.1.2)
@@ -88,6 +89,7 @@ PLATFORMS
88 89 ruby
89 90
90 91 DEPENDENCIES
  92 + annotate-models (= 1.0.4)
91 93 rails (= 3.0.1)
92 94 rspec (= 2.0.1)
93 95 rspec-rails (= 2.0.1)
5 app/controllers/users_controller.rb
... ... @@ -1,4 +1,9 @@
1 1 class UsersController < ApplicationController
  2 +
  3 + def show
  4 + @user = User.find(params[:id])
  5 + end
  6 +
2 7 def new
3 8 @title = "Sign Up"
4 9 end
25 app/models/user.rb
... ... @@ -0,0 +1,25 @@
  1 +# == Schema Information
  2 +# Schema version: 20110228103104
  3 +#
  4 +# Table name: users
  5 +#
  6 +# id :integer not null, primary key
  7 +# name :string(255)
  8 +# email :string(255)
  9 +# created_at :datetime
  10 +# updated_at :datetime
  11 +#
  12 +
  13 +class User < ActiveRecord::Base
  14 + attr_accessible :name, :email
  15 +
  16 + email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  17 +
  18 + validates :name, :presence => true,
  19 + :length => { :minimum => 3, :maximum => 50 }
  20 +
  21 + validates :email, :presence => true,
  22 + :format => { :with => email_regex },
  23 + :uniqueness => { :case_sensitive => false }
  24 +
  25 +end
1  app/views/layouts/application.html.erb
@@ -12,6 +12,7 @@
12 12 <%= yield %>
13 13 </section>
14 14 <%= render 'layouts/footer' %>
  15 + <%= debug(params) if Rails.env.development? %>
15 16 </div>
16 17 </body>
17 18 </html>
1  app/views/users/show.html.erb
... ... @@ -0,0 +1 @@
  1 +<%= @user.name %>, <%= @user.email %>
2  config/routes.rb
... ... @@ -1,5 +1,5 @@
1 1 SampleApp::Application.routes.draw do
2   - get "users/new"
  2 + resources :users
3 3
4 4 match '/contact', :to => 'pages#contact'
5 5 match '/about', :to => 'pages#about'
14 db/migrate/20110228103104_create_users.rb
... ... @@ -0,0 +1,14 @@
  1 +class CreateUsers < ActiveRecord::Migration
  2 + def self.up
  3 + create_table :users do |t|
  4 + t.string :name
  5 + t.string :email
  6 +
  7 + t.timestamps
  8 + end
  9 + end
  10 +
  11 + def self.down
  12 + drop_table :users
  13 + end
  14 +end
9 db/migrate/20110228131804_add_email_uniqueness_index.rb
... ... @@ -0,0 +1,9 @@
  1 +class AddEmailUniquenessIndex < ActiveRecord::Migration
  2 + def self.up
  3 + add_index :users, :email, :unique => true
  4 + end
  5 +
  6 + def self.down
  7 + remove_index :users, :email
  8 + end
  9 +end
24 db/schema.rb
... ... @@ -0,0 +1,24 @@
  1 +# This file is auto-generated from the current state of the database. Instead
  2 +# of editing this file, please use the migrations feature of Active Record to
  3 +# incrementally modify your database, and then regenerate this schema definition.
  4 +#
  5 +# Note that this schema.rb definition is the authoritative source for your
  6 +# database schema. If you need to create the application database on another
  7 +# system, you should be using db:schema:load, not running all the migrations
  8 +# from scratch. The latter is a flawed and unsustainable approach (the more migrations
  9 +# you'll amass, the slower it'll run and the greater likelihood for issues).
  10 +#
  11 +# It's strongly recommended to check this file into your version control system.
  12 +
  13 +ActiveRecord::Schema.define(:version => 20110228131804) do
  14 +
  15 + create_table "users", :force => true do |t|
  16 + t.string "name"
  17 + t.string "email"
  18 + t.datetime "created_at"
  19 + t.datetime "updated_at"
  20 + end
  21 +
  22 + add_index "users", ["email"], :name => "index_users_on_email", :unique => true
  23 +
  24 +end
62 spec/models/user_spec.rb
... ... @@ -0,0 +1,62 @@
  1 +require 'spec_helper'
  2 +
  3 +describe User do
  4 + before(:each) do
  5 + @attr = { :name => "Example User", :email => "user@example.com" }
  6 + end
  7 +
  8 + it "should create a new instance givien valid attributes" do
  9 + User.create!(@attr)
  10 + end
  11 +
  12 + it "should require a name" do
  13 + no_name_user = User.new(@attr.merge(:name => ""))
  14 + no_name_user.should_not be_valid
  15 + end
  16 + it "should require an email address" do
  17 + no_email_user = User.new(@attr.merge(:email => ""))
  18 + no_email_user.should_not be_valid
  19 + end
  20 +
  21 + it "should reject names that are too long" do
  22 + long_name = "a" * 51
  23 + long_name_user = User.new(@attr.merge(:name => long_name))
  24 + long_name_user.should_not be_valid
  25 + end
  26 +
  27 + it "should reject names that are too short" do
  28 + short_name = "a" * 2
  29 + short_name_user = User.new(@attr.merge(:name => short_name))
  30 + short_name_user.should_not be_valid
  31 + end
  32 +
  33 + it "should accept valid email addresses" do
  34 + addresses = %w[user@foo.com THE_USER@foo.bar.org first.last@foo.jp]
  35 + addresses.each do |address|
  36 + valid_email_user = User.new(@attr.merge(:email => address))
  37 + valid_email_user.should be_valid
  38 + end
  39 + end
  40 +
  41 + it "should reject invalid email addresses" do
  42 + addresses = %w[user@foo,com user_at_foo.org example.user@foo.]
  43 + addresses.each do |address|
  44 + invalid_email_user = User.new(@attr.merge(:email => address))
  45 + invalid_email_user.should_not be_valid
  46 + end
  47 + end
  48 +
  49 + it "should reject duplicate email addresses" do
  50 + User.create!(@attr)
  51 + user_with_duplicate_email_address = User.new(@attr)
  52 + user_with_duplicate_email_address.should_not be_valid
  53 + end
  54 +
  55 + it "should reject duplicate email addresses ignoring case" do
  56 + upcased_email = @attr[:email].upcase
  57 + User.create!(@attr.merge(:email => upcased_email))
  58 + user_with_duplicate_email_address = User.new(@attr)
  59 + user_with_duplicate_email_address.should_not be_valid
  60 + end
  61 +
  62 +end

0 comments on commit 6b30651

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