Skip to content

Commit

Permalink
A basic User model
Browse files Browse the repository at this point in the history
  • Loading branch information
diasks2 committed Apr 26, 2012
1 parent 5142cde commit c0aa292
Show file tree
Hide file tree
Showing 15 changed files with 139 additions and 2 deletions.
52 changes: 51 additions & 1 deletion README.md
Expand Up @@ -112,6 +112,56 @@ Now navigate to http://[yourappname].herokuapp.com/surveys

###Section 3 - Add a user model

17)
17) Create a Users controller

$ rails generate controller Users new

18) Generate a User model

$ rails generate model User name:string email:string

19) Migrate the new model

$ bundle exec rake db:migrate

20) Add some basic validations to the User model

class User < ActiveRecord::Base
attr_accessible :email, :name

before_save { |user| user.email = email.downcase }

validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
end

21) Add an index to the user email

$ rails generate migration add_index_to_users_email

22) Navigate to the db/migrate folder and open the file that was just created db/migrate/[timestamp]_add_index_to_users_email.rb and edit it like below

class AddIndexToUsersEmail < ActiveRecord::Migration
def change
add_index :users, :email, unique: true
end
end

23) Migrate the database

$ bundle exec rake db:migrate

24) Open the rails console and create a new user

$ rails console
>> User.create(name: "Your Name", email: "yourname@example.com")
25) Commit the changes

$ git add .
$ git commit -m "A basic User model"



3 changes: 3 additions & 0 deletions app/assets/javascripts/users.js.coffee
@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/users.css.scss
@@ -0,0 +1,3 @@
// Place all the styles related to the Users controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
4 changes: 4 additions & 0 deletions app/controllers/users_controller.rb
@@ -0,0 +1,4 @@
class UsersController < ApplicationController
def new
end
end
2 changes: 2 additions & 0 deletions app/helpers/users_helper.rb
@@ -0,0 +1,2 @@
module UsersHelper
end
10 changes: 10 additions & 0 deletions app/models/user.rb
@@ -0,0 +1,10 @@
class User < ActiveRecord::Base
attr_accessible :email, :name

before_save { |user| user.email = email.downcase }

validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
end
2 changes: 2 additions & 0 deletions app/views/users/new.html.erb
@@ -0,0 +1,2 @@
<h1>Users#new</h1>
<p>Find me in app/views/users/new.html.erb</p>
2 changes: 2 additions & 0 deletions config/routes.rb
@@ -1,4 +1,6 @@
SurveyorExample::Application.routes.draw do
get "users/new"

# The priority is based upon order of creation:
# first created -> highest priority.

Expand Down
10 changes: 10 additions & 0 deletions db/migrate/20120426232512_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
5 changes: 5 additions & 0 deletions db/migrate/20120426233439_add_index_to_users_email.rb
@@ -0,0 +1,5 @@
class AddIndexToUsersEmail < ActiveRecord::Migration
def change
add_index :users, :email, unique: true
end
end
11 changes: 10 additions & 1 deletion db/schema.rb
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20120425000563) do
ActiveRecord::Schema.define(:version => 20120426233439) do

create_table "answers", :force => true do |t|
t.integer "question_id"
Expand Down Expand Up @@ -166,6 +166,15 @@

add_index "surveys", ["access_code"], :name => "surveys_ac_idx", :unique => true

create_table "users", :force => true do |t|
t.string "name"
t.string "email"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end

add_index "users", ["email"], :name => "index_users_on_email", :unique => true

create_table "validation_conditions", :force => true do |t|
t.integer "validation_id"
t.string "rule_key"
Expand Down
12 changes: 12 additions & 0 deletions spec/controllers/users_controller_spec.rb
@@ -0,0 +1,12 @@
require 'spec_helper'

describe UsersController do

describe "GET 'new'" do
it "returns http success" do
get 'new'
response.should be_success
end
end

end
15 changes: 15 additions & 0 deletions spec/helpers/users_helper_spec.rb
@@ -0,0 +1,15 @@
require 'spec_helper'

# Specs in this file have access to a helper object that includes
# the UsersHelper. For example:
#
# describe UsersHelper do
# describe "string concat" do
# it "concats two strings with spaces" do
# helper.concat_strings("this","that").should == "this that"
# end
# end
# end
describe UsersHelper do
pending "add some examples to (or delete) #{__FILE__}"
end
5 changes: 5 additions & 0 deletions spec/models/user_spec.rb
@@ -0,0 +1,5 @@
require 'spec_helper'

describe User do
pending "add some examples to (or delete) #{__FILE__}"
end
5 changes: 5 additions & 0 deletions spec/views/users/new.html.erb_spec.rb
@@ -0,0 +1,5 @@
require 'spec_helper'

describe "users/new.html.erb" do
pending "add some examples to (or delete) #{__FILE__}"
end

0 comments on commit c0aa292

Please sign in to comment.