From a6c89351c5b6c6cafd9377a75623ec8390d3add6 Mon Sep 17 00:00:00 2001 From: Christian Bruckmayer Date: Thu, 10 Mar 2016 13:59:59 +0100 Subject: [PATCH] Make Comment find_since_last_login scope robust against nil value to fix production ArgumentError: bad value for range. Fixes #807. --- app/models/comment.rb | 6 +++++- spec/models/comment_spec.rb | 10 ++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 spec/models/comment_spec.rb diff --git a/app/models/comment.rb b/app/models/comment.rb index 40d69240..c699f18a 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -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 diff --git a/spec/models/comment_spec.rb b/spec/models/comment_spec.rb new file mode 100644 index 00000000..94cb6372 --- /dev/null +++ b/spec/models/comment_spec.rb @@ -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