Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

feat(core): rename Collection rules association and grant collection access

Łukasz Adamczyke91eb4b2af38
diff --git a/spree/core/app/models/spree/api_key.rb b/spree/core/app/models/spree/api_key.rb
index 7d529f99f6..0fcb8ae63e 100644
--- a/spree/core/app/models/spree/api_key.rb
+++ b/spree/core/app/models/spree/api_key.rb
@@ -21,6 +21,7 @@ module Spree
read_store_credits write_store_credits
read_stock write_stock
read_categories write_categories
+ read_collections write_collections
read_settings write_settings
read_webhooks write_webhooks
read_api_keys write_api_keys
diff --git a/spree/core/app/models/spree/collection.rb b/spree/core/app/models/spree/collection.rb
index 3948b5b606..7d8e924df7 100644
--- a/spree/core/app/models/spree/collection.rb
+++ b/spree/core/app/models/spree/collection.rb
@@ -46,11 +46,10 @@ module Spree
has_many :product_collections, class_name: 'Spree::ProductCollection', dependent: :destroy_async, inverse_of: :collection
has_many :products, through: :product_collections
- has_many :collection_rules, class_name: 'Spree::CollectionRule', dependent: :destroy, inverse_of: :collection
- accepts_nested_attributes_for :collection_rules, allow_destroy: true, reject_if: proc { |attributes|
+ has_many :rules, class_name: 'Spree::CollectionRule', dependent: :destroy, inverse_of: :collection
+ accepts_nested_attributes_for :rules, allow_destroy: true, reject_if: proc { |attributes|
attributes['value'].blank?
}
- alias rules collection_rules
#
# Attachments
@@ -141,6 +140,44 @@ module Spree
products
end
+ # Syncs automatic rules from an array of attribute hashes by mutating the
+ # in-memory `rules` association: hashes with an id update the matching rule
+ # (accepting the prefixed `crule_` id), hashes without an id build a new
+ # rule (the STI `type` selects the subclass), and any existing rule absent
+ # from the payload is destroyed. Autosave persists the whole set in the
+ # parent's save transaction. Mirrors Spree::OptionType#option_values= so the
+ # Admin API can send the full desired rule set and have it round-trip.
+ #
+ # Falls back to the association writer when given CollectionRule records
+ # (non-API callers / accepts_nested_attributes_for).
+ #
+ # @param rules_params [Array<Hash>] array of rule attribute hashes
+ # @return [void]
+ def rules=(rules_params)
+ return super if rules_params.blank? || rules_params.first.is_a?(Spree::CollectionRule)
+
+ existing_by_id = rules.to_a.index_by(&:id)
+ retained_ids = []
+
+ rules_params.each do |rule_data|
+ data = rule_data.to_h.with_indifferent_access
+ rule_id = data.delete(:id)
+
+ record = if rule_id.present?
+ existing_by_id[Spree::PrefixedId.decode_prefixed_id(rule_id) || rule_id] ||
+ raise(ActiveRecord::RecordNotFound.new("Couldn't find Spree::CollectionRule with param=#{rule_id}", 'Spree::CollectionRule'))
+ else
+ rules.build(data)
+ end
+ record.assign_attributes(data) if rule_id.present?
+ retained_ids << record.id if record.persisted?
+ end
+
+ existing_by_id.each_value do |existing|
+ existing.mark_for_destruction unless retained_ids.include?(existing.id)
+ end
+ end
+
# Slug generation, flat (no parent hierarchy). Mirrors Spree::Taxon's dual set_permalink:
# with translations off, the model writes the base permalink column directly (which
# column_fallback routes the default locale to, bypassing the Translation before_save);
diff --git a/spree/core/app/models/spree/collection_rule.rb b/spree/core/app/models/spree/collection_rule.rb
index e864717fcb..53478837cc 100644
--- a/spree/core/app/models/spree/collection_rule.rb
+++ b/spree/core/app/models/spree/collection_rule.rb
@@ -6,7 +6,7 @@ module Spree
MATCH_POLICIES = %w[is_equal_to is_not_equal_to contains does_not_contain].freeze
- belongs_to :collection, class_name: 'Spree::Collection', inverse_of: :collection_rules, touch: true
+ belongs_to :collection, class_name: 'Spree::Collection', inverse_of: :rules, touch: true
validates :collection, :type, :value, presence: true
validates :match_policy, inclusion: { in: MATCH_POLICIES }, presence: true
diff --git a/spree/core/app/models/spree/permission_sets/product_management.rb b/spree/core/app/models/spree/permission_sets/product_management.rb
index 715b99adca..6a63350148 100644
--- a/spree/core/app/models/spree/permission_sets/product_management.rb
+++ b/spree/core/app/models/spree/permission_sets/product_management.rb
@@ -17,6 +17,9 @@ module Spree
can :manage, Spree::Taxon
can :manage, Spree::Taxonomy
can :manage, Spree::Classification
+ can :manage, Spree::Collection
+ can :manage, Spree::ProductCollection
+ can :manage, Spree::CollectionRule
can :manage, Spree::Price
can :manage, Spree::PriceList
can :manage, Spree::PriceRule
diff --git a/spree/core/app/services/spree/products/auto_match_collections.rb b/spree/core/app/services/spree/products/auto_match_collections.rb
index 9683a04090..a16ab86225 100644
--- a/spree/core/app/services/spree/products/auto_match_collections.rb
+++ b/spree/core/app/services/spree/products/auto_match_collections.rb
@@ -15,7 +15,7 @@ module Spree
collections_to_add = []
# existing memberships that no longer match (tags/prices changed)
- product.collections.automatic.includes(:collection_rules).each do |collection|
+ product.collections.automatic.includes(:rules).each do |collection|
collections_to_remove << collection unless collection.products_matching_rules.ids.include?(product.id)
end
@@ -24,7 +24,7 @@ module Spree
end
# automatic collections in the product's store that it now matches
- Spree::Collection.automatic.where(store_id: product.store_id).includes(:collection_rules, :products).each do |collection|
+ Spree::Collection.automatic.where(store_id: product.store_id).includes(:rules, :products).each do |collection|
collections_to_add << collection if collection.products.exclude?(product) && collection.products_matching_rules.ids.include?(product.id)
end