Refactor User.for_credentials/for_email into class methods
This commit is contained in:
parent
e95ac75c31
commit
a2b0a637fa
@ -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
|
||||
|
@ -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) }
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user