From 29b4f2f4c7986b03d5222da8aa610fd3e5fd8516 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Mon, 10 Aug 2026 09:28:58 +0000 Subject: [PATCH 1/2] Implement crop photo fallback in PhotoCapable thumbnail_url Currently, plantings, seeds, and harvests display "no photo available" placeholders when they do not have specific photos attached. This change enables the PhotoCapable#thumbnail_url method to automatically fall back to the associated crop's thumbnail_url when no item-specific photo is present. Additionally, this commit: - Fixes validation in PhotoAssociation to handle crop photos that do not have a polymorphic photographable owner. - Disables cache store during testing to prevent stale/cached default photos from leaking across specs. - Adds comprehensive unit tests for the fallback logic. Co-authored-by: CloCkWeRX <365751+CloCkWeRX@users.noreply.github.com> --- app/models/concerns/photo_capable.rb | 8 +++++--- app/models/photo_association.rb | 1 + config/environments/test.rb | 1 + config/initializers/faraday_patch.rb | 9 +++++++++ spec/models/planting_spec.rb | 19 +++++++++++++++++++ spec/spec_helper.rb | 2 ++ spec/support/database_cleaner.rb | 4 ++++ 7 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 config/initializers/faraday_patch.rb diff --git a/app/models/concerns/photo_capable.rb b/app/models/concerns/photo_capable.rb index c1433a7fc3..b8f4ff09aa 100644 --- a/app/models/concerns/photo_capable.rb +++ b/app/models/concerns/photo_capable.rb @@ -18,9 +18,11 @@ def default_photo def thumbnail_url df = default_photo - return unless df - - df.source == 'flickr' ? df.fullsize_url : df.thumbnail_url + if df + df.source == 'flickr' ? df.fullsize_url : df.thumbnail_url + elsif respond_to?(:crop) && crop.present? + crop.thumbnail_url + end end def most_liked_photo diff --git a/app/models/photo_association.rb b/app/models/photo_association.rb index 7197cfb714..2aefb16c84 100644 --- a/app/models/photo_association.rb +++ b/app/models/photo_association.rb @@ -28,6 +28,7 @@ def set_crop def photo_and_item_have_same_owner return if photographable_type == 'Crop' + return if photographable.blank? errors.add(:photo, :photo_owner_mismatch) unless photographable.owner_id == photo.owner_id end diff --git a/config/environments/test.rb b/config/environments/test.rb index c2c4a4ecb9..926b8395ef 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -23,6 +23,7 @@ # Show full error reports and disable caching. config.consider_all_requests_local = true config.action_controller.perform_caching = false + config.cache_store = :null_store # Raise exceptions instead of rendering exception templates. config.action_dispatch.show_exceptions = :none diff --git a/config/initializers/faraday_patch.rb b/config/initializers/faraday_patch.rb new file mode 100644 index 0000000000..909dbf02c6 --- /dev/null +++ b/config/initializers/faraday_patch.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +# Compatibility shim for Faraday 2.x which removed Faraday::Error namespace +module Faraday + class Error < StandardError + ConnectionFailed = Faraday::ConnectionFailed + TimeoutError = Faraday::TimeoutError + end +end diff --git a/spec/models/planting_spec.rb b/spec/models/planting_spec.rb index 03ffe813fa..d6ef39e8f3 100644 --- a/spec/models/planting_spec.rb +++ b/spec/models/planting_spec.rb @@ -421,6 +421,25 @@ def one_hundred_day_old_planting planting.photos << @photo2 expect(planting.default_photo).to eq @photo2 end + + describe '#thumbnail_url' do + let(:crop_photo) { create(:photo) } + + it 'returns its own default photo if present' do + expect(planting.thumbnail_url).to eq photo.fullsize_url + end + + it 'falls back to crop default photo if no planting photo is present' do + planting_without_photo = create(:planting, crop:) + PhotoAssociation.create!(photo: crop_photo, photographable: crop) + expect(planting_without_photo.thumbnail_url).to eq crop_photo.fullsize_url + end + + it 'returns nil if neither planting nor crop has a photo' do + planting_without_photo = create(:planting, crop:) + expect(planting_without_photo.thumbnail_url).to be_nil + end + end end context 'interesting plantings' do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index de08a53e14..ad24697a79 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -46,6 +46,8 @@ def index_everything # reindex models Crop.reindex + rescue Faraday::ConnectionFailed, Faraday::TimeoutError, Errno::ECONNREFUSED => e + warn "Searchkick/Elasticsearch is not available: #{e.message}. Skipping reindexing." end config.before(:suite) do diff --git a/spec/support/database_cleaner.rb b/spec/support/database_cleaner.rb index 4c7f0fec59..f13034c927 100644 --- a/spec/support/database_cleaner.rb +++ b/spec/support/database_cleaner.rb @@ -43,4 +43,8 @@ config.append_after do DatabaseCleaner.clean end + + config.before(:each) do + Rails.cache.clear + end end From f05841de45d0aaab27917c427762caffeb625f36 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Mon, 10 Aug 2026 09:39:48 +0000 Subject: [PATCH 2/2] Keep only core crop photo fallback and unit tests, removing excess changes Co-authored-by: CloCkWeRX <365751+CloCkWeRX@users.noreply.github.com> --- config/environments/test.rb | 1 - config/initializers/faraday_patch.rb | 9 --------- spec/spec_helper.rb | 2 -- spec/support/database_cleaner.rb | 4 ---- 4 files changed, 16 deletions(-) delete mode 100644 config/initializers/faraday_patch.rb diff --git a/config/environments/test.rb b/config/environments/test.rb index 926b8395ef..c2c4a4ecb9 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -23,7 +23,6 @@ # Show full error reports and disable caching. config.consider_all_requests_local = true config.action_controller.perform_caching = false - config.cache_store = :null_store # Raise exceptions instead of rendering exception templates. config.action_dispatch.show_exceptions = :none diff --git a/config/initializers/faraday_patch.rb b/config/initializers/faraday_patch.rb deleted file mode 100644 index 909dbf02c6..0000000000 --- a/config/initializers/faraday_patch.rb +++ /dev/null @@ -1,9 +0,0 @@ -# frozen_string_literal: true - -# Compatibility shim for Faraday 2.x which removed Faraday::Error namespace -module Faraday - class Error < StandardError - ConnectionFailed = Faraday::ConnectionFailed - TimeoutError = Faraday::TimeoutError - end -end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index ad24697a79..de08a53e14 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -46,8 +46,6 @@ def index_everything # reindex models Crop.reindex - rescue Faraday::ConnectionFailed, Faraday::TimeoutError, Errno::ECONNREFUSED => e - warn "Searchkick/Elasticsearch is not available: #{e.message}. Skipping reindexing." end config.before(:suite) do diff --git a/spec/support/database_cleaner.rb b/spec/support/database_cleaner.rb index f13034c927..4c7f0fec59 100644 --- a/spec/support/database_cleaner.rb +++ b/spec/support/database_cleaner.rb @@ -43,8 +43,4 @@ config.append_after do DatabaseCleaner.clean end - - config.before(:each) do - Rails.cache.clear - end end