Pulling plantings from elastic search too

This commit is contained in:
Brenda Wallace
2019-12-10 13:36:23 +13:00
parent 43b7dc7d7c
commit 5afe3e1c1e
12 changed files with 90 additions and 23 deletions

View File

@@ -19,10 +19,11 @@ module CropSearch
# boost the crops that are planted the most
plantings_count: plantings_count,
harvests_count: harvests_count,
photos_count: photo_associations_count,
# boost this crop for these members
planters_ids: plantings.pluck(:owner_id),
has_photos: photos.size.positive?,
photo: default_photo&.thumbnail_url,
has_photos: photo_associations_count.positive?,
thumbnail_url: default_photo&.thumbnail_url,
scientific_name: default_scientific_name&.name,
description: description,
created_at: created_at.to_i

View File

@@ -12,11 +12,26 @@ module HarvestSearch
crop_slug: crop.slug,
crop_name: crop.name,
crop_id: crop_id,
plant_part: plant_part&.name,
owner_id: owner_id,
owner_name: owner.login_name,
planting_id: planting_id,
photos_count: photos.size,
has_photos: photos.size.positive?,
thumbnail_url: default_photo&.thumbnail_url,
created_at: created_at.to_i
}
end
def self.homepage_records(limit)
self.search('*',
limit: limit,
where: {
photos_count: {gt: 0},
},
boost_by: [:created_at],
load: false
)
end
end
end

View File

@@ -0,0 +1,37 @@
module PlantingSearch
extend ActiveSupport::Concern
included do
searchkick
scope :search_import, -> { includes(:owner, :crop) }
def search_data
{
slug: slug,
crop_slug: crop.slug,
crop_name: crop.name,
crop_id: crop_id,
owner_id: owner_id,
owner_name: owner.login_name,
planted_from: planted_from,
photos_count: photos.size,
harvests_count: photos.size,
has_photos: photos.size.positive?,
thumbnail_url: default_photo&.thumbnail_url,
created_at: created_at.to_i
}
end
def self.homepage_records(limit)
self.search('*',
limit: limit,
where: {
photos_count: {gt: 0},
},
boost_by: [:created_at],
load: false
)
end
end
end

View File

@@ -5,7 +5,7 @@ class Photo < ApplicationRecord
PHOTO_CAPABLE = %w(Garden Planting Harvest Seed Post Crop).freeze
has_many :photo_associations, foreign_key: :photo_id, dependent: :delete_all, inverse_of: :photo
has_many :crops, through: :photo_associations
has_many :crops, through: :photo_associations, counter_cache: true
validates :fullsize_url, url: true
validates :thumbnail_url, url: true

View File

@@ -1,7 +1,7 @@
class PhotoAssociation < ApplicationRecord
belongs_to :photo, inverse_of: :photo_associations
belongs_to :photographable, polymorphic: true
belongs_to :crop, optional: true
belongs_to :crop, optional: true, counter_cache: true
validate :photo_and_item_have_same_owner

View File

@@ -5,6 +5,8 @@ class Planting < ApplicationRecord
include Ownable
include PredictPlanting
include PredictHarvest
include PlantingSearch
friendly_id :planting_slug, use: %i(slugged finders)
# Constants

View File

@@ -29,7 +29,7 @@ class CropSearchService
limit: limit,
load: false,
body: body
).response['hits']['hits'].map { |c| c['_source'] }
)
end
def self.recent(limit)
@@ -37,6 +37,6 @@ class CropSearchService
limit: limit,
load: false,
boost_by: { created_at: { factor: 100 } } # default factor is 1
).response['hits']['hits'].map { |c| c['_source'] }
)
end
end

View File

@@ -1,7 +1,7 @@
.card.crop-thumbnail
= link_to crop_path(slug: crop['slug']) do
= image_tag(crop['photo'] ? crop['photo'] : placeholder_image,
= image_tag(crop['thumbnail_url'] ? crop['thumbnail_url'] : placeholder_image,
alt: crop['name'],
class: 'img img-card')

View File

@@ -1,11 +1,12 @@
%h2= t('.recently_harvested')
- Harvest.has_photos.recent.includes(:crop, :owner, :photos, :plant_part).limit(6).each do |harvest|
- Harvest.homepage_records(6).each do |harvest|
- cache harvest do
= link_to harvest, class: 'list-group-item list-group-item-action flex-column align-items-start' do
.d-flex.w-100.justify-content-between.homepage--list-item
%div
%h5= harvest.crop.name
%span.badge.badge-success=harvest.plant_part
%h5= harvest['crop_name']
%span.badge.badge-success=harvest['plant_part']
%small.text-muted
harvested by #{harvest.owner}
%p.mb-2= image_tag harvest_image_path(harvest), width: 75, class: 'rounded shadow'
harvested by #{harvest['owner_name']}
%p.mb-2
= image_tag harvest['thumbnail_url'], width: 75, class: 'rounded shadow'

View File

@@ -1,12 +1,11 @@
%h2= t('.recently_planted')
- Planting.has_photos.recent.includes(:owner, :crop).limit(6).each do |planting|
- cache planting do
= link_to planting, class: 'list-group-item list-group-item-action flex-column align-items-start' do
.d-flex.w-100.justify-content-between.homepage--list-item
%p.mb-2
= image_tag planting_image_path(planting), width: 75, class: 'rounded shadow'
.text-right
%h5= planting.crop.name
- if planting.planted_from.present?
%span.badge.badge-success= planting.planted_from.pluralize
%small.text-muted planted by #{planting.owner}
- Planting.homepage_records(6).each do |planting|
= link_to planting, class: 'list-group-item list-group-item-action flex-column align-items-start' do
.d-flex.w-100.justify-content-between.homepage--list-item
%p.mb-2
= image_tag planting['thumbnail_url'], width: 75, class: 'rounded shadow'
.text-right
%h5= planting['crop_name']
- if planting['planted_from'].present?
%span.badge.badge-success= planting['planted_from'].pluralize
%small.text-muted planted by #{planting['owner_name']}

View File

@@ -1,6 +1,17 @@
class AddHarvestCountToCrop < ActiveRecord::Migration[5.2]
def change
add_column :crops, :harvests_count, :integer
add_column :crops, :photo_associations_count, :integer
add_column :plant_parts, :harvests_count, :integer
Crop.all.each do |crop|
Crop.reset_counters(crop.id, :harvests)
Crop.reset_counters(crop.id, :photos)
say "Crop #{crop.name} counter caches updated"
end
PlantPart.all.each do |pp|
PlantPart.reset_counters(pp.id, :harvests)
say "PlantPart #{pp.name} counter caches updated"
end
end
end

View File

@@ -197,6 +197,7 @@ ActiveRecord::Schema.define(version: 2019_12_09_202348) do
t.integer "median_days_to_last_harvest"
t.jsonb "openfarm_data"
t.integer "harvests_count"
t.integer "photo_associations_count"
t.index ["name"], name: "index_crops_on_name"
t.index ["requester_id"], name: "index_crops_on_requester_id"
t.index ["slug"], name: "index_crops_on_slug", unique: true