Skip to content

Commit

Permalink
Merge pull request #519 from guitsaru/optimize-guide-index
Browse files Browse the repository at this point in the history
Eager load gardens on guide reindex.
  • Loading branch information
simonv3 committed Mar 3, 2015
2 parents 7f9d2fc + 87dda23 commit 1c6bd48
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 4 deletions.
4 changes: 3 additions & 1 deletion app/jobs/reindex_guides_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ class ReindexGuidesJob < ActiveJob::Base
queue_as :default

def perform
Guide.reindex
Guide.desc(:popularity_score).each do |guide|
guide.reindex_async
end
end
end
2 changes: 1 addition & 1 deletion app/models/guide.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def compatibilities

@compatibilities = []

User.each do |user|
User.includes(:gardens).each do |user|
@compatibilities << {
user_id: user.id.to_s, score: compatibility_score(user).to_i
}
Expand Down
4 changes: 3 additions & 1 deletion spec/jobs/reindex_guides_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

describe ReindexGuidesJob do
it 'reindexes guides' do
expect(Guide).to receive(:reindex)
FactoryGirl.create(:guide)

expect_any_instance_of(Guide).to receive(:reindex_async)

ReindexGuidesJob.new.perform
end
Expand Down
4 changes: 3 additions & 1 deletion spec/models/garden_crop_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
end

it 'reindexes guides' do
expect(Guide).to receive(:reindex)
FactoryGirl.create(:guide)

expect_any_instance_of(Guide).to receive(:reindex_async)

FactoryGirl.create(:garden, user: FactoryGirl.create(:user))
end
Expand Down

0 comments on commit 1c6bd48

Please sign in to comment.