diff --git a/app/controllers/async_info_controller.rb b/app/controllers/async_info_controller.rb index c903de0e9..50a1590c7 100644 --- a/app/controllers/async_info_controller.rb +++ b/app/controllers/async_info_controller.rb @@ -43,7 +43,7 @@ class AsyncInfoController < ApplicationController id: @user.id, name: @user.name, username: @user.username, - profile_image_90: Images::Profile.call(@user.profile_image_url, length: 90), + profile_image_90: @user.profile_image_url_for(length: 90), followed_tags: @user.cached_followed_tags.to_json(only: %i[id name bg_color_hex text_color_hex hotness_score], methods: [:points]), followed_podcast_ids: @user.cached_following_podcasts_ids, diff --git a/app/controllers/notifications_controller.rb b/app/controllers/notifications_controller.rb index 71ca05ea9..d9128c486 100644 --- a/app/controllers/notifications_controller.rb +++ b/app/controllers/notifications_controller.rb @@ -47,7 +47,16 @@ class NotificationsController < ApplicationController # the first few notifications. After that the JS frontend code (see `initNotification.js`) # will call this action again by sending the offset id for the last known notifications, the result # will be the partial rendering of only the list of notifications that will be attached to the DOM by JS - render partial: "notifications_list" if notified_at_offset + if notified_at_offset + render partial: "notifications_list" + else + # [@jeremyf] I added an explicit render. Before adding the explicit render, we relied on the + # implicit render cycle of Rails. Which is fine, but it's very disarming to read `render + # partial: "notifications_list" if notified_at_offset` as the last line of the method. + # + # My hope is that this explicit render removes at least one future head scratch. + render "index" + end end # rubocop:enable Metrics/CyclomaticComplexity # rubocop:enable Metrics/PerceivedComplexity diff --git a/app/controllers/stories_controller.rb b/app/controllers/stories_controller.rb index 07b9ea0b9..9ba1668e3 100644 --- a/app/controllers/stories_controller.rb +++ b/app/controllers/stories_controller.rb @@ -337,11 +337,11 @@ class StoriesController < ApplicationController }, url: URL.user(@user), sameAs: user_same_as, - image: Images::Profile.call(@user.profile_image_url, length: 320), + image: @user.profile_image_url_for(length: 320), name: @user.name, email: decorated_user.profile_email, description: decorated_user.profile_summary - }.reject { |_, v| v.blank? } + }.compact_blank end def set_article_json_ld @@ -399,7 +399,7 @@ class StoriesController < ApplicationController "@id": URL.organization(@organization) }, url: URL.organization(@organization), - image: Images::Profile.call(@organization.profile_image_url, length: 320), + image: @organization.profile_image_url_for(length: 320), name: @organization.name, description: @organization.summary.presence || "404 bio not found" } @@ -412,6 +412,6 @@ class StoriesController < ApplicationController @user.twitter_username.present? ? "https://twitter.com/#{@user.twitter_username}" : nil, @user.github_username.present? ? "https://github.com/#{@user.github_username}" : nil, @user.profile.website_url, - ].reject(&:blank?) + ].compact_blank end end diff --git a/app/models/articles/cached_entity.rb b/app/models/articles/cached_entity.rb index 612444df5..0a080f7dc 100644 --- a/app/models/articles/cached_entity.rb +++ b/app/models/articles/cached_entity.rb @@ -1,6 +1,8 @@ module Articles # NOTE: articles cache either users or organizations, but they have the same attributes. CachedEntity = Struct.new(:name, :username, :slug, :profile_image_90, :profile_image_url) do + include Images::Profile.for(:profile_image_url) + def self.from_object(object) new( object.name, diff --git a/app/models/organization.rb b/app/models/organization.rb index d963fd772..a77a3b169 100644 --- a/app/models/organization.rb +++ b/app/models/organization.rb @@ -1,6 +1,8 @@ class Organization < ApplicationRecord include CloudinaryHelper + include Images::Profile.for(:profile_image_url) + COLOR_HEX_REGEXP = /\A#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})\z/ INTEGER_REGEXP = /\A\d+\z/ SLUG_REGEXP = /\A[a-zA-Z0-9\-_]+\z/ @@ -103,7 +105,7 @@ class Organization < ApplicationRecord end def profile_image_90 - Images::Profile.call(profile_image_url, length: 90) + profile_image_url_for(length: 90) end def enough_credits?(num_credits_needed) diff --git a/app/models/podcast.rb b/app/models/podcast.rb index 1b825217f..273731c69 100644 --- a/app/models/podcast.rb +++ b/app/models/podcast.rb @@ -1,6 +1,8 @@ class Podcast < ApplicationRecord resourcify + include Images::Profile.for(:profile_image_url) + belongs_to :creator, class_name: "User", inverse_of: :created_podcasts, optional: true has_many :podcast_episodes, dependent: :destroy @@ -50,7 +52,7 @@ class Podcast < ApplicationRecord end def image_90 - Images::Profile.call(profile_image_url, length: 90) + profile_image_url_for(length: 90) end private diff --git a/app/models/user.rb b/app/models/user.rb index fd5c6323a..dd879e93a 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -4,6 +4,8 @@ class User < ApplicationRecord include CloudinaryHelper + include Images::Profile.for(:profile_image_url) + # NOTE: we are using an inline module to keep profile related things together. concerning :Profiles do included do @@ -507,7 +509,7 @@ class User < ApplicationRecord end def profile_image_90 - Images::Profile.call(profile_image_url, length: 90) + profile_image_url_for(length: 90) end def unsubscribe_from_newsletters diff --git a/app/serializers/search/podcast_episode_serializer.rb b/app/serializers/search/podcast_episode_serializer.rb index f3359e3c4..af5be99e9 100644 --- a/app/serializers/search/podcast_episode_serializer.rb +++ b/app/serializers/search/podcast_episode_serializer.rb @@ -1,7 +1,7 @@ module Search class PodcastEpisodeSerializer < ApplicationSerializer def self.podcast_image_url(podcast_episode) - Images::Profile.call(podcast_episode.podcast.profile_image_url, length: 90) + podcast_episode.podcast.profile_image_url_for(length: 90) end attribute :id, &:search_id diff --git a/app/services/images/profile.rb b/app/services/images/profile.rb index 4031cbbff..384c866e8 100644 --- a/app/services/images/profile.rb +++ b/app/services/images/profile.rb @@ -1,5 +1,22 @@ module Images module Profile + # A convenience module for wrapping the Profile::Images logic. + # + # @param attribute [Symbol] named attribute that is an image_url + # + # @return [Module] for inclusion into the given class + # + # @example + # class User + # include Images::Profile.for(:image_url) + # end + def self.for(attribute) + Module.new do + define_method("#{attribute}_for") do |length:| + Images::Profile.call(public_send(attribute), length: length) + end + end + end BACKUP_LINK = "https://thepracticaldev.s3.amazonaws.com/i/99mvlsfu5tfj9m7ku25d.png".freeze def self.call(image_url, length: 120) diff --git a/app/views/admin/comments/index.html.erb b/app/views/admin/comments/index.html.erb index dfb5b2534..0fccede1f 100644 --- a/app/views/admin/comments/index.html.erb +++ b/app/views/admin/comments/index.html.erb @@ -19,7 +19,7 @@
<%= t("views.logo.color_1.description") %>
<%= t("views.logo.color_2.description_html") %>