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
17 changes: 8 additions & 9 deletions app/controllers/harvests_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,26 @@ class HarvestsController < DataController
after_action :update_crop_medians, only: %i(create update destroy)

def index
where = {}
@harvests = Harvest.all

if params[:member_slug].present?
@owner = Member.find_by!(slug: params[:member_slug])
where['owner_id'] = @owner.id
@harvests = @harvests.where(owner_id: @owner.id)
end

if params[:crop_slug]
@crop = Crop.find_by(slug: params[:crop_slug])
where['crop_id'] = @crop.id
@harvests = @harvests.where(crop_id: @crop.id) if @crop
end

if params[:planting_slug]
@planting = Planting.find_by(slug: params[:planting_slug])
where['planting_id'] = @planting.id
@harvests = @harvests.where(planting_id: @planting.id) if @planting
end

@harvests = Harvest.search('*', where:,
limit: 100,
page: params[:page],
load: (request.format.csv? ? { include: %i(crop owner plant_part) } : false),
boost_by: [:created_at])
@harvests = @harvests.includes(:crop, :owner, :plant_part)

@harvests = @harvests.recent.paginate(page: params[:page], per_page: 100)

@filename = csv_filename

Expand Down
9 changes: 3 additions & 6 deletions app/controllers/members_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,9 @@ def show
end
end

@harvests = Harvest.search(
where: { owner_id: @member.id },
boost_by: [:created_at],
limit: 16,
load: false
)
@harvests = Harvest.where(owner_id: @member.id)
.recent
.limit(16)

respond_to do |format|
format.html # show.html.haml
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/plantings_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def index

def show
@photos = @planting.photos.includes(:owner).order(date_taken: :desc)
@harvests = Harvest.search(where: { planting_id: @planting.id })
@harvests = Harvest.where(planting_id: @planting.id).recent
@current_activities = @planting.activities.current.includes(:owner).order(created_at: :desc)
@finished_activities = @planting.activities.finished.includes(:owner).order(created_at: :desc)
@matching_seeds = matching_seeds
Expand Down
74 changes: 0 additions & 74 deletions app/models/concerns/search_harvests.rb

This file was deleted.

5 changes: 4 additions & 1 deletion app/models/harvest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ class Harvest < ApplicationRecord
extend FriendlyId
include PhotoCapable
include Ownable
include SearchHarvests
include Likeable

attr_accessor :overall_rating
Expand Down Expand Up @@ -156,6 +155,10 @@ def crop_name_to_human
end
end

def self.homepage_records(limit)
recent.one_per_owner.limit(limit)
end

private

def crop_must_match_planting
Expand Down
2 changes: 0 additions & 2 deletions db/migrate/20191226051019_elastic_indexing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ def up
Planting.reindex
say 'indexing seeds'
Seed.reindex
say 'indexing harvests'
Harvest.reindex
say 'indexing photos'
Photo.reindex
end
Expand Down
1 change: 0 additions & 1 deletion lib/tasks/search.rake
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ namespace :search do
task reindex: :environment do
Crop.reindex
Planting.reindex
Harvest.reindex
Seed.reindex
end
end
8 changes: 3 additions & 5 deletions spec/controllers/harvests_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require 'rails_helper'

describe HarvestsController, :search do
describe HarvestsController do
login_member

def valid_attributes
Expand All @@ -22,14 +22,12 @@ def valid_attributes
let!(:tomato_harvest) { create(:harvest, owner_id: first_member.id, crop_id: tomato.id) }
let!(:maize_harvest) { create(:harvest, owner_id: second_member.id, crop_id: maize.id) }

before { Harvest.reindex }

describe "assigns all harvests as @harvests" do
before { get :index, params: {} }

it { expect(assigns(:harvests).size).to eq 2 }
it { expect(assigns(:harvests)[0].slug).to eq tomato_harvest.slug }
it { expect(assigns(:harvests)[1].slug).to eq maize_harvest.slug }
it { expect(assigns(:harvests)).to include(tomato_harvest) }
it { expect(assigns(:harvests)).to include(maize_harvest) }
end

describe "picks up owner from params and shows owner's harvests only" do
Expand Down
3 changes: 0 additions & 3 deletions spec/factories/harvests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,5 @@
end

trait :reindex do
after(:create) do |harvest, _evaluator|
harvest.reindex(refresh: true)
end
end
end
8 changes: 3 additions & 5 deletions spec/features/harvests/browse_harvests_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require 'rails_helper'

describe "browse harvests", :search do
describe "browse harvests" do
subject { page }

let!(:harvest) { create(:harvest, owner: member) }
Expand All @@ -11,10 +11,9 @@
include_context 'signed in member'

describe 'blank optional fields' do
let!(:harvest) { create(:harvest, :no_description, :reindex) }
let!(:harvest) { create(:harvest, :no_description) }

before do
Harvest.reindex
visit harvests_path
end

Expand All @@ -24,10 +23,9 @@
end

describe "filled in optional fields" do
let!(:harvest) { create(:harvest, :long_description, :reindex) }
let!(:harvest) { create(:harvest, :long_description) }

before do
Harvest.reindex
visit harvests_path
end

Expand Down
3 changes: 1 addition & 2 deletions spec/features/home/home_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require 'rails_helper'

describe "home page", :search do
describe "home page" do
subject { page }

let(:member) { create(:member) }
Expand All @@ -27,7 +27,6 @@
Crop.reindex
Planting.reindex
Seed.reindex
Harvest.reindex

visit root_path
end
Expand Down
13 changes: 13 additions & 0 deletions spec/models/harvest_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,19 @@
end
end

describe '.homepage_records' do
it 'returns unique harvests per owner' do
member1 = create(:member)
member2 = create(:member)
create(:harvest, owner: member1, created_at: 1.day.ago)
h2 = create(:harvest, owner: member1, created_at: 1.hour.ago)
h3 = create(:harvest, owner: member2, created_at: 2.hours.ago)

records = described_class.homepage_records(5)
expect(records).to contain_exactly(h2, h3)
end
end

context "stringification" do
let(:crop) { create(:crop, name: "apricot") }

Expand Down
1 change: 0 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
def index_everything
# reindex models
Crop.reindex
Harvest.reindex
Planting.reindex
Seed.reindex
end
Expand Down
5 changes: 2 additions & 3 deletions spec/views/harvests/index.rss.haml_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require 'rails_helper'

describe 'harvests/index.rss.haml', :search do
describe 'harvests/index.rss.haml' do
before do
controller.stub(:current_user) { nil }
@member = create(:member)
Expand All @@ -12,8 +12,7 @@
@harvest2 = create(:harvest, crop: @tomato)
@harvest3 = create(:harvest, crop: @tomato)

Harvest.searchkick_index.refresh
assign(:harvests, Harvest.search(load: false))
assign(:harvests, Harvest.all)
end

context 'all harvests' do
Expand Down
Loading