Class: Spree::Channel

Inherits:
Object
  • Object
show all
Includes:
Gating, HasCustomFields, Metadata, OrderRouting::HasStrategyPreference, SingleStoreResource
Defined in:
app/models/spree/channel.rb,
app/models/spree/channel/gating.rb

Overview

Lightweight distribution surface within a Store: online storefront, POS, marketplace integration, wholesale portal. Channels carry order attribution and the routing-strategy override.

Defined Under Namespace

Modules: Gating

Constant Summary collapse

DEFAULT_CODE =
'online'.freeze

Constants included from Gating

Gating::STOREFRONT_ACCESS

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Gating

#resolved_guest_checkout, #resolved_storefront_access, #storefront_login_required?, #storefront_prices_hidden?

Methods included from Metadata

#private_metadata, #private_metadata=

Class Method Details

.add_products(product_ids) ⇒ Integer

Publishes the given products on every channel in the current scope (store.channels.where(id: ids).add_products(product_ids)), touching and reindexing each product once rather than once per channel.

Parameters:

  • product_ids (Array<Integer>, Integer)

Returns:

  • (Integer) —

    the number of ProductPublications created or updated



71
72
73
74
75
76
77
78
79
80
# File 'app/models/spree/channel.rb', line 71

def self.add_products(product_ids)
  product_ids = Array(product_ids).map(&:to_s).uniq
  channels = all.to_a
  return 0 if product_ids.empty? || channels.empty?

  count = channels.sum { |channel| channel.send(:upsert_publications, product_ids) }
  refresh_products(product_ids)
  channels.each(&:touch)
  count
end

.refresh_products(product_ids) ⇒ void

This method returns an undefined value.

Parameters:

  • product_ids (Array<String>)


99
100
101
102
103
104
105
# File 'app/models/spree/channel.rb', line 99

def self.refresh_products(product_ids)
  products = Spree::Product.where(id: product_ids)
  products.touch_all
  # `to_a` once: `touch_all` does not load the relation, so iterating it
  # afterwards would re-run the query.
  products.to_a.each(&:enqueue_search_index)
end

.remove_products(product_ids) ⇒ Integer

Unpublishes the given products from every channel in the current scope.

Parameters:

  • product_ids (Array<Integer>, Integer)

Returns:

  • (Integer) —

    the number of ProductPublications destroyed



85
86
87
88
89
90
91
92
93
94
95
# File 'app/models/spree/channel.rb', line 85

def self.remove_products(product_ids)
  product_ids = Array(product_ids).map(&:to_s).uniq
  channels = all.to_a
  return 0 if product_ids.empty? || channels.empty?

  removed = Spree::ProductPublication.includes(:product).where(channel_id: channels.map(&:id), product_id: product_ids).destroy_all
  refresh_products(product_ids)
  emptied_channel_ids = removed.map(&:channel_id).uniq
  channels.select { |channel| emptied_channel_ids.include?(channel.id) }.each(&:touch)
  removed.size
end

Instance Method Details

#add_products(product_ids, published_at: nil, unpublished_at: nil) ⇒ Integer

Publishes the given products on this channel by creating/upserting ProductPublications. Optionally sets the publication window; if not given, the products will be published immediately with no end date.

Parameters:

  • product_ids (Array<Integer>, Integer) —

    the IDs of the products to publish on this channel

  • published_at (Time, nil) (defaults to: nil) —

    when the publications go live; nil means immediately

  • unpublished_at (Time, nil) (defaults to: nil) —

    when the publications come down; nil means never

Returns:

  • (Integer) —

    the number of ProductPublications created or updated



114
115
116
117
118
119
120
121
122
# File 'app/models/spree/channel.rb', line 114

def add_products(product_ids, published_at: nil, unpublished_at: nil)
  product_ids = Array(product_ids).map(&:to_s).uniq
  return 0 if product_ids.empty?

  count = upsert_publications(product_ids, published_at: published_at, unpublished_at: unpublished_at)
  self.class.refresh_products(product_ids)
  touch
  count
end

#can_be_deleted? ⇒ Boolean

Returns:

  • (Boolean)


139
140
141
# File 'app/models/spree/channel.rb', line 139

def can_be_deleted?
  !default? && !api_keys.active.exists?
end

#remove_products(product_ids) ⇒ Integer

Unpublishes the given products from this channel.

Parameters:

  • product_ids (Array<Integer>, Integer) —

    the IDs of the products to unpublish

Returns:

  • (Integer) —

    the number of ProductPublications destroyed



127
128
129
130
131
132
133
134
135
136
# File 'app/models/spree/channel.rb', line 127

def remove_products(product_ids)
  product_ids = Array(product_ids).map(&:to_s).uniq
  return 0 if product_ids.empty?

  count = publications.includes(:product).where(product_id: product_ids).destroy_all.size

  self.class.refresh_products(product_ids)
  touch if count.positive?
  count
end

#served_stock_location_ids ⇒ Array<Integer>

Returns ids of the locations this channel is limited to.

Returns:

  • (Array<Integer>) —

    ids of the locations this channel is limited to



156
157
158
# File 'app/models/spree/channel.rb', line 156

def served_stock_location_ids
  @served_stock_location_ids ||= channel_stock_locations.map(&:stock_location_id)
end

#served_stock_locations ⇒ ActiveRecord::Relation<Spree::StockLocation>

The locations allowed to fulfill this channel's traffic.

Returns:



163
164
165
166
167
# File 'app/models/spree/channel.rb', line 163

def served_stock_locations
  return store.stock_locations if served_stock_location_ids.empty?

  store.stock_locations.where(id: served_stock_location_ids)
end

#serves_location?(stock_location) ⇒ Boolean

Whether packages originating from this stock location may serve this channel's carts. No linked locations means every store location.

Parameters:

Returns:

  • (Boolean)


148
149
150
151
152
153
# File 'app/models/spree/channel.rb', line 148

def serves_location?(stock_location)
  return false if stock_location.nil?
  return true if served_stock_location_ids.empty?

  served_stock_location_ids.include?(stock_location.id)
end