From 8fadbd353415cb69986d662aa368a4dc8ac33a53 Mon Sep 17 00:00:00 2001 From: Robert Koeze Date: Sun, 20 Apr 2025 20:41:01 -0400 Subject: [PATCH] Move gravatar_enable method to User model This method configures a gravatar for the user. Since this is business logic let's put it on the model instead of in the controller. Now we can also test this method outside of the request/response lifecycle. --- app/controllers/confirmations_controller.rb | 24 ++------------------- app/models/user.rb | 20 +++++++++++++++++ 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/app/controllers/confirmations_controller.rb b/app/controllers/confirmations_controller.rb index cd22dc23c..675d8dd90 100644 --- a/app/controllers/confirmations_controller.rb +++ b/app/controllers/confirmations_controller.rb @@ -28,7 +28,7 @@ class ConfirmationsController < ApplicationController else user.activate user.email_valid = true - flash[:notice] = gravatar_status_message(user) if gravatar_enable(user) + flash[:notice] = gravatar_status_message(user) if user.gravatar_enable user.save! cookies.delete :_osm_anonymous_notes_count referer = safe_referer(params[:referer]) if params[:referer] @@ -73,7 +73,7 @@ class ConfirmationsController < ApplicationController current_user.email = current_user.new_email current_user.new_email = nil current_user.email_valid = true - gravatar_enabled = gravatar_enable(current_user) + gravatar_enabled = current_user.gravatar_enable if current_user.save flash[:notice] = if gravatar_enabled "#{t('.success')} #{gravatar_status_message(current_user)}" @@ -97,26 +97,6 @@ class ConfirmationsController < ApplicationController private - ## - # check if this user has a gravatar and set the user pref is true - def gravatar_enable(user) - # code from example https://en.gravatar.com/site/implement/images/ruby/ - return false if user.avatar.attached? - - begin - hash = Digest::MD5.hexdigest(user.email.downcase) - url = "https://www.gravatar.com/avatar/#{hash}?d=404" # without d=404 we will always get an image back - response = OSM.http_client.get(URI.parse(url)) - available = response.success? - rescue StandardError - available = false - end - - oldsetting = user.image_use_gravatar - user.image_use_gravatar = available - oldsetting != user.image_use_gravatar - end - ## # display a message about the current status of the Gravatar setting def gravatar_status_message(user) diff --git a/app/models/user.rb b/app/models/user.rb index 27e42e665..48093ddba 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -446,6 +446,26 @@ class User < ApplicationRecord deletion_allowed_at <= Time.now.utc end + ## + # check if this user has a gravatar and set the user pref is true + def gravatar_enable + # code from example https://en.gravatar.com/site/implement/images/ruby/ + return false if avatar.attached? + + begin + hash = Digest::MD5.hexdigest(email.downcase) + url = "https://www.gravatar.com/avatar/#{hash}?d=404" # without d=404 we will always get an image back + response = OSM.http_client.get(URI.parse(url)) + available = response.success? + rescue StandardError + available = false + end + + oldsetting = image_use_gravatar + self.image_use_gravatar = available + oldsetting != image_use_gravatar + end + private def encrypt_password -- 2.39.5