From 5fbdd577d682469702e4a55db89b73ec0e822f2f Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Mon, 3 Feb 2020 11:54:26 +1300 Subject: [PATCH] One per person on home page --- app/models/concerns/search_harvests.rb | 24 ++++++--- app/models/concerns/search_plantings.rb | 72 +++++++++++++++---------- 2 files changed, 63 insertions(+), 33 deletions(-) diff --git a/app/models/concerns/search_harvests.rb b/app/models/concerns/search_harvests.rb index 79b7c9796..43ae17587 100644 --- a/app/models/concerns/search_harvests.rb +++ b/app/models/concerns/search_harvests.rb @@ -49,12 +49,24 @@ module SearchHarvests end def self.homepage_records(limit) - search('*', limit: limit, - where: { - photos_count: { gt: 0 } - }, - boost_by: [:created_at], - load: false) + records = [] + owners = [] + 1..limit.times do + where = { + photos_count: { gt: 0 }, + owner_id: { not: owners } + } + one_record = search('*', + limit: 1, + where: where, + boost_by: [:created_at], + load: false).first + return records if one_record.nil? + + owners << one_record.owner_id + records << one_record + end + records end end end diff --git a/app/models/concerns/search_plantings.rb b/app/models/concerns/search_plantings.rb index 680b4a1d7..61fd4fc2c 100644 --- a/app/models/concerns/search_plantings.rb +++ b/app/models/concerns/search_plantings.rb @@ -18,46 +18,64 @@ module SearchPlantings def search_data { - slug: slug, - active: active, - finished: finished?, - has_photos: photos.size.positive?, - location: location, - percentage_grown: percentage_grown.to_i, - planted_at: planted_at, - planted_from: planted_from, - quantity: quantity, - sunniness: sunniness, + slug: slug, + active: active, + finished: finished?, + has_photos: photos.size.positive?, + location: location, + percentage_grown: percentage_grown.to_i, + planted_at: planted_at, + planted_from: planted_from, + planted_year: planted_at.year, + quantity: quantity, + sunniness: sunniness, + garden_id: garden_id, + + first_harvest_predicted_at: first_harvest_predicted_at, + finish_predicted_at: finish_predicted_at, # crops - crop_id: crop_id, - crop_name: crop.name, - crop_slug: crop.slug, + crop_id: crop_id, + crop_name: crop_name, + crop_slug: crop_slug, + crop_perennial: crop_perennial, # owner - owner_id: owner_id, - owner_location: owner_location, - owner_login_name: owner_login_name, - owner_slug: owner_slug, + owner_id: owner_id, + owner_location: owner_location, + owner_login_name: owner_login_name, + owner_slug: owner_slug, # photos - thumbnail_url: default_photo&.thumbnail_url || crop.default_photo&.thumbnail_url, + thumbnail_url: default_photo&.thumbnail_url || crop.default_photo&.thumbnail_url, # counts - photos_count: photos.size, - harvests_count: harvests_count, + photos_count: photos.size, + harvests_count: harvests_count, # timestamps - created_at: created_at.to_i + created_at: created_at.to_i } end def self.homepage_records(limit) - search('*', limit: limit, - where: { - photos_count: { gt: 0 } - }, - boost_by: [:created_at], - load: false) + records = [] + owners = [] + 1..limit.times do + where = { + photos_count: { gt: 0 }, + owner_id: { not: owners } + } + one_record = search('*', + limit: 1, + where: where, + boost_by: [:created_at], + load: false).first + return records if one_record.nil? + + owners << one_record.owner_id + records << one_record + end + records end end end