Skip to content
Browse files

basic users with devise

  • Loading branch information...
1 parent 48a26ad commit 7d6f2353554f0d2812006a5fa653d9008927dd7a @mccolin committed Mar 25, 2012
View
3 app/controllers/home_controller.rb
@@ -3,8 +3,9 @@
class HomeController < ApplicationController
+ before_filter :authenticate_user!
+
def index
- render :text=>"Hello WOSoMP"
end
end
View
20 app/helpers/application_helper.rb
@@ -1,2 +1,22 @@
module ApplicationHelper
+
+ # Render the login status
+ def login_status(opts={})
+ if user_signed_in?
+ "Welcome, #{current_user.first_name}!"
+ else
+ "Not Logged In"
+ end
+ end
+
+ # Render the appropriate link for session management:
+ def login_logout_link(opts={})
+ if user_signed_in?
+ link_to("Logout", destroy_user_session_path, opts.merge(:method => :delete))
+ else
+ link_to("Login", new_user_session_path, opts)
+ end
+ end
+
+
end
View
12 app/models/user.rb
@@ -0,0 +1,12 @@
+# WOSOMP
+#
+
+class User < ActiveRecord::Base
+ # Include default devise modules. Others available are:
+ # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
+ devise :database_authenticatable, :registerable,
+ :recoverable, :rememberable, :trackable, :validatable
+
+ # Setup accessible (or protected) attributes for your model
+ attr_accessible :email, :password, :password_confirmation, :remember_me
+end
View
2 app/views/home/index.html.erb
@@ -0,0 +1,2 @@
+
+<p>Welcome to WOSoMP</p>
View
9 app/views/layouts/application.html.erb
@@ -7,6 +7,15 @@
<%= csrf_meta_tags %>
</head>
<body>
+
+ <header id="header">
+ <h1>WOSoMP</h1>
+ <ul>
+ <li><%=login_status%></li>
+ <li><%=login_logout_link(:style=>"color:red;")%></li>
+ </ul>
+ </header>
+
<%= yield %>
View
8 config/routes.rb
@@ -3,6 +3,14 @@
Wosomp::Application.routes.draw do
+ # Authentication for Users:
+ devise_for :users
+ devise_scope :users do
+ get "login", :to=>"devise/sessions#new"
+ delete "logout", :to=>"devise/sessions#destroy"
+ end
+
+ # Our Application Root:
root :to=>"home#index", :as=>"home"
resources :users
View
54 db/migrate/20120325205046_devise_create_users.rb
@@ -0,0 +1,54 @@
+class DeviseCreateUsers < ActiveRecord::Migration
+ def change
+ create_table(:users) do |t|
+ ## Database authenticatable
+ t.string :email, :null => false, :default => ""
+ t.string :encrypted_password, :null => false, :default => ""
+
+ ## Recoverable
+ t.string :reset_password_token
+ t.datetime :reset_password_sent_at
+
+ ## Rememberable
+ t.datetime :remember_created_at
+
+ ## Trackable
+ t.integer :sign_in_count, :default => 0
+ t.datetime :current_sign_in_at
+ t.datetime :last_sign_in_at
+ t.string :current_sign_in_ip
+ t.string :last_sign_in_ip
+
+ ## Encryptable
+ # t.string :password_salt
+
+ ## Confirmable
+ # t.string :confirmation_token
+ # t.datetime :confirmed_at
+ # t.datetime :confirmation_sent_at
+ # t.string :unconfirmed_email # Only if using reconfirmable
+
+ ## Lockable
+ # t.integer :failed_attempts, :default => 0 # Only if lock strategy is :failed_attempts
+ # t.string :unlock_token # Only if unlock strategy is :email or :both
+ # t.datetime :locked_at
+
+ ## Token authenticatable
+ # t.string :authentication_token
+
+ # User fields:
+ t.string :first_name
+ t.string :last_name
+ t.datetime :birthday
+
+ # Standard Time-keeping:
+ t.timestamps
+ end
+
+ add_index :users, :email, :unique => true
+ add_index :users, :reset_password_token, :unique => true
+ # add_index :users, :confirmation_token, :unique => true
+ # add_index :users, :unlock_token, :unique => true
+ # add_index :users, :authentication_token, :unique => true
+ end
+end
View
23 db/schema.rb
@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20120325202350) do
+ActiveRecord::Schema.define(:version => 20120325205046) do
create_table "olympiads", :force => true do |t|
t.string "name", :null => false
@@ -50,4 +50,25 @@
add_index "slugs", ["scope", "slug", "created_at"], :name => "index_slugs_on_scope_and_slug_and_created_at"
add_index "slugs", ["scope", "slug"], :name => "index_slugs_on_scope_and_slug"
+ create_table "users", :force => true do |t|
+ t.string "email", :default => "", :null => false
+ t.string "encrypted_password", :default => "", :null => false
+ t.string "reset_password_token"
+ t.datetime "reset_password_sent_at"
+ t.datetime "remember_created_at"
+ t.integer "sign_in_count", :default => 0
+ t.datetime "current_sign_in_at"
+ t.datetime "last_sign_in_at"
+ t.string "current_sign_in_ip"
+ t.string "last_sign_in_ip"
+ t.string "first_name"
+ t.string "last_name"
+ t.datetime "birthday"
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ end
+
+ add_index "users", ["email"], :name => "index_users_on_email", :unique => true
+ add_index "users", ["reset_password_token"], :name => "index_users_on_reset_password_token", :unique => true
+
end
View
11 test/fixtures/users.yml
@@ -0,0 +1,11 @@
+# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html
+
+# This model initially had no columns defined. If you add columns to the
+# model remove the '{}' from the fixture names and add the columns immediately
+# below each fixture, per the syntax in the comments below
+#
+one: {}
+# column: value
+#
+two: {}
+# column: value
View
7 test/unit/user_test.rb
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class UserTest < ActiveSupport::TestCase
+ # test "the truth" do
+ # assert true
+ # end
+end

0 comments on commit 7d6f235

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