mirror of
https://github.com/Growstuff/growstuff.git
synced 2025-12-24 10:07:47 -05:00
fixing photos by crop query in photos controller
This commit is contained in:
@@ -16,7 +16,7 @@ class PhotosController < ApplicationController
|
||||
where = {}
|
||||
if params[:crop_slug]
|
||||
@crop = Crop.find params[:crop_slug]
|
||||
where = { crop_id: @crop.id }
|
||||
where = { crops: [@crop.id] }
|
||||
elsif params[:planting_id]
|
||||
@planting = Planting.find params[:planting_id]
|
||||
where = { planting_id: @planting.id }
|
||||
|
||||
@@ -12,12 +12,13 @@ module SearchPhotos
|
||||
}
|
||||
}
|
||||
|
||||
# scope :search_import, -> { includes(:owner, :crops, :plantings, :harvests, :seeds, :posts) }
|
||||
scope :search_import, -> { includes(:owner, :crops, :plantings, :harvests, :seeds, :posts) }
|
||||
|
||||
def search_data
|
||||
{
|
||||
id: id,
|
||||
title: title,
|
||||
crops: crops.map(&:id),
|
||||
crops: photo_associations.map(&:crop_id),
|
||||
owner_id: owner_id,
|
||||
owner_login_name: owner.login_name,
|
||||
thumbnail_url: thumbnail_url,
|
||||
|
||||
@@ -9,29 +9,49 @@ describe PhotosController, :search do
|
||||
describe 'all photos' do
|
||||
let!(:photo) { FactoryBot.create :photo }
|
||||
|
||||
before { get :index }
|
||||
before do
|
||||
Photo.searchkick_index.refresh
|
||||
photo.reindex
|
||||
sleep 1
|
||||
get :index
|
||||
end
|
||||
|
||||
it { expect(assigns(:photos)).to eq [photo] }
|
||||
it "finds photos" do
|
||||
expect(assigns(:photos).size).to eq 1
|
||||
expect(assigns(:photos).first.id).to eq photo.id
|
||||
end
|
||||
end
|
||||
|
||||
describe 'crop photos' do
|
||||
let!(:photo) { FactoryBot.create :photo, owner: member }
|
||||
let!(:crop_photo) { FactoryBot.create :photo, owner: member }
|
||||
let!(:photo) { FactoryBot.create :photo, owner: member, title: 'no assocations photo' }
|
||||
let!(:crop_photo) { FactoryBot.create :photo, owner: member, title: 'photos of planting' }
|
||||
let!(:planting) { FactoryBot.create :planting, crop: crop, owner: member }
|
||||
let!(:crop) { FactoryBot.create :crop }
|
||||
|
||||
before do
|
||||
planting.photos << crop_photo
|
||||
Photo.searchkick_index.refresh
|
||||
crop_photo.reload
|
||||
crop_photo.reindex
|
||||
# This is terrible, but this is needed for ES to fully index this
|
||||
sleep 1
|
||||
|
||||
# all these tests are inline, so we only sleep once
|
||||
get :index, params: { crop_slug: crop.to_param }
|
||||
end
|
||||
|
||||
it { expect(assigns(:crop)).to eq crop }
|
||||
it { expect(assigns(:photos)).to eq [crop_photo] }
|
||||
it "find photos by crop" do
|
||||
expect(Photo.search).to include crop_photo
|
||||
expect(assigns(:crop)).to eq crop
|
||||
expect(assigns(:photos).size).to eq 1
|
||||
expect(assigns(:photos).first.crops).to include crop.id
|
||||
expect(assigns(:photos).first.id).to eq crop_photo.id
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "GET new" do
|
||||
let(:tomato) { FactoryBot.create(:tomato) }
|
||||
let(:tomato) { FactoryBot.create(:tomato) }
|
||||
let(:planting) { FactoryBot.create(:planting, crop: tomato, owner: member) }
|
||||
let(:garden) { FactoryBot.create(:garden, owner: member) }
|
||||
let(:harvest) { FactoryBot.create(:harvest, owner: member) }
|
||||
|
||||
Reference in New Issue
Block a user