Class: Workarea::Catalog::Product

Inherits:
Object
  • Object
show all
Includes:
Mongoid::Document::Taggable, ApplicationDocument, Workarea::Commentable, Details, Navigable, Releasable
Defined in:
app/models/workarea/catalog/product.rb

Class Method Summary collapse

Instance Method Summary collapse

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

#slug=, #to_param

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

included

Methods included from ApplicationDocument

#releasable?

Methods included from Sidekiq::Callbacks

assert_valid_config!, async, disable, enable, inline, #run_callbacks

Methods included from Mongoid::Document

#embedded_children

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.autocomplete_image_options(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.

Parameters:

Returns:



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

Finds all products that were updated:

If neither are supplied, then all products are returned.

Parameters:

  • options (Hash)

Returns:



92
93
94
95
96
97
# File 'app/models/workarea/catalog/product.rb', line 92

def self.find_by_update(options)
  context = all
  context = context.where(:updated_at.gte => options[:start]) if options[:start].present?
  context = context.where(:updated_at.lte => options[:end])   if options[: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.

Parameters:

Returns:



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.

Returns:

  • (Array<String>)

    representing Catalog::Product#id

  • (Array)

    of Catalog::Product



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

.sortsObject



52
53
54
# File 'app/models/workarea/catalog/product.rb', line 52

def self.sorts
  [Sort.newest, Sort.modified, Sort.sales, Sort.name_asc, Sort.name_desc]
end

Instance Method Details

#active?Boolean

Returns:

  • (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.

Returns:

  • (Boolean)


138
139
140
# File 'app/models/workarea/catalog/product.rb', line 138

def active_by_relations?
  variants.active.any?
end

#categoriesObject



116
117
118
# File 'app/models/workarea/catalog/product.rb', line 116

def categories
  Category.any_in(product_ids: id)
end

#category_idsObject



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.

Returns:

  • (Boolean)


149
150
151
# File 'app/models/workarea/catalog/product.rb', line 149

def purchasable?
  variants.active.blank? ? false : super
end

#skusObject



124
125
126
# File 'app/models/workarea/catalog/product.rb', line 124

def skus
  variants.map(&:sku).uniq
end