Refactor User.for_credentials/for_email into class methods

This commit is contained in:
Marcin Kulik 2013-10-20 18:51:35 +02:00
parent e95ac75c31
commit a2b0a637fa
2 changed files with 41 additions and 3 deletions

View File

@ -18,11 +18,13 @@ class User < ActiveRecord::Base
attr_accessible :nickname, :email, :name
scope :for_credentials, -> (credentials) {
def self.for_credentials(credentials)
where(provider: credentials.provider, uid: credentials.uid).first
}
end
scope :for_email, -> (email) { where(email: email).first }
def self.for_email(email)
where(email: email).first
end
def to_param
nickname

View File

@ -14,6 +14,42 @@ describe User do
end
end
describe '.for_credentials' do
subject { described_class.for_credentials(credentials) }
let!(:user) { create(:user, provider: 'twitter', uid: '1') }
context "when there is matching record" do
let(:credentials) { double('credentials', provider: 'twitter', uid: '1') }
it { should eq(user) }
end
context "when there isn't matching record" do
let(:credentials) { double('credentials', provider: 'twitter', uid: '2') }
it { should be(nil) }
end
end
describe '.for_email' do
subject { described_class.for_email(email) }
let!(:user) { create(:user, email: 'foo@bar.com') }
context "when there is matching record" do
let(:email) { 'foo@bar.com' }
it { should eq(user) }
end
context "when there isn't matching record" do
let(:email) { 'qux@bar.com' }
it { should be(nil) }
end
end
describe '#add_user_token' do
let(:user) { build(:user) }