Class: Spree::Collection
- Inherits:
-
Object
- Object
- Spree::Collection
- Extended by:
- FriendlyId
- Includes:
- HasCustomFields, HasLibraryMedia, HasListPosition, Metadata, SanitizableRichText, SingleStoreResource, TranslatableResource, TranslatableResourceSlug
- Defined in:
- app/models/spree/collection.rb
Constant Summary collapse
- RULES_MATCH_POLICIES =
%w[all any].freeze
- SORT_ORDERS =
Space format (e.g. 'price asc') is required by the search sort pipeline: FiltersAggregator#to_api_sort splits on a space, and apply_sort maps to 'price'/etc.
[ 'manual', 'best_selling', 'price asc', 'price desc', 'available_on desc', 'available_on asc', 'name asc', 'name desc' ].freeze
- TRANSLATABLE_FIELDS =
%i[name description permalink].freeze
- RICH_TEXT_TRANSLATABLE_FIELDS =
%i[description].freeze
Class Method Summary collapse
-
.resolve_rule_type(type) ⇒ String?
Maps a wire
typeonto a registered rule class name.
Instance Method Summary collapse
- #generate_slug ⇒ Object
- #manual? ⇒ Boolean
- #manual_sort_order? ⇒ Boolean
-
#products_matching_rules(opts = {}) ⇒ ActiveRecord::Relation
Products matching the automatic rules (mirrors Spree::Category#products_matching_rules).
-
#regenerate_products(only_once: false) ⇒ void
Rebuild the materialized ProductCollection membership from the rules.
-
#rules=(rules_params) ⇒ void
Syncs automatic rules from an array of attribute hashes by mutating the in-memory
rulesassociation: hashes with an id update the matching rule (accepting the prefixedcrule_id), hashes without an id build a new rule (the STItypeselects the subclass), and any existing rule absent from the payload is destroyed. -
#set_permalink ⇒ Object
Slug generation, flat (no parent hierarchy).
- #slug ⇒ Object
- #slug=(value) ⇒ Object
Methods included from HasLibraryMedia
Methods included from SanitizableRichText
Methods included from Metadata
#private_metadata, #private_metadata=
Methods included from TranslatableResource
#get_field_with_locale, #translatable_store, #upsert_translations
Class Method Details
.resolve_rule_type(type) ⇒ String?
Maps a wire type onto a registered rule class name. Accepts both the
/collection_rules/types shorthand (tag) and the STI class name,
matching how every other subclassed resource is written.
Matching is string-only against the in-memory registry — nothing from the wire is ever constantized. An unregistered value resolves to nil, which leaves the record as the abstract base class so it fails CollectionRule's type validation with a 422 (assigning it would make ActiveRecord raise SubclassNotFound, a 500).
180 181 182 183 184 185 |
# File 'app/models/spree/collection.rb', line 180 def self.resolve_rule_type(type) return nil if type.blank? Rails.application.config.spree.collection_rules. find { |klass| klass.api_type == type.to_s }&.to_s end |
Instance Method Details
#generate_slug ⇒ Object
244 245 246 |
# File 'app/models/spree/collection.rb', line 244 def generate_slug permalink.blank? ? name.to_url : permalink.to_url end |
#manual? ⇒ Boolean
113 114 115 |
# File 'app/models/spree/collection.rb', line 113 def manual? !automatic? end |
#manual_sort_order? ⇒ Boolean
117 118 119 |
# File 'app/models/spree/collection.rb', line 117 def manual_sort_order? sort_order == 'manual' end |
#products_matching_rules(opts = {}) ⇒ ActiveRecord::Relation
Products matching the automatic rules (mirrors Spree::Category#products_matching_rules).
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'app/models/spree/collection.rb', line 144 def products_matching_rules(opts = {}) return Spree::Product.none if manual? || rules.empty? currency = opts[:currency] || store.default_currency storefront = opts[:storefront] || false all_products = store.products.not_archived all_products = all_products.active(currency: currency) if storefront any_rules_match_policy = rules_match_policy == 'any' products = any_rules_match_policy ? Spree::Product.none : all_products rules.each do |rule| if any_rules_match_policy product_ids = rule.apply(all_products).ids products = products.or(all_products.where(id: product_ids)) if product_ids.any? else products = rule.apply(products) end end products end |
#regenerate_products(only_once: false) ⇒ void
This method returns an undefined value.
Rebuild the materialized ProductCollection membership from the rules. Pass only_once: true to fire at most once per object lifecycle.
134 135 136 137 138 139 |
# File 'app/models/spree/collection.rb', line 134 def regenerate_products(only_once: false) return unless marked_for_regenerate_products? Spree::Collections::RegenerateProducts.call(collection: self) self.marked_for_regenerate_products = false if !frozen? && only_once end |
#rules=(rules_params) ⇒ void
This method returns an undefined value.
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).
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'app/models/spree/collection.rb', line 200 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_with_index do |rule_data, index| data = rule_data.to_h.with_indifferent_access rule_id = data.delete(:id) if data.key?(:type) || rule_id.blank? # Refused with the accepted kinds named; letting the nil through # answered "type can't be blank" to a caller who had sent one. data[:type] = self.class.resolve_rule_type(data[:type]) || raise(unknown_row_type_error(Spree::CollectionRule, data[:type], "/rules/#{index}")) end 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 |
#set_permalink ⇒ Object
Slug generation, flat (no parent hierarchy). Mirrors Spree::Category'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); with translations on, each Translation generates its own. An existing permalink is sticky — renaming the collection does not rewrite it (set permalink explicitly to change).
236 237 238 239 240 241 242 |
# File 'app/models/spree/collection.rb', line 236 def set_permalink if Spree.use_translations? translations.each(&:set_permalink) else self.permalink = generate_slug end end |
#slug ⇒ Object
121 122 123 |
# File 'app/models/spree/collection.rb', line 121 def slug permalink end |
#slug=(value) ⇒ Object
125 126 127 |
# File 'app/models/spree/collection.rb', line 125 def slug=(value) self.permalink = value end |