User#add_user_token

This commit is contained in:
Marcin Kulik 2012-03-04 20:29:19 +01:00
parent 9f51211bc1
commit 0de5784e68
2 changed files with 27 additions and 0 deletions

View File

@ -16,4 +16,7 @@ class User < ActiveRecord::Base
end
end
def add_user_token(token)
user_tokens.find_or_create_by_token(token)
end
end

View File

@ -47,4 +47,28 @@ describe User do
end
end
describe '#add_user_token' do
before { user.save }
context "when user doesn't have given token" do
let(:token) { Factory.attributes_for(:user_token)[:token] }
it 'returns created UserToken' do
ut = user.add_user_token(token)
ut.should be_kind_of(UserToken)
ut.id.should_not be(nil)
end
end
context "when user doesn't have given token" do
let(:existing_token) { Factory(:user_token, :user => user) }
let(:token) { existing_token.token }
it 'returns existing UserToken' do
ut = user.add_user_token(token)
ut.should == existing_token
end
end
end
end