diff --git a/app/helpers/crops_helper.rb b/app/helpers/crops_helper.rb index 1ac4c626e..41484b754 100644 --- a/app/helpers/crops_helper.rb +++ b/app/helpers/crops_helper.rb @@ -77,7 +77,7 @@ module CropsHelper } end - crop.posts.each do |post| + crop.posts.limit(50).each do |post| subject_of_entities << { '@type': "SocialMediaPosting", url: post_url(post), @@ -89,7 +89,7 @@ module CropsHelper } end - crop.photos.each do |photo| + crop.photos.limit(50).each do |photo| images << photo.fullsize_url end end diff --git a/app/models/concerns/photo_capable.rb b/app/models/concerns/photo_capable.rb index b8f4ff09a..9308d9068 100644 --- a/app/models/concerns/photo_capable.rb +++ b/app/models/concerns/photo_capable.rb @@ -10,18 +10,18 @@ module PhotoCapable scope :has_photos, -> { includes(:photos).where.not(photos: { id: nil }) } def default_photo - Rails.cache.fetch("#{cache_key_with_version}/default_photo", expires_in: 8.hours) do - most_liked_photo - end + most_liked_photo end def thumbnail_url - df = default_photo + Rails.cache.fetch("#{cache_key_with_version}/thumbnail_url", expires_in: 8.hours) do + df = default_photo - if df - df.source == 'flickr' ? df.fullsize_url : df.thumbnail_url - elsif respond_to?(:crop) && crop.present? - crop.thumbnail_url + if df + df.source == 'flickr' ? df.fullsize_url : df.thumbnail_url + elsif respond_to?(:crop) && crop.present? + crop.thumbnail_url + end end end diff --git a/spec/helpers/crops_helper_spec.rb b/spec/helpers/crops_helper_spec.rb index 7099ad066..9899a6e72 100644 --- a/spec/helpers/crops_helper_spec.rb +++ b/spec/helpers/crops_helper_spec.rb @@ -42,4 +42,25 @@ describe CropsHelper do end end end + + describe '#crop_jsonld_data' do + let(:crop) { create(:crop, name: 'Tomato') } + + it 'returns schema.org BioChemEntity hash structure' do + data = helper.crop_jsonld_data(crop) + expect(data['@context']).to eq('https://schema.org') + expect(data['@type']).to eq('BioChemEntity') + expect(data[:name]).to eq('Tomato') + end + + it 'caps posts and photos at 50' do + create_list(:post, 60, crops: [crop]) + photos = create_list(:photo, 60) + photos.each { |p| crop.photo_associations.create!(photo: p) } + + data = helper.crop_jsonld_data(crop, full_attributes: true) + expect(data[:subjectOf].size).to eq(50) + expect(data[:image].size).to eq(50) + end + end end diff --git a/spec/models/crop_spec.rb b/spec/models/crop_spec.rb index 2c5890dea..dce317c77 100644 --- a/spec/models/crop_spec.rb +++ b/spec/models/crop_spec.rb @@ -154,6 +154,13 @@ describe Crop do it { expect(crop.default_photo).to eq photo } + it 'caches thumbnail_url string in Rails.cache' do + expected_url = photo.source == 'flickr' ? photo.fullsize_url : photo.thumbnail_url + expect(crop.thumbnail_url).to eq expected_url + cached_value = Rails.cache.read("#{crop.cache_key_with_version}/thumbnail_url") + expect(cached_value).to eq expected_url + end + include_examples 'has default photo' end