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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
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.rbindex 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 Spreeread_store_credits write_store_creditsread_stock write_stockread_categories write_categories+ read_collections write_collectionsread_settings write_settingsread_webhooks write_webhooksread_api_keys write_api_keysdiff --git a/spree/core/app/models/spree/collection.rb b/spree/core/app/models/spree/collection.rbindex 3948b5b606..7d8e924df7 100644--- a/spree/core/app/models/spree/collection.rb+++ b/spree/core/app/models/spree/collection.rb@@ -46,11 +46,10 @@ module Spreehas_many :product_collections, class_name: 'Spree::ProductCollection', dependent: :destroy_async, inverse_of: :collectionhas_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 Spreeproductsend+ # 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.rbindex 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 SpreeMATCH_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: truevalidates :collection, :type, :value, presence: truevalidates :match_policy, inclusion: { in: MATCH_POLICIES }, presence: truediff --git a/spree/core/app/models/spree/permission_sets/product_management.rb b/spree/core/app/models/spree/permission_sets/product_management.rbindex 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 Spreecan :manage, Spree::Taxoncan :manage, Spree::Taxonomycan :manage, Spree::Classification+ can :manage, Spree::Collection+ can :manage, Spree::ProductCollection+ can :manage, Spree::CollectionRulecan :manage, Spree::Pricecan :manage, Spree::PriceListcan :manage, Spree::PriceRulediff --git a/spree/core/app/services/spree/products/auto_match_collections.rb b/spree/core/app/services/spree/products/auto_match_collections.rbindex 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 Spreecollections_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 Spreeend# 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