Commit 29bac5bd

mo khan <mo@mokhan.ca>
2014-08-07 00:35:22
create sessions table.
1 parent 3d17c2d
app/models/session.rb
@@ -1,8 +1,5 @@
-class Session
-  attr_accessor :id
-
-  def persisted?
-  end
+class Session < ActiveRecord::Base
+  belongs_to :user
 
   class << self
     def login(username, password)
@@ -11,7 +8,7 @@ class Session
       bcrypt = ::BCrypt::Password.new(user.encrypted_password)
       password = ::BCrypt::Engine.hash_secret("#{password}#{User.pepper}", bcrypt.salt)
       if secure_compare(password, user.encrypted_password)
-        #Session.create!(user: user)
+        create!(user: user)
       else
         false
       end
db/migrate/20140807003036_create_sessions.rb
@@ -0,0 +1,8 @@
+class CreateSessions < ActiveRecord::Migration
+  def change
+    create_table :sessions, id: :uuid do |t|
+      t.integer :user_id
+      t.timestamps
+    end
+  end
+end
db/schema.rb
@@ -11,10 +11,7 @@
 #
 # It's strongly recommended that you check this file into your version control system.
 
-ActiveRecord::Schema.define(version: 20140705232142) do
-
-  # These are extensions that must be enabled in order to support this database
-  enable_extension "plpgsql"
+ActiveRecord::Schema.define(version: 20140807003036) do
 
   create_table "activities", force: true do |t|
     t.integer  "subject_id",   null: false
@@ -24,20 +21,20 @@ ActiveRecord::Schema.define(version: 20140705232142) do
     t.datetime "updated_at",   null: false
   end
 
-  add_index "activities", ["subject_id"], name: "index_activities_on_subject_id", using: :btree
-  add_index "activities", ["subject_type"], name: "index_activities_on_subject_type", using: :btree
-  add_index "activities", ["user_id"], name: "index_activities_on_user_id", using: :btree
+  add_index "activities", ["subject_id"], name: "index_activities_on_subject_id"
+  add_index "activities", ["subject_type"], name: "index_activities_on_subject_type"
+  add_index "activities", ["user_id"], name: "index_activities_on_user_id"
 
   create_table "avatars", force: true do |t|
     t.integer  "user_id"
-    t.datetime "created_at",        null: false
-    t.datetime "updated_at",        null: false
+    t.datetime "created_at"
+    t.datetime "updated_at"
     t.string   "avatar"
     t.boolean  "avatar_processing"
     t.string   "avatar_tmp"
   end
 
-  add_index "avatars", ["user_id"], name: "index_avatars_on_user_id", using: :btree
+  add_index "avatars", ["user_id"], name: "index_avatars_on_user_id"
 
   create_table "categories", force: true do |t|
     t.string   "name"
@@ -55,8 +52,8 @@ ActiveRecord::Schema.define(version: 20140705232142) do
     t.datetime "updated_at"
   end
 
-  add_index "comments", ["creation_id"], name: "index_comments_on_creation_id", using: :btree
-  add_index "comments", ["user_id"], name: "index_comments_on_user_id", using: :btree
+  add_index "comments", ["creation_id"], name: "index_comments_on_creation_id"
+  add_index "comments", ["user_id"], name: "index_comments_on_user_id"
 
   create_table "creations", force: true do |t|
     t.string   "name"
@@ -71,8 +68,8 @@ ActiveRecord::Schema.define(version: 20140705232142) do
     t.integer  "category_id"
   end
 
-  add_index "creations", ["created_at"], name: "index_creations_on_created_at", using: :btree
-  add_index "creations", ["user_id"], name: "index_creations_on_user_id", using: :btree
+  add_index "creations", ["created_at"], name: "index_creations_on_created_at"
+  add_index "creations", ["user_id"], name: "index_creations_on_user_id"
 
   create_table "delayed_jobs", force: true do |t|
     t.integer  "priority",   default: 0
@@ -84,11 +81,11 @@ ActiveRecord::Schema.define(version: 20140705232142) do
     t.datetime "failed_at"
     t.string   "locked_by"
     t.string   "queue"
-    t.datetime "created_at",             null: false
-    t.datetime "updated_at",             null: false
+    t.datetime "created_at"
+    t.datetime "updated_at"
   end
 
-  add_index "delayed_jobs", ["priority", "run_at"], name: "delayed_jobs_priority", using: :btree
+  add_index "delayed_jobs", ["priority", "run_at"], name: "delayed_jobs_priority"
 
   create_table "favorites", force: true do |t|
     t.integer  "user_id"
@@ -97,13 +94,13 @@ ActiveRecord::Schema.define(version: 20140705232142) do
     t.datetime "updated_at"
   end
 
-  add_index "favorites", ["creation_id"], name: "index_favorites_on_creation_id", using: :btree
-  add_index "favorites", ["user_id"], name: "index_favorites_on_user_id", using: :btree
+  add_index "favorites", ["creation_id"], name: "index_favorites_on_creation_id"
+  add_index "favorites", ["user_id"], name: "index_favorites_on_user_id"
 
   create_table "interests", force: true do |t|
     t.string   "name"
