Skip to content

Commit

Permalink
Merge pull request #4956 from nebulab/ryanofwoods/move-ransackable-sc…
Browse files Browse the repository at this point in the history
…opes

Introduce allowed_ransackable_scopes
  • Loading branch information
kennyadsl authored Mar 2, 2023
2 parents aa3063e + a68e768 commit c09ead3
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
5 changes: 5 additions & 0 deletions core/app/models/concerns/spree/ransackable_attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Spree::RansackableAttributes
included do
class_attribute :allowed_ransackable_associations, default: []
class_attribute :allowed_ransackable_attributes, default: []
class_attribute :allowed_ransackable_scopes, default: []

def self.whitelisted_ransackable_associations
Spree::Deprecation.deprecation_warning(:whitelisted_ransackable_associations, 'use allowed_ransackable_associations instead')
Expand Down Expand Up @@ -38,5 +39,9 @@ def ransackable_associations(*_args)
def ransackable_attributes(*_args)
default_ransackable_attributes | allowed_ransackable_attributes
end

def ransackable_scopes(*_args)
allowed_ransackable_scopes
end
end
end
5 changes: 1 addition & 4 deletions core/app/models/spree/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,7 @@ def find_or_build_master

self.allowed_ransackable_associations = %w[stores variants_including_master master variants]
self.allowed_ransackable_attributes = %w[name slug]

def self.ransackable_scopes(_auth_object = nil)
%i(available with_discarded with_variant_sku_cont with_all_variant_sku_cont with_kept_variant_sku_cont)
end
self.allowed_ransackable_scopes = %i[available with_discarded with_variant_sku_cont with_all_variant_sku_cont with_kept_variant_sku_cont]

# @return [Boolean] true if there are any variants
def has_variants?
Expand Down
4 changes: 1 addition & 3 deletions core/app/models/spree/promotion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ class Promotion < Spree::Base

self.allowed_ransackable_associations = ['codes']
self.allowed_ransackable_attributes = %w[name path promotion_category_id]
def self.ransackable_scopes(*)
%i(active)
end
self.allowed_ransackable_scopes = %i[active]

def self.order_activatable?(order)
order && !UNACTIVATABLE_ORDER_STATES.include?(order.state)
Expand Down
20 changes: 20 additions & 0 deletions core/spec/models/spree/concerns/ransackable_attributes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,25 @@
expect(test_class.ransackable_attributes).to match_array(["id", "test", "new_value"])
end
end

context "allowed_ransackable_scopes" do
before do
test_class.allowed_ransackable_scopes = []
end

it 'reads' do
expect(test_class.allowed_ransackable_scopes).to be_empty
end

it 'allows setting an array' do
test_class.allowed_ransackable_scopes = [:test]
expect(test_class.allowed_ransackable_scopes).to match_array([:test])
end

it 'allows concatenating' do
test_class.allowed_ransackable_scopes.concat([:new_value])
expect(test_class.allowed_ransackable_scopes).to match_array([:new_value])
end
end
end
end

0 comments on commit c09ead3

Please sign in to comment.