Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions app/helpers/users_helper.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,44 @@
module UsersHelper

# see also: /config/initializers/gravatar_image_tag.rb

def avatar(user)
case user.avatar_provider
when 'gravatar'
if user.avatar.nil? or user.avatar.empty? then
content_tag :div, nil, class: [ 'avatar-missing' ]
else
else
gravatar_image_tag user.avatar, class: user.active? ? '' : 'disabled'
end
when 'webfinger'
webfinger_activitypub_image_tag user.avatar
end
end

def get_avatar_url(user)
case user.avatar_provider
when 'gravatar'
if user.avatar.nil? or user.avatar.empty? then
return nil
end

id = Digest::MD5.hexdigest(user.avatar)
return "https://secure.gravatar.com/avatar/#{id}"
when 'webfinger'
identifier = user.avatar
avatar_url = Rails.cache.fetch("fetch_avatar_url_from_webfinger_or_activitypub #{identifier}", expires_in: 1.day) do
fetch_avatar_url_from_webfinger_or_activitypub identifier
end

if not avatar_url
return nil
end

return avatar_url
end

end

def redirect_path(user)
return users_path + '/#' + user.initial
end
Expand Down
12 changes: 10 additions & 2 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

include UsersHelper

class User < ApplicationRecord
validates :name, presence: true
validates :barcode, uniqueness: { conditions: -> { where.not(barcode: nil) } }
Expand All @@ -15,7 +17,7 @@ class User < ApplicationRecord
user.barcode = nil
end
end


after_save do |user|
Audit.create! difference: user.balance - user.balance_before_last_save, drink: @purchased_drink, user: user.audit? ? user : nil
Expand Down Expand Up @@ -58,7 +60,13 @@ def email
end
end

def avatar_url
@url = get_avatar_url(self)
@url
end


def as_json(options={})
super(except: [ :avatar_provider, :avatar ], methods: :email)
super(except: [ :avatar_provider, :avatar ], methods: [ :email, :avatar_url ])
end
end
Loading