Add user model, and .create_with_omniauth method

openid
Micha Wrobel 13 years ago
parent 3e267d87bd
commit f59841abaf

@ -0,0 +1,15 @@
class User < ActiveRecord::Base
validate :provider, :presence => true
validate :uid, :presence => true
def self.create_with_omniauth(auth)
create! do |user|
user.provider = auth["provider"]
user.uid = auth["uid"]
user.name = auth["user_info"]["name"]
user.email = auth["user_info"]["email"]
end
end
end

@ -0,0 +1,14 @@
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :provider
t.string :uid
t.string :email
t.string :name
t.timestamps
end
add_index :users, [ :provider, :uid ], :unique => true
end
end

@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20120114195316) do
ActiveRecord::Schema.define(:version => 20120225124004) do
create_table "asciicasts", :force => true do |t|
t.integer "user_id"
@ -24,8 +24,8 @@ ActiveRecord::Schema.define(:version => 20120114195316) do
t.string "command"
t.string "shell"
t.string "uname"
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "stdin"
t.string "stdin_timing"
t.string "stdout"
@ -36,4 +36,15 @@ ActiveRecord::Schema.define(:version => 20120114195316) do
add_index "asciicasts", ["recorded_at"], :name => "index_asciicasts_on_recorded_at"
add_index "asciicasts", ["user_id"], :name => "index_asciicasts_on_user_id"
create_table "users", :force => true do |t|
t.string "provider"
t.string "uid"
t.string "email"
t.string "name"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
add_index "users", ["provider", "uid"], :name => "index_users_on_provider_and_uid", :unique => true
end

@ -0,0 +1,10 @@
# Read about factories at http://github.com/thoughtbot/factory_girl
FactoryGirl.define do
factory :user do
provider "twitter"
uid "1234"
email "foo@bar.com"
name "foo"
end
end

@ -0,0 +1,36 @@
require 'spec_helper'
describe User do
let(:user) { Factory.build(:user) }
it "has valid factory" do
Factory.build(:user).valid?.should be_true
end
describe ".create_with_omniauth" do
let(:email) { "foo@bar.com" }
let(:uid) { "123" }
let(:provider) { "twitter" }
let(:name) { "foo" }
let(:auth) do
{
"provider" => provider,
"uid" => uid,
"user_info" => {
"name" => name,
"email" => email }
}
end
it "should create user with valid attributes" do
user = User.create_with_omniauth(auth)
user.provider.should == provider
user.uid.should == uid
user.name.should == name
user.email.should == email
end
end
end
Loading…
Cancel
Save