-    t.datetime "created_at", null: false
-    t.datetime "updated_at", null: false
+    t.datetime "created_at"
+    t.datetime "updated_at"
   end
 
   create_table "photos", force: true do |t|
@@ -121,7 +118,10 @@ ActiveRecord::Schema.define(version: 20140705232142) do
     t.string   "watermark"
   end
 
-  add_index "photos", ["creation_id"], name: "index_photos_on_creation_id", using: :btree
+  add_index "photos", ["creation_id"], name: "index_photos_on_creation_id"
+
+# Could not dump table "sessions" because of following NoMethodError
+#   undefined method `[]' for nil:NilClass
 
   create_table "taggings", force: true do |t|
     t.integer  "tag_id"
@@ -133,11 +133,11 @@ ActiveRecord::Schema.define(version: 20140705232142) do
     t.datetime "created_at"
   end
 
-  add_index "taggings", ["tag_id"], name: "index_taggings_on_tag_id", using: :btree
-  add_index "taggings", ["taggable_id", "taggable_type", "context"], name: "index_taggings_on_taggable_id_and_taggable_type_and_context", using: :btree
-  add_index "taggings", ["taggable_type"], name: "index_taggings_on_taggable_type", using: :btree
-  add_index "taggings", ["tagger_id"], name: "index_taggings_on_tagger_id", using: :btree
-  add_index "taggings", ["tagger_type"], name: "index_taggings_on_tagger_type", using: :btree
+  add_index "taggings", ["tag_id"], name: "index_taggings_on_tag_id"
+  add_index "taggings", ["taggable_id", "taggable_type", "context"], name: "index_taggings_on_taggable_id_and_taggable_type_and_context"
+  add_index "taggings", ["taggable_type"], name: "index_taggings_on_taggable_type"
+  add_index "taggings", ["tagger_id"], name: "index_taggings_on_tagger_id"
+  add_index "taggings", ["tagger_type"], name: "index_taggings_on_tagger_type"
 
   create_table "tags", force: true do |t|
     t.string "name"
@@ -148,22 +148,22 @@ ActiveRecord::Schema.define(version: 20140705232142) do
     t.text     "description"
     t.string   "url"
     t.integer  "user_id"
-    t.datetime "created_at",  null: false
-    t.datetime "updated_at",  null: false
+    t.datetime "created_at"
+    t.datetime "updated_at"
     t.string   "image_url"
     t.string   "author"
     t.string   "author_url"
   end
 
-  add_index "tutorials", ["user_id"], name: "index_tutorials_on_user_id", using: :btree
+  add_index "tutorials", ["user_id"], name: "index_tutorials_on_user_id"
 
   create_table "users", force: true do |t|
-    t.string   "email",                              default: "", null: false
-    t.string   "encrypted_password",     limit: 128, default: "", null: false
+    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.integer  "sign_in_count",          default: 0
     t.datetime "current_sign_in_at"
     t.datetime "last_sign_in_at"
     t.string   "current_sign_in_ip"
@@ -182,28 +182,28 @@ ActiveRecord::Schema.define(version: 20140705232142) do
     t.datetime "confirmed_at"
     t.datetime "confirmation_sent_at"
     t.string   "unconfirmed_email"
-    t.integer  "failed_attempts",                    default: 0
+    t.integer  "failed_attempts",        default: 0
     t.string   "unlock_token"
     t.datetime "locked_at"
     t.string   "authentication_token"
     t.string   "invitation_token"
     t.string   "full_address"
-    t.integer  "creations_count",                    default: 0
+    t.integer  "creations_count",        default: 0
     t.boolean  "is_admin"
   end
 
-  add_index "users", ["authentication_token"], name: "index_users_on_authentication_token", unique: true, using: :btree
-  add_index "users", ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true, using: :btree
-  add_index "users", ["created_at"], name: "index_users_on_created_at", using: :btree
-  add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree
-  add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree
-  add_index "users", ["unlock_token"], name: "index_users_on_unlock_token", unique: true, using: :btree
+  add_index "users", ["authentication_token"], name: "index_users_on_authentication_token", unique: true
+  add_index "users", ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true
+  add_index "users", ["created_at"], name: "index_users_on_created_at"
+  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
+  add_index "users", ["unlock_token"], name: "index_users_on_unlock_token", unique: true
 
   create_table "users_interests", id: false, force: true do |t|
     t.integer "user_id"
     t.integer "interest_id"
   end
 
-  add_index "users_interests", ["interest_id", "user_id"], name: "index_users_interests_on_interest_id_and_user_id", using: :btree
+  add_index "users_interests", ["interest_id", "user_id"], name: "index_users_interests_on_interest_id_and_user_id"
 
 end
spec/models/session_spec.rb
@@ -11,11 +11,25 @@ describe Session do
     context "when the email is known" do
       let(:user) { create(:user) }
 
+      before :each do
+        user.password = 'password'
+        user.save!
+      end
+
       context "when the password is incorrect" do
         it "returns false" do
           expect(Session.login(user.email, 'blah')).to be_falsey
         end
       end
+
+      context "when the password is correct" do
+        it "returns a new session" do
+          result = Session.login(user.email, 'password')
+          expect(result).to be_truthy
+          expect(result).to_not be_new_record
+          expect(result.user).to eql(user)
+        end
+      end
     end
   end
 end