Add user model, and .create_with_omniauth method
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
|
@ -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…
Reference in New Issue