Merge branch 'master' into comments
* master: Display asciicast author and creation time Claiming asciicasts User#add_user_token Asciicast.assign_user User tokens for assigning asciicasts to users Better styles + pagination Conflicts: app/models/asciicast.rb db/schema.rb spec/factories/asciicasts.rb spec/factories/users.rbopenid
commit
c4f40322ed
@ -0,0 +1,21 @@
|
||||
class UserTokensController < ApplicationController
|
||||
# before_filter :ensure_authenticated!
|
||||
|
||||
def create
|
||||
ut = current_user.add_user_token(params[:user_token])
|
||||
|
||||
if ut.valid?
|
||||
claimed_num = Asciicast.assign_user(ut.token, current_user)
|
||||
|
||||
if claimed_num > 0
|
||||
notice = "Claimed #{claimed_num} asciicasts, yay!"
|
||||
else
|
||||
notice = "Authenticated successfully, yippie!"
|
||||
end
|
||||
|
||||
redirect_to root_path, :notice => notice
|
||||
else
|
||||
render :error
|
||||
end
|
||||
end
|
||||
end
|
@ -0,0 +1,5 @@
|
||||
class UserToken < ActiveRecord::Base
|
||||
belongs_to :user
|
||||
|
||||
validates :user, :token, :presence => true
|
||||
end
|
@ -0,0 +1,6 @@
|
||||
<li class="asciicast-preview">
|
||||
<h3><%= link_to asciicast_title(asciicast), asciicast %></h3>
|
||||
<% if asciicast.description.present? %>
|
||||
<p class="description"><%= asciicast.description %></p>
|
||||
<% end %>
|
||||
</li>
|
@ -1,9 +1,7 @@
|
||||
<h2>Some title</h2>
|
||||
<h2>Recent asciicasts</h2>
|
||||
|
||||
<ul class="asciicasts">
|
||||
<% @asciicasts.each do |asciicast| %>
|
||||
<li class="asciicast">
|
||||
<a href="/<%= asciicast.id %>"><%= asciicast.id %></a>
|
||||
</li>
|
||||
<% end %>
|
||||
<%= render :partial => "asciicasts/preview", :collection => @asciicasts, :as => :asciicast %>
|
||||
</ul>
|
||||
|
||||
<%= paginate @asciicasts %>
|
||||
|
@ -0,0 +1,3 @@
|
||||
<h2>Oops.</h2>
|
||||
|
||||
<p>Seems like the token is incorrect. Please make sure you've pasted it correctly.</p>
|
@ -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
|
@ -0,0 +1,49 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe UserTokensController do
|
||||
describe '#create' do
|
||||
let(:user) { Factory(:user) }
|
||||
let(:user_token) { Factory.build(:user_token, :user => nil) }
|
||||
|
||||
before do
|
||||
@controller.stub!(:current_user => user)
|
||||
user.stub!(:add_user_token => user_token)
|
||||
end
|
||||
|
||||
context 'when given token is valid' do
|
||||
before do
|
||||
user_token.stub!(:valid? => true)
|
||||
end
|
||||
|
||||
it 'calls Asciicast.assign_user' do
|
||||
Asciicast.should_receive(:assign_user).with(user_token.token, user).and_return(1)
|
||||
|
||||
post :create, :user_token => user_token.token
|
||||
end
|
||||
|
||||
it 'redirects to root_path' do
|
||||
post :create, :user_token => user_token.token
|
||||
|
||||
response.should redirect_to(root_path)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when given token is invalid' do
|
||||
before do
|
||||
user_token.stub!(:valid? => false)
|
||||
end
|
||||
|
||||
it 'calls Asciicast.assign_user' do
|
||||
Asciicast.should_not_receive(:assign_user)
|
||||
|
||||
post :create, :user_token => user_token.token
|
||||
end
|
||||
|
||||
it 'renders :error' do
|
||||
post :create, :user_token => user_token.token
|
||||
|
||||
response.should render_template(:error)
|
||||
end
|
||||
end
|
||||
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
|
||||
sequence(:token) { |n| "2b4b4e02-6613-11e1-9be5-#{Kernel.format('%012i', n)}" }
|
||||
end
|
||||
end
|
@ -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,70 @@
|
||||
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 '.assign_user' do
|
||||
let(:user) { Factory(:user) }
|
||||
let(:token) { 'token' }
|
||||
let!(:asciicast) { Factory(:asciicast, :user => nil, :user_token => token) }
|
||||
|
||||
subject { Asciicast.assign_user(token, user) }
|
||||
|
||||
it 'returns number of updated records' do
|
||||
subject.should == 1
|
||||
end
|
||||
|
||||
it 'assigns user to matching asciicasts' do
|
||||
subject
|
||||
asciicast.reload.user.should == user
|
||||
end
|
||||
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,7 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe UserToken do
|
||||
it "has valid factory" do
|
||||
Factory.build(:user_token).should be_valid
|
||||
end
|
||||
end
|
@ -0,0 +1,11 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe 'connect routing' do
|
||||
it 'routes /connect/:user_token to user_tokens#create for user_token' do
|
||||
{ :get => '/connect/jolka-misio' }.should route_to(
|
||||
:controller => 'user_tokens',
|
||||
:action => 'create',
|
||||
:user_token => 'jolka-misio'
|
||||
)
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue