Permalink
Browse files

Add devise and customise subdomain login

  • Loading branch information...
1 parent cc9a20e commit ae83f4fb0d990d13fb8e5bcbafbab61d7c2ca2b4 @bangline committed Dec 5, 2011
View
1 .rspec
@@ -0,0 +1 @@
+--colour
View
@@ -27,3 +27,8 @@ gem 'jquery-rails'
# To use debugger
# gem 'ruby-debug19', :require => 'ruby-debug'
+group :development, :test do
+ gem 'rspec-rails'
+ gem 'capybara'
+ gem 'factory_girl_rails'
+end
View
@@ -33,6 +33,15 @@ GEM
arel (2.2.1)
bcrypt-ruby (3.0.1)
builder (3.0.0)
+ capybara (1.1.2)
+ mime-types (>= 1.16)
+ nokogiri (>= 1.3.3)
+ rack (>= 1.0.0)
+ rack-test (>= 0.5.4)
+ selenium-webdriver (~> 2.0)
+ xpath (~> 0.1.4)
+ childprocess (0.2.3)
+ ffi (~> 1.0.6)
coffee-rails (3.1.1)
coffee-script (>= 2.2.0)
railties (~> 3.1.0)
@@ -44,9 +53,16 @@ GEM
bcrypt-ruby (~> 3.0)
orm_adapter (~> 0.0.3)
warden (~> 1.0.3)
+ diff-lcs (1.1.3)
erubis (2.7.0)
execjs (1.2.9)
multi_json (~> 1.0)
+ factory_girl (2.3.2)
+ activesupport
+ factory_girl_rails (1.4.0)
+ factory_girl (~> 2.3.0)
+ railties (>= 3.0.0)
+ ffi (1.0.11)
hike (1.2.1)
i18n (0.6.0)
jquery-rails (1.0.19)
@@ -59,6 +75,7 @@ GEM
treetop (~> 1.4.8)
mime-types (1.17.2)
multi_json (1.0.4)
+ nokogiri (1.5.0)
orm_adapter (0.0.5)
polyglot (0.3.3)
rack (1.3.5)
@@ -88,12 +105,31 @@ GEM
rake (0.9.2.2)
rdoc (3.11)
json (~> 1.4)
+ rspec (2.7.0)
+ rspec-core (~> 2.7.0)
+ rspec-expectations (~> 2.7.0)
+ rspec-mocks (~> 2.7.0)
+ rspec-core (2.7.1)
+ rspec-expectations (2.7.0)
+ diff-lcs (~> 1.1.2)
+ rspec-mocks (2.7.0)
+ rspec-rails (2.7.0)
+ actionpack (~> 3.0)
+ activesupport (~> 3.0)
+ railties (~> 3.0)
+ rspec (~> 2.7.0)
+ rubyzip (0.9.5)
sass (3.1.11)
sass-rails (3.1.5)
actionpack (~> 3.1.0)
railties (~> 3.1.0)
sass (~> 3.1.10)
tilt (~> 1.3.2)
+ selenium-webdriver (2.14.0)
+ childprocess (>= 0.2.1)
+ ffi (~> 1.0.9)
+ multi_json (~> 1.0.4)
+ rubyzip
sprockets (2.0.3)
hike (~> 1.2)
rack (~> 1.0)
@@ -110,15 +146,20 @@ GEM
multi_json (>= 1.0.2)
warden (1.0.5)
rack (>= 1.0)
+ xpath (0.1.4)
+ nokogiri (~> 1.3)
PLATFORMS
ruby
DEPENDENCIES
+ capybara
coffee-rails (~> 3.1.0)
devise
+ factory_girl_rails
jquery-rails
rails (= 3.1.0)
+ rspec-rails
sass-rails (~> 3.1.0)
sqlite3
uglifier
@@ -1,3 +1,4 @@
class ApplicationController < ActionController::Base
protect_from_forgery
+ before_filter :authenticate_user!
end
View
@@ -0,0 +1,17 @@
+class User < ActiveRecord::Base
+ belongs_to :account
+ has_many :notes
+ # 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
+
+ def self.find_for_authentication(conditions={})
+ conditions[:account_id] = Account.find_by_subdomain(conditions.delete(:subdomain)).id
+ super
+ end
+
+end
@@ -7,8 +7,14 @@
<%= csrf_meta_tags %>
</head>
<body>
+ <% if flash[:notice] %>
+ <<p class="notice"><%= flash[:notice] %></p>
+ <% end %>
+ <% if flash[:error] %>
+ <p class="error"><%= flash[:error] %></p>
+ <% end %>
-<%= yield %>
+ <%= yield %>
</body>
</html>
@@ -30,7 +30,7 @@
# find_for_authentication method and considered in your model lookup. For instance,
# if you set :request_keys to [:subdomain], :subdomain will be used on authentication.
# The same considerations mentioned for authentication_keys also apply to request_keys.
- # config.request_keys = []
+ config.request_keys = [ :subdomain ]
# Configure which authentication keys should be case-insensitive.
# These keys will be downcased upon creating or modifying a user and when used
View
@@ -1,4 +1,6 @@
DeviseDemo::Application.routes.draw do
+ devise_for :users
+
resources :notes
# The priority is based upon order of creation:
@@ -50,7 +52,7 @@
# You can have the root of your site routed with "root"
# just remember to delete public/index.html.
- # root :to => 'welcome#index'
+ root :to => 'notes#index'
# See how all your routes lay out with "rake routes"
@@ -1,7 +1,7 @@
class CreateAccounts < ActiveRecord::Migration
def change
create_table :accounts do |t|
- t.string :slug
+ t.string :subdomain
t.timestamps
end
@@ -0,0 +1,28 @@
+class DeviseCreateUsers < ActiveRecord::Migration
+ def self.up
+ create_table(:users) do |t|
+ t.database_authenticatable :null => false
+ t.recoverable
+ t.rememberable
+ t.trackable
+
+ # t.encryptable
+ # t.confirmable
+ # t.lockable :lock_strategy => :failed_attempts, :unlock_strategy => :both
+ # t.token_authenticatable
+
+ t.integer :account_id
+ 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
+
+ def self.down
+ drop_table :users
+ end
+end
View
@@ -11,20 +11,40 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20111204143733) do
+ActiveRecord::Schema.define(:version => 20111204232930) do
create_table "accounts", :force => true do |t|
- t.string "slug"
+ t.string "subdomain"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "notes", :force => true do |t|
t.string "title"
- t.text "note"
+ t.text "content"
+ t.integer "user_id"
t.integer "account_id"
t.datetime "created_at"
t.datetime "updated_at"
end
+ create_table "users", :force => true do |t|
+ t.string "email", :default => "", :null => false
+ t.string "encrypted_password", :limit => 128, :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.integer "account_id"
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ 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
Oops, something went wrong.

0 comments on commit ae83f4f

Please sign in to comment.