Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix N+1 problem on Api::TaxonsController#index #3011

Merged
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
15 changes: 15 additions & 0 deletions api/app/controllers/spree/api/taxons_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ def index
@taxons = Spree::Taxon.accessible_by(current_ability, :read).order(:taxonomy_id, :lft).ransack(params[:q]).result
end

unless params[:without_children]
@taxons = @taxons.includes(:children)
stem marked this conversation as resolved.
Show resolved Hide resolved
end

@taxons = paginate(@taxons)
preload_taxon_parents(@taxons)
respond_with(@taxons)
end

Expand Down Expand Up @@ -106,6 +111,16 @@ def taxon_params
{}
end
end

def preload_taxon_parents(taxons)
parents = Spree::Taxon.none

taxons.map do |taxon|
parents = parents.or(taxon.ancestors)
end

Spree::Taxon.associate_parents(taxons + parents)
stem marked this conversation as resolved.
Show resolved Hide resolved
end
end
end
end
28 changes: 22 additions & 6 deletions api/spec/requests/spree/api/taxons_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@

module Spree
describe Api::TaxonsController, type: :request do
let(:taxonomy) { create(:taxonomy) }
let(:taxon) { create(:taxon, name: "Ruby", taxonomy: taxonomy) }
let(:taxon2) { create(:taxon, name: "Rails", taxonomy: taxonomy) }
let!(:taxonomy) { create(:taxonomy) }
let!(:taxon) { create(:taxon, name: "Ruby", parent: taxonomy.root, taxonomy: taxonomy) }
let!(:taxon2) { create(:taxon, name: "Rails", parent: taxon, taxonomy: taxonomy) }
let!(:rails_v3_2_2) { create(:taxon, name: "3.2.2", parent: taxon2, taxonomy: taxonomy) }
let(:attributes) { ["id", "name", "pretty_name", "permalink", "parent_id", "taxonomy_id"] }

before do
stub_authentication!
taxon2.children << create(:taxon, name: "3.2.2", taxonomy: taxonomy)
taxon.children << taxon2
taxonomy.root.children << taxon
end

context "as a normal user" do
Expand Down Expand Up @@ -85,6 +83,24 @@ module Spree
end
end

context 'filtering by taxon ids' do
it 'returns only requested id' do
get spree.api_taxons_path, params: { ids: [rails_v3_2_2.id] }

expect(json_response['taxons'].size).to eq 1
end

it 'returns only requested ids' do
# We need a completly new branch to avoid having parent that can be preloaded from the rails ancestors
python = create(:taxon, name: "Python", parent: taxonomy.root, taxonomy: taxonomy)
python_3 = create(:taxon, name: "3.0", parent: python, taxonomy: taxonomy)

get spree.api_taxons_path, params: { ids: [rails_v3_2_2.id, python_3.id] }

expect(json_response['taxons'].size).to eq 2
end
end

it "gets a single taxon" do
get spree.api_taxonomy_taxon_path(taxonomy, taxon.id)

Expand Down
11 changes: 8 additions & 3 deletions core/app/models/spree/taxon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,14 @@ def all_variants
# @return [String] this taxon's ancestors names followed by its own name,
# separated by arrows
def pretty_name
ancestor_chain = ancestors.map(&:name)
ancestor_chain << name
ancestor_chain.join(" -> ")
if parent.present?
[
parent.pretty_name,
name
].compact.join(" -> ")
else
name
end
end

# @see https://github.com/spree/spree/issues/3390
Expand Down