Class: Workarea::Pricing::Sku

Inherits:
Object
  • Object
show all
Includes:
ApplicationDocument, UnsupportedSegmentation, Releasable
Defined in:
app/models/workarea/pricing/sku.rb

Defined Under Namespace

Classes: MissingPrices

Class Method Summary collapse

Instance Method Summary collapse

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?, #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 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

.sortsObject



20
21
22
# File 'app/models/workarea/pricing/sku.rb', line 20

def self.sorts
  [Sort.modified, Sort.sku, Sort.newest]
end

Instance Method Details

#active_pricesArray<Workarea::Pricing::Price>

All active prices (with i18n fallbacks included) for the given SKU.

Returns:



73
74
75
# File 'app/models/workarea/pricing/sku.rb', line 73

def active_prices
  prices.select(&:active).sort_by(&:min_quantity).reverse
end

#find_price(options = {}) ⇒ Pricing::Price

Find the price to sell at for a specific options.

The only supported option is :quantity, but this takes an options hash as an extension point to plugins and implementations.

Parameters:

  • (Hash)

Returns:



40
41
42
43
44
45
46
47
48
# File 'app/models/workarea/pricing/sku.rb', line 40

def find_price(options = {})
  quantity = options[:quantity] || 1

  match = active_prices.detect do |price|
    quantity >= price.min_quantity
  end

  match || Price.new
end

#nameString

This is for compatibility with the admin, all models must implement this

Returns:



28
29
30
# File 'app/models/workarea/pricing/sku.rb', line 28

def name
  I18n.t('workarea.pricing_sku.name', id: id)
end

#sale_priceMoney

Default sale price, when one unit is placed into the cart of an anonymous user.

Returns:



65
66
67
# File 'app/models/workarea/pricing/sku.rb', line 65

def sale_price
  find_price(quantity: 1).sale
end

#sell_priceMoney Also known as: regular_price

Default selling price, when one unit is placed into the cart of an anonymous user.

Returns:



55
56
57
# File 'app/models/workarea/pricing/sku.rb', line 55

def sell_price
  find_price(quantity: 1).sell
end