diff --git a/app/models/user.rb b/app/models/user.rb index 8a9a746..d02baa0 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 2d5b4c9..90c6ec7 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -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) }