Class: Workarea::Catalog::Product
- Inherits:
-
Object
- Object
- Workarea::Catalog::Product
- Includes:
- Mongoid::Document::Taggable, ApplicationDocument, Workarea::Commentable, Details, Navigable, Releasable
- Defined in:
- app/models/workarea/catalog/product.rb
Class Method Summary collapse
- .autocomplete_image_options(string) ⇒ Object
-
.find_by_sku(sku) ⇒ Catalog::Product?
Finds the first product that has the given SKU.
- .find_by_update(options) ⇒ Mongoid::Criteria
-
.find_for_update_by_sku(sku) ⇒ Array<Catalog::Product>
Find any and all products which have a variant set with the SKU, for the purposes of updating search indexes, caches, etc.
-
.find_ordered_for_display(*ids) ⇒ Array<String>, Array
Finds the corresponding products for the given ids that are active and returns them in the same order.
- .sorts ⇒ Object
Instance Method Summary collapse
- #active? ⇒ Boolean
-
#active_by_relations? ⇒ Boolean
This hook allows plugins extending this model a place to hook into #active? without overriding all the logic in that method’s ‘super`.
- #categories ⇒ Object
- #category_ids ⇒ Object
-
#purchasable? ⇒ Boolean
Whether to allow purchasing on this product.
- #skus ⇒ Object
Methods included from Details
#detail_names, #fetch_detail, #has_detail?, #matches_detail?, #matches_details?, #update_details
Methods included from Workarea::Commentable
#add_subscription, #remove_subscription
Methods included from Navigable
Methods included from Releasable
#changesets_with_children, #destroy, #in_release, #release_changes, #release_originals, #save_changeset, #skip_changeset, #without_release
Methods included from Segmentable
#active_segment_ids_with_children, #segmented?, #segments
Methods included from Release::Activation
#activate_with?, #create_activation_changeset, #save_activate_with, #was_active?
Methods included from Mongoid::Document::Taggable
Methods included from ApplicationDocument
Methods included from Sidekiq::Callbacks
assert_valid_config!, async, disable, enable, inline, #run_callbacks
Methods included from Mongoid::Document
Class Method Details
.autocomplete_image_options(string) ⇒ Object
109 110 111 112 113 114 |
# File 'app/models/workarea/catalog/product.rb', line 109 def self.(string) regex = /#{::Regexp.quote(string)}/i where('images.option' => regex).map do |product| product.images.where(option: regex).map(&:option) end.flatten.map(&:titleize).uniq end |
.find_by_sku(sku) ⇒ Catalog::Product?
Finds the first product that has the given SKU. Tries to match exactly, but will look for a case-insensitive match if exact match isn’t found.
62 63 64 65 66 67 68 69 70 71 |
# File 'app/models/workarea/catalog/product.rb', line 62 def self.find_by_sku(sku) return unless sku.present? begin find_by('variants.sku' => sku) rescue Mongoid::Errors::DocumentNotFound regex = /^#{::Regexp.quote(sku)}$/i find_by('variants.sku' => regex) rescue nil end end |
.find_by_update(options) ⇒ Mongoid::Criteria
92 93 94 95 96 97 |
# File 'app/models/workarea/catalog/product.rb', line 92 def self.find_by_update() context = all context = context.where(:updated_at.gte => [:start]) if [:start].present? context = context.where(:updated_at.lte => [:end]) if [:end].present? context end |
.find_for_update_by_sku(sku) ⇒ Array<Catalog::Product>
Find any and all products which have a variant set with the SKU, for the purposes of updating search indexes, caches, etc.
79 80 81 |
# File 'app/models/workarea/catalog/product.rb', line 79 def self.find_for_update_by_sku(sku) where('variants.sku' => sku) end |
.find_ordered_for_display(*ids) ⇒ Array<String>, Array
Finds the corresponding products for the given ids that are active and returns them in the same order.
105 106 107 |
# File 'app/models/workarea/catalog/product.rb', line 105 def self.find_ordered_for_display(*ids) find_ordered(ids).select(&:active?) end |
Instance Method Details
#active? ⇒ Boolean
128 129 130 |
# File 'app/models/workarea/catalog/product.rb', line 128 def active? super && active_by_relations? end |
#active_by_relations? ⇒ Boolean
This hook allows plugins extending this model a place to hook into #active? without overriding all the logic in that method’s ‘super`. For example, package products.
138 139 140 |
# File 'app/models/workarea/catalog/product.rb', line 138 def active_by_relations? variants.active.any? end |
#categories ⇒ Object
116 117 118 |
# File 'app/models/workarea/catalog/product.rb', line 116 def categories Category.any_in(product_ids: id) end |
#category_ids ⇒ Object
120 121 122 |
# File 'app/models/workarea/catalog/product.rb', line 120 def category_ids categories.map(&:id) end |
#purchasable? ⇒ Boolean
Whether to allow purchasing on this product. This is always false if there are no variants associated with the product because of all the detail page and cart logic depending on the existence of SKUs and variants.
149 150 151 |
# File 'app/models/workarea/catalog/product.rb', line 149 def purchasable? variants.active.blank? ? false : super end |
#skus ⇒ Object
124 125 126 |
# File 'app/models/workarea/catalog/product.rb', line 124 def skus variants.map(&:sku).uniq end |