Skip to content

Commit

Permalink
Merge pull request #5853 from tvdeyen/ransack-4.2
Browse files Browse the repository at this point in the history
Add Ransack 4.2 support
  • Loading branch information
tvdeyen authored Sep 13, 2024
2 parents a791a5f + ae69534 commit 0f77e8b
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def filters
{
label: option_type.presentation,
combinator: 'or',
attribute: "variants_option_values",
attribute: "option_values_id",
predicate: "in",
options: option_type.option_values.pluck(:name, :id),
}
Expand Down
24 changes: 4 additions & 20 deletions core/app/models/spree/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ class Product < Spree::Base
has_many :line_items, through: :variants_including_master
has_many :orders, through: :line_items

has_many :option_values, -> { distinct }, through: :variants_including_master

scope :sort_by_master_default_price_amount_asc, -> {
with_default_price.order('spree_prices.amount ASC')
}
Expand Down Expand Up @@ -127,26 +129,8 @@ def find_or_build_master

alias :options :product_option_types

# The :variants_option_values ransacker filters Spree::Product based on
# variant option values ids.
#
# Usage:
# Spree::Product.ransack(
# variants_option_values_in: [option_value_id1, option_value_id2]
# ).result
ransacker :variants_option_values, formatter: proc { |v|
if OptionValue.exists?(v)
joins(variants_including_master: :option_values)
.where(spree_option_values: { id: v })
.distinct
.select(:id).arel
end
} do |parent|
parent.table[:id]
end

self.allowed_ransackable_associations = %w[stores variants_including_master master variants]
self.allowed_ransackable_attributes = %w[name slug variants_option_values]
self.allowed_ransackable_associations = %w[stores variants_including_master master variants option_values]
self.allowed_ransackable_attributes = %w[name slug]
self.allowed_ransackable_scopes = %i[available with_discarded with_all_variant_sku_cont with_kept_variant_sku_cont]

# @return [Boolean] true if there are any variants
Expand Down
2 changes: 0 additions & 2 deletions core/lib/spree/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
require 'ransack'
require 'state_machines-activerecord'

require_relative './ransack_4_1_patch'

# This is required because ActiveModel::Validations#invalid? conflicts with the
# invalid state of a Payment. In the future this should be removed.
StateMachines::Machine.ignore_method_conflicts = true
Expand Down
16 changes: 0 additions & 16 deletions core/lib/spree/ransack_4_1_patch.rb

This file was deleted.

4 changes: 1 addition & 3 deletions core/solidus_core.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ Gem::Specification.new do |s|
s.add_dependency 'monetize', '~> 1.8'
s.add_dependency 'kt-paperclip', ['>= 6.3', '< 8']
s.add_dependency 'psych', ['>= 4.0.1', '< 6.0']
# @note ransack 4.2 contains a bug which has not yet been addressed.
# @see https://github.com/activerecord-hackery/ransack/pull/1468
s.add_dependency 'ransack', ['~> 4.0', '< 4.2']
s.add_dependency 'ransack', ['~> 4.0', '< 5']
s.add_dependency 'sprockets-rails', '!= 3.5.0'
s.add_dependency 'state_machines-activerecord', '~> 0.6'
s.add_dependency 'omnes', '~> 0.2.2'
Expand Down
12 changes: 6 additions & 6 deletions core/spec/models/spree/product_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -627,13 +627,13 @@ class Extension < Spree::Base
end
end

context "ransacker :variants_option_values" do
describe "ransack :option_values_id_in" do
it "filters products based on option values of their variants" do
product_1 = create(:product)
option_value_1 = create(:option_value)
create(:variant, product: product_1, option_values: [option_value_1])

result = Spree::Product.ransack(variants_option_values_in: [option_value_1.id]).result
result = Spree::Product.ransack(option_values_id_in: [option_value_1.id]).result
expect(result).to contain_exactly(product_1)
end

Expand All @@ -644,13 +644,13 @@ class Extension < Spree::Base
create(:variant, product: product_1, option_values: [option_value_1])
create(:variant, product: product_2, option_values: [option_value_1])

result = Spree::Product.ransack(variants_option_values_in: [option_value_1.id]).result
result = Spree::Product.ransack(option_values_id_in: [option_value_1.id]).result
expect(result).to contain_exactly(product_1, product_2)
end

it "returns no products if there is no match" do
non_existing_option_value_id = 99999
result = Spree::Product.ransack(variants_option_values_in: [non_existing_option_value_id]).result
result = Spree::Product.ransack(option_values_id_in: [non_existing_option_value_id]).result
expect(result).to be_empty
end

Expand All @@ -662,7 +662,7 @@ class Extension < Spree::Base
create(:variant, product: product_1, option_values: [option_value_1])
create(:variant, product: product_2, option_values: [option_value_2])

result = Spree::Product.ransack(variants_option_values_in: [option_value_1.id, option_value_2.id]).result
result = Spree::Product.ransack(option_values_id_in: [option_value_1.id, option_value_2.id]).result
expect(result).to contain_exactly(product_1, product_2)
end

Expand All @@ -674,7 +674,7 @@ class Extension < Spree::Base
create(:variant, product: product_1, option_values: [option_value_1])
create(:variant, product: product_2, option_values: [option_value_2])

result = Spree::Product.ransack(variants_option_values_in: [option_value_1.id]).result
result = Spree::Product.ransack(option_values_id_in: [option_value_1.id]).result
expect(result).not_to include(product_2)
end
end
Expand Down

0 comments on commit 0f77e8b

Please sign in to comment.