Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 11 additions & 19 deletions app/controllers/photos_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ class PhotosController < ApplicationController
responders :flash

def index
@photos = Photo.search(
load: false,
boost_by: [:created_at],
where: index_where_clause,
page: params[:page],
limit: Photo.per_page
)
@photos = if @crop
@crop.photos
elsif @planting
@planting.photos
else
Photo.all
end

@photos = @photos.includes(:owner)
.order(created_at: :desc)
.paginate(page: params[:page], per_page: Photo.per_page)
respond_with(@photos)
end

Expand Down Expand Up @@ -114,18 +118,6 @@ def retrieve_from_flickr
end
end

def index_where_clause
if params[:crop_slug]
{ crops: @crop.id }
elsif params[:planting_id]
{ planting_id: @planting.id }
elsif params[:planting_slug]
{ plantings: @planting.id }
else
{}
end
end

def set_crop_and_planting
@crop = Crop.find params[:crop_slug] if params[:crop_slug]
@planting = Planting.find params[:planting_id] if params[:planting_id]
Expand Down
37 changes: 0 additions & 37 deletions app/models/concerns/search_photos.rb

This file was deleted.

1 change: 0 additions & 1 deletion app/models/photo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
class Photo < ApplicationRecord
include Likeable
include Ownable
include SearchPhotos

PHOTO_CAPABLE = %w(Garden Planting Harvest Seed Post Crop).freeze

Expand Down
20 changes: 7 additions & 13 deletions spec/controllers/photos_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@

require 'rails_helper'

describe PhotosController, :search do
describe PhotosController do
login_member

describe 'GET index' do
describe 'all photos' do
let!(:photo) { create(:photo, :reindex) }
let!(:photo) { create(:photo) }

before do
Photo.reindex
get :index
end

Expand All @@ -21,28 +20,23 @@
end

describe '#index crop photos' do
let!(:photo) { create(:photo, :reindex, owner: member, title: 'no assocations photo') }
let!(:crop_photo) { create(:photo, :reindex, owner: member, title: 'photos of planting') }
let!(:planting) { create(:planting, :reindex, crop:, owner: member) }
let!(:crop) { create(:crop, :reindex) }
let!(:photo) { create(:photo, owner: member, title: 'no assocations photo') }
let!(:crop_photo) { create(:photo, owner: member, title: 'photos of planting') }
let!(:crop) { create(:crop) }
let!(:planting) { create(:planting, crop:, owner: member) }

before do
planting.photos << crop_photo
Photo.reindex
get :index, params: { crop_slug: crop.to_param }
end

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.crops).to include crop }
it { expect(assigns(:photos).first.id).to eq crop_photo.id }
end
end
Expand Down
5 changes: 0 additions & 5 deletions spec/factories/photos.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,5 @@
license_url { nil }
end

trait :reindex do
after(:create) do |photo, _evaluator|
photo.reindex(refresh: true)
end
end
end
end
34 changes: 1 addition & 33 deletions spec/models/photo_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require 'rails_helper'

describe Photo do
let(:photo) { create(:photo, :reindex, owner: member) }
let(:photo) { create(:photo, owner: member) }
let(:member) { create(:member) }

it_behaves_like "it is likeable"
Expand Down Expand Up @@ -237,36 +237,4 @@
end
end

describe 'Elastic search indexing', :search do
let!(:planting) { create(:planting, :reindex, owner: photo.owner) }
let!(:crop) { create(:crop, :reindex) }

before do
planting.photos << photo
described_class.reindex
described_class.searchkick_index.refresh
end

describe "finds all photos in search index" do
it "finds just one" do
expect(described_class.search.count).to eq 1
end

it "finds the matching photo" do
expect(described_class.search).to include photo
end

it "retrieves crops from ES" do
expect(described_class.search(load: false).first.crops).to eq [planting.crop.id]
end
end

it "finds photos by owner in search index" do
expect(described_class.search(where: { owner_id: planting.owner_id })).to include photo
end

it "finds photos by crop in search index" do
expect(described_class.search(where: { crops: planting.crop.id })).to include photo
end
end
end
Loading