From 9ba59608cb4be43c7977711dc55f0a36d68f76ca Mon Sep 17 00:00:00 2001 From: Andrew Tomaka Date: Thu, 2 Oct 2014 16:29:18 -0400 Subject: [PATCH] User model --- app/models/user.rb | 11 ++++++++++ db/migrate/20141002202749_create_users.rb | 12 +++++++++++ db/schema.rb | 25 +++++++++++++++++++++++ test/fixtures/users.yml | 13 ++++++++++++ test/models/user_test.rb | 7 +++++++ 5 files changed, 68 insertions(+) create mode 100644 app/models/user.rb create mode 100644 db/migrate/20141002202749_create_users.rb create mode 100644 db/schema.rb create mode 100644 test/fixtures/users.yml create mode 100644 test/models/user_test.rb diff --git a/app/models/user.rb b/app/models/user.rb new file mode 100644 index 0000000..b1903c3 --- /dev/null +++ b/app/models/user.rb @@ -0,0 +1,11 @@ +class User < ActiveRecord::Base + def self.from_omniauth(auth) + where(provider: auth[:provider], uid: auth[:uid]).first_or_create do |user| + user.provider = auth[:provider] + user.uid = auth[:uid] + user.name = auth[:info][:name] + user.email = auth[:info][:email] + user.save + end + end +end diff --git a/db/migrate/20141002202749_create_users.rb b/db/migrate/20141002202749_create_users.rb new file mode 100644 index 0000000..fb03193 --- /dev/null +++ b/db/migrate/20141002202749_create_users.rb @@ -0,0 +1,12 @@ +class CreateUsers < ActiveRecord::Migration + def change + create_table :users do |t| + t.string :provider + t.string :uid + t.string :name + t.string :email + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 0000000..f1104d1 --- /dev/null +++ b/db/schema.rb @@ -0,0 +1,25 @@ +# encoding: UTF-8 +# This file is auto-generated from the current state of the database. Instead +# of editing this file, please use the migrations feature of Active Record to +# incrementally modify your database, and then regenerate this schema definition. +# +# Note that this schema.rb definition is the authoritative source for your +# database schema. If you need to create the application database on another +# system, you should be using db:schema:load, not running all the migrations +# from scratch. The latter is a flawed and unsustainable approach (the more migrations +# you'll amass, the slower it'll run and the greater likelihood for issues). +# +# It's strongly recommended that you check this file into your version control system. + +ActiveRecord::Schema.define(version: 20141002202749) do + + create_table "users", force: true do |t| + t.string "provider" + t.string "uid" + t.string "name" + t.string "email" + t.datetime "created_at" + t.datetime "updated_at" + end + +end diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml new file mode 100644 index 0000000..a642bda --- /dev/null +++ b/test/fixtures/users.yml @@ -0,0 +1,13 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + provider: MyString + uid: MyString + name: MyString + email: MyString + +two: + provider: MyString + uid: MyString + name: MyString + email: MyString diff --git a/test/models/user_test.rb b/test/models/user_test.rb new file mode 100644 index 0000000..82f61e0 --- /dev/null +++ b/test/models/user_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class UserTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end