Skip to content

Commit

Permalink
[NX-7] Filter taxons by taxonomies' permalinks
Browse files Browse the repository at this point in the history
  • Loading branch information
Piotr Milewski committed Aug 25, 2021
1 parent 42acab1 commit 7851158
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
12 changes: 12 additions & 0 deletions api/spec/requests/spree/api/v2/storefront/taxons_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@
end
end

context 'by parent permalink' do
before { get "/api/v2/storefront/taxons?filter[parent_permalink]=#{taxonomy.root.permalink}" }

it_behaves_like 'returns 200 HTTP status'

it 'returns children taxons by parent' do
expect(json_response['data'].size).to eq(2)
expect(json_response['data'][0]).to have_relationship(:parent).with_data('id' => taxonomy.root.id.to_s, 'type' => 'taxon')
expect(json_response['data'][1]).to have_relationship(:parent).with_data('id' => taxonomy.root.id.to_s, 'type' => 'taxon')
end
end

context 'by taxonomy' do
before { get "/api/v2/storefront/taxons?taxonomy_id=#{taxonomy.id}" }

Expand Down
24 changes: 18 additions & 6 deletions core/app/finders/spree/taxons/find.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@ module Taxons
class Find
def initialize(scope:, params:)
@scope = scope
@ids = String(params.dig(:filter, :ids)).split(',')
@parent = params.dig(:filter, :parent_id)
@taxonomy = params.dig(:filter, :taxonomy_id)
@name = params.dig(:filter, :name)
@roots = params.dig(:filter, :roots)
@ids = String(params.dig(:filter, :ids)).split(',')
@parent = params.dig(:filter, :parent_id)
@parent_permalink = params.dig(:filter, :parent_permalink)
@taxonomy = params.dig(:filter, :taxonomy_id)
@name = params.dig(:filter, :name)
@roots = params.dig(:filter, :roots)
end

def execute
taxons = by_ids(scope)
taxons = by_parent(taxons)
taxons = by_parent_permalink(taxons)
taxons = by_taxonomy(taxons)
taxons = by_roots(taxons)
taxons = by_name(taxons)
Expand All @@ -22,7 +24,7 @@ def execute

private

attr_reader :ids, :parent, :taxonomy, :roots, :name, :scope
attr_reader :ids, :parent, :parent_permalink, :taxonomy, :roots, :name, :scope

def ids?
ids.present?
Expand All @@ -32,6 +34,10 @@ def parent?
parent.present?
end

def parent_permalink?
parent_permalink.present?
end

def taxonomy?
taxonomy.present?
end
Expand Down Expand Up @@ -60,6 +66,12 @@ def by_parent(taxons)
taxons.where(parent_id: parent)
end

def by_parent_permalink(taxons)
return taxons unless parent_permalink?

taxons.where(parent: { permalink: parent_permalink })
end

def by_taxonomy(taxons)
return taxons unless taxonomy?

Expand Down
2 changes: 1 addition & 1 deletion core/lib/spree/permitted_attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ module PermittedAttributes
@@taxonomy_attributes = [:name]

@@taxon_attributes = [
:name, :parent_id, :position, :icon, :description, :permalink, :hide_from_nav,
:name, :parent_id, :parent_permalink, :position, :icon, :description, :permalink, :hide_from_nav,
:taxonomy_id, :meta_description, :meta_keywords, :meta_title, :child_index
]

Expand Down

0 comments on commit 7851158

Please sign in to comment.