Skip to content

Commit b35be1b

Browse files
committed
prevented delegation errors for users without a community user (data errors - we used to hard delete)
1 parent 15f6e41 commit b35be1b

1 file changed

Lines changed: 9 additions & 1 deletion

File tree

app/models/user.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class User < ApplicationRecord
3838

3939
validates :login_token, uniqueness: { allow_blank: true, case_sensitive: false }
4040

41-
delegate :reputation, :reputation=, :privilege?, :privilege, to: :community_user
41+
delegate :reputation, :reputation=, :privilege?, :privilege, to: :community_user, allow_nil: true
4242

4343
alias_attribute :name, :username
4444

@@ -57,6 +57,12 @@ def self.search(term)
5757
where('username LIKE ?', "%#{sanitize_sql_like(term)}%")
5858
end
5959

60+
# Safely gets the user's reputation even if they don't have a community user
61+
# @return [Integer] user's reputation
62+
def reputation
63+
community_user&.reputation || 1
64+
end
65+
6066
# Safely gets the user's trust level even if they don't have a community user
6167
# @return [Integer] user's trust level
6268
def trust_level
@@ -208,6 +214,8 @@ def can_update?(post, post_type)
208214
end
209215

210216
def metric(key)
217+
return 0 unless community_user
218+
211219
Rails.cache.fetch("community_user/#{community_user.id}/metric/#{key}", expires_in: 24.hours) do
212220
case key
213221
when 'p'

0 commit comments

Comments
 (0)