diff --git a/app/models/crop.rb b/app/models/crop.rb index 15787f6f1..682610436 100644 --- a/app/models/crop.rb +++ b/app/models/crop.rb @@ -120,12 +120,7 @@ class Crop < ActiveRecord::Base # later we can choose a default photo based on different criteria, # eg. popularity def default_photo - # most recent photo - return photos.order(created_at: :desc).first if photos.any? - - # Crop has no photos? Look for the most recent harvest with a photo. - harvest_with_photo = Harvest.where(crop_id: id).joins(:photos).order('harvests.id DESC').limit(1).first - harvest_with_photo.photos.first if harvest_with_photo + first_photo(:plantings) || first_photo(:harvests) || first_photo(:seeds) end # returns hash indicating whether this crop is grown in @@ -242,4 +237,8 @@ class Crop < ActiveRecord::Base return unless reason_for_rejection == "other" && rejection_notes.blank? errors.add(:rejection_notes, "must be added if the reason for rejection is \"other\"") end + + def first_photo(type) + Photo.joins(type).where("#{type}": { crop_id: id }).order("photos.created_at DESC").first + end end diff --git a/spec/helpers/photos_helper_spec.rb b/spec/helpers/photos_helper_spec.rb new file mode 100644 index 000000000..60ba61fb4 --- /dev/null +++ b/spec/helpers/photos_helper_spec.rb @@ -0,0 +1,88 @@ +require 'rails_helper' + +describe PhotosHelper do + let(:crop) { FactoryBot.create :crop } + let(:crop_photo) { FactoryBot.create :photo, thumbnail_url: 'crop.jpg' } + let(:garden) { FactoryBot.create :garden } + let(:garden_photo) { FactoryBot.create(:photo, thumbnail_url: 'garden.jpg') } + let(:planting) { FactoryBot.create :planting } + let(:planting_photo) { FactoryBot.create(:photo, thumbnail_url: 'planting.jpg') } + let(:harvest) { FactoryBot.create :harvest } + let(:harvest_photo) { FactoryBot.create(:photo, thumbnail_url: 'harvest.jpg') } + let(:seed) { FactoryBot.create :seed } + let(:seed_photo) { FactoryBot.create(:photo, thumbnail_url: 'seed.jpg') } + + describe "crops" do + subject { crop_image_path(crop) } + + it { is_expected.to eq 'placeholder_150.png' } + + describe "with a planting" do + before do + planting.photos << planting_photo + crop.plantings << planting + end + it "uses planting photos" do + is_expected.to eq planting_photo.thumbnail_url + end + end + + describe "with a harvest photos" do + before do + harvest.photos << harvest_photo + crop.harvests << harvest + end + it "uses harvest photos" do + is_expected.to eq harvest_photo.thumbnail_url + end + end + + describe "uses seed photo" do + before do + seed.photos << seed_photo + crop.seeds << seed + end + it "uses seed photos" do + is_expected.to eq seed_photo.thumbnail_url + end + end + end + + describe "gardens" do + subject { garden_image_path(garden) } + it { is_expected.to eq 'placeholder_150.png' } + + describe "uses garden's own photo" do + before { garden.photos << garden_photo } + it { is_expected.to eq garden_photo.thumbnail_url } + end + end + + describe 'plantings' do + subject { planting_image_path(planting) } + it { is_expected.to eq 'placeholder_150.png' } + describe "uses planting's own photo" do + before { planting.photos << planting_photo } + it { is_expected.to eq planting_photo.thumbnail_url } + end + end + + describe 'harvests' do + subject { harvest_image_path(harvest) } + it { is_expected.to eq 'placeholder_150.png' } + describe "uses harvest's own photo" do + before { harvest.photos << harvest_photo } + it { is_expected.to eq harvest_photo.thumbnail_url } + end + end + + describe 'seeds' do + subject { seed_image_path(seed) } + it { is_expected.to eq 'placeholder_150.png' } + + describe "uses seed's own photo" do + before { seed.photos << seed_photo } + it { is_expected.to eq seed_photo.thumbnail_url } + end + end +end