User tokens for assigning asciicasts to users

openid
Marcin Kulik 12 years ago
parent b7807c3f0d
commit 1ba9067551

@ -7,11 +7,16 @@ class Asciicast < ActiveRecord::Base
validates :stdout, :stdout_timing, :presence => true
validates :terminal_columns, :terminal_lines, :duration, :presence => true
belongs_to :user
before_create :assign_user, :unless => :user
attr_reader :description
def meta=(file)
data = JSON.parse(file.tempfile.read)
self.user_token = data['user_token']
self.duration = data['duration']
self.recorded_at = data['recorded_at']
self.title = data['title']
@ -45,4 +50,13 @@ class Asciicast < ActiveRecord::Base
nil
end
end
def assign_user
if user_token.present?
if ut = UserToken.find_by_token(user_token)
self.user = ut.user
self.user_token = nil
end
end
end
end

@ -4,6 +4,8 @@ class User < ActiveRecord::Base
validate :uid, :presence => true
validate :nickname, :presence => true
has_many :user_tokens
def self.create_with_omniauth(auth)
create! do |user|
user.provider = auth["provider"]

@ -0,0 +1,5 @@
class UserToken < ActiveRecord::Base
belongs_to :user
validates :user, :token, :presence => true
end

@ -0,0 +1,7 @@
class AddUserTokenToAsciicast < ActiveRecord::Migration
def change
add_column :asciicasts, :user_token, :string
add_index :asciicasts, :user_token
end
end

@ -0,0 +1,13 @@
class CreateUserTokens < ActiveRecord::Migration
def change
create_table :user_tokens do |t|
t.integer :user_id, :null => false
t.string :token, :null => false
t.timestamps
end
add_index :user_tokens, :user_id
add_index :user_tokens, :token
end
end

@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20120226184448) do
ActiveRecord::Schema.define(:version => 20120304162005) do
create_table "asciicasts", :force => true do |t|
t.integer "user_id"
@ -24,17 +24,29 @@ ActiveRecord::Schema.define(:version => 20120226184448) do
t.string "command"
t.string "shell"
t.string "uname"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
t.string "stdin"
t.string "stdin_timing"
t.string "stdout"
t.string "stdout_timing"
t.string "user_token"
end
add_index "asciicasts", ["created_at"], :name => "index_asciicasts_on_created_at"
add_index "asciicasts", ["recorded_at"], :name => "index_asciicasts_on_recorded_at"
add_index "asciicasts", ["user_id"], :name => "index_asciicasts_on_user_id"
add_index "asciicasts", ["user_token"], :name => "index_asciicasts_on_user_token"
create_table "user_tokens", :force => true do |t|
t.integer "user_id", :null => false
t.string "token", :null => false
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
add_index "user_tokens", ["token"], :name => "index_user_tokens_on_token"
add_index "user_tokens", ["user_id"], :name => "index_user_tokens_on_user_id"
create_table "users", :force => true do |t|
t.string "provider", :null => false

@ -2,15 +2,16 @@
FactoryGirl.define do
factory :asciicast do
user_id 1
title "MyString"
duration 1
association :user
title "bashing"
duration 100
recorded_at "2011-11-23 22:06:07"
terminal_type "MyString"
terminal_columns 1
terminal_lines 1
command "MyString"
shell "MyString"
uname "MyString"
terminal_type "xterm"
terminal_columns 80
terminal_lines 25
shell "/bin/bash"
uname "uname"
stdout { File.open('spec/fixtures/asciicasts/1/stdout') }
stdout_timing { File.open('spec/fixtures/asciicasts/1/stdout.time') }
end
end

@ -0,0 +1,8 @@
# Read about factories at http://github.com/thoughtbot/factory_girl
FactoryGirl.define do
factory :user_token do
association :user
token "some-token"
end
end

@ -3,7 +3,7 @@
FactoryGirl.define do
factory :user do
provider "twitter"
uid "1234"
sequence(:uid) { |n| "uid-#{n}" }
nickname "mrFoo"
email nil
name nil

@ -0,0 +1,14 @@
{
"command": null,
"duration": 5.914434194564819,
"recorded_at": "Sun, 04 Mar 2012 16:43:52 +0000",
"shell": "/bin/zsh",
"term": {
"columns": 89,
"lines": 27,
"type": "rxvt-unicode-256color"
},
"title": null,
"uname": "Linux 3.2.6-3.fc16.x86_64 #1 SMP Mon Feb 13 20:35:42 UTC 2012 x86_64",
"user_token": "2b4b4e02-6613-11e1-9be5-00215c6bbb11"
}

Binary file not shown.

Binary file not shown.

@ -1,5 +1,53 @@
require 'spec_helper'
describe Asciicast do
pending "add some examples to (or delete) #{__FILE__}"
it "has valid factory" do
Factory.build(:asciicast).should be_valid
end
describe '#save' do
let(:asciicast) { Factory.build(:asciicast, :user => user) }
context 'when no user given' do
let(:user) { nil }
it 'calls #assign_user' do
asciicast.should_receive(:assign_user)
asciicast.save
end
end
context 'when user given' do
let(:user) { Factory.build(:user) }
it "doesn't call #assign_user" do
asciicast.should_not_receive(:assign_user)
asciicast.save
end
end
end
describe '#assign_user' do
let(:user) { Factory(:user) }
let(:asciicast) { Factory(:asciicast, :user => nil, :user_token => user_token) }
context 'when user exists with given token' do
let(:user_token) { Factory(:user_token, :user => user).token }
it 'assigns user and resets user_token' do
asciicast.assign_user
asciicast.user.should == user
asciicast.user_token.should be(nil)
end
end
context 'when there is no user with given token' do
let(:user_token) { 'some-foo-bar' }
it 'assigns user' do
asciicast.assign_user
asciicast.user.should be(nil)
end
end
end
end

@ -0,0 +1,5 @@
require 'spec_helper'
describe UserToken do
pending "add some examples to (or delete) #{__FILE__}"
end
Loading…
Cancel
Save