diff --git a/app/controllers/photos_controller.rb b/app/controllers/photos_controller.rb index ad64d58f5..322c5a66e 100644 --- a/app/controllers/photos_controller.rb +++ b/app/controllers/photos_controller.rb @@ -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 diff --git a/spec/controllers/photos_controller_spec.rb b/spec/controllers/photos_controller_spec.rb index a6680d308..7d318e5f5 100644 --- a/spec/controllers/photos_controller_spec.rb +++ b/spec/controllers/photos_controller_spec.rb @@ -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) }