Make Comment find_since_last_login scope robust against nil value

to fix production ArgumentError: bad value for range. Fixes #807.
This commit is contained in:
Christian Bruckmayer 2016-03-10 13:59:59 +01:00
parent 55ab8a8e8f
commit a6c89351c5
2 changed files with 15 additions and 1 deletions

View file

@ -41,7 +41,11 @@ class Comment < ActiveRecord::Base
}
scope :find_since_last_login, lambda { |user|
where(created_at: (user.last_sign_in_at..Time.now)).order(created_at: :desc)
if user.last_sign_in_at
where(created_at: (user.last_sign_in_at..Time.now)).order(created_at: :desc)
else
none
end
}
# Helper class method to look up a commentable object
# given the commentable class name and id

View file

@ -0,0 +1,10 @@
require 'spec_helper'
describe Commercial do
describe '.find_since_last_login' do
it 'returns none if last_sign_in_at is nil' do
expect(Comment.find_since_last_login(create(:user))).to eq(Comment.none)
end
end
end