photos controller working with ES

This commit is contained in:
Brenda Wallace
2019-12-31 21:46:42 +13:00
parent 6bc0aa7194
commit f76266a716
2 changed files with 32 additions and 33 deletions

View File

@@ -16,17 +16,19 @@ class PhotosController < ApplicationController
where = {}
if params[:crop_slug]
@crop = Crop.find params[:crop_slug]
where = { crops: [@crop.id] }
where = { crops: @crop.id }
elsif params[:planting_id]
@planting = Planting.find params[:planting_id]
where = { planting_id: @planting.id }
end
@photos = Photo.search(load: false,
boost_by: [:created_at],
where: where,
page: params[:page],
limit: 50)
@photos = Photo.search(
load: false,
boost_by: [:created_at],
where: where,
page: params[:page],
limit: Photo.per_page
)
respond_with(@photos)
end
@@ -88,7 +90,7 @@ class PhotosController < ApplicationController
def find_or_create_photo_from_flickr_photo
photo = Photo.find_or_initialize_by(
source_id: photo_params[:source_id],
source: 'flickr'
source: 'flickr'
)
photo.update(photo_params)
photo.owner_id = current_member.id

View File

@@ -7,45 +7,42 @@ describe PhotosController, :search do
describe 'GET index' do
describe 'all photos' do
let!(:photo) { FactoryBot.create :photo }
let!(:photo) { FactoryBot.create :photo, :reindex }
before do
Photo.searchkick_index.refresh
photo.reindex
sleep 1
get :index
end
it "finds photos" do
expect(assigns(:photos).size).to eq 1
expect(assigns(:photos).count).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, 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 }
describe '#index crop photos' do
let!(:photo) { FactoryBot.create :photo, :reindex, owner: member, title: 'no assocations photo' }
let!(:crop_photo) { FactoryBot.create :photo, :reindex, owner: member, title: 'photos of planting' }
let!(:planting) { FactoryBot.create :planting, :reindex, crop: crop, owner: member }
let!(:crop) { FactoryBot.create :crop, :reindex }
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 "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
describe "find photos by crop" do
it "has indexed the photos of this crop" do
expect(Photo.search).to include crop_photo
end
it "assigns crop" do
expect(assigns(:crop)).to eq crop
end
it { expect(assigns(:photos).size).to eq 1 }
it { expect(assigns(:photos).first.crops).to include crop.id }
it { expect(assigns(:photos).first.id).to eq crop_photo.id }
end
end
end
@@ -91,12 +88,12 @@ describe PhotosController, :search do
describe "POST create" do
before do
Photo.any_instance.stub(:flickr_metadata).and_return(title: "A Heartbreaking work of staggering genius",
license_name: "CC-BY",
license_url: "http://example.com/aybpl",
Photo.any_instance.stub(:flickr_metadata).and_return(title: "A Heartbreaking work of staggering genius",
license_name: "CC-BY",
license_url: "http://example.com/aybpl",
thumbnail_url: "http://example.com/thumb.jpg",
fullsize_url: "http://example.com/full.jpg",
link_url: "http://example.com")
fullsize_url: "http://example.com/full.jpg",
link_url: "http://example.com")
end
let(:member) { FactoryBot.create(:member) }