Class: Spree::Store

Inherits:
Object
  • Object
show all
Includes:
FriendlyId, HasPageLinks, Metadata, Metafields, Spree::Security::Stores, Spree::Stores::Setup, Spree::Stores::Socials, TranslatableResource, UserManagement, Webhooks::HasWebhooks
Defined in:
app/models/spree/store.rb

Constant Summary collapse

RESERVED_CODES =
%w(
  admin default app api www cdn files assets checkout account auth login user
)
TRANSLATABLE_FIELDS =

Translations

i[name meta_description meta_keywords seo_title facebook
twitter instagram customer_support_email
address contact_phone].freeze

Constants included from Spree::Stores::Socials

Spree::Stores::Socials::SOCIAL_NETWORKS_CONFIG, Spree::Stores::Socials::SUPPORTED_SOCIAL_NETWORKS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from UserManagement

#add_user, #default_user_role, #remove_user

Methods included from Spree::Stores::Socials

#social_handle, #social_links

Methods included from Spree::Stores::Setup

#payment_method_setup?, #setup_completed?, #setup_percentage, #setup_task_done?, #setup_tasks_done, #setup_tasks_list, #setup_tasks_total

Instance Attribute Details

#skip_validate_not_lastObject

Returns the value of attribute skip_validate_not_last.



150
151
152
# File 'app/models/spree/store.rb', line 150

def skip_validate_not_last
  @skip_validate_not_last
end

Class Method Details

.available_localesObject



232
233
234
# File 'app/models/spree/store.rb', line 232

def self.available_locales
  Spree::Store.all.map(&:supported_locales_list).flatten.uniq
end

.current(url = nil) ⇒ Object



211
212
213
214
215
216
217
# File 'app/models/spree/store.rb', line 211

def self.current(url = nil)
  if url.present?
    Spree.current_store_finder.new(url: url).execute
  else
    Spree::Current.store
  end
end

.defaultObject

FIXME: we need to drop ‘or_initialize` in v5 this behaviour is very buggy and unpredictable



221
222
223
224
225
226
227
228
229
230
# File 'app/models/spree/store.rb', line 221

def self.default
  Rails.cache.fetch('default_store') do
    # workaround for Mobility bug with first_or_initialize
    if where(default: true).any?
      where(default: true).first
    else
      new(default: true)
    end
  end
end

Instance Method Details

#active_webhooks_subscribersArray<Spree::Webhooks::Subscriber>

Returns all active webhooks subscribers for the store

Returns:

  • (Array<Spree::Webhooks::Subscriber>)


432
433
434
# File 'app/models/spree/store.rb', line 432

def active_webhooks_subscribers
  @active_webhooks_subscribers ||= Spree::Webhooks::Subscriber.active
end

#admin_usersObject



375
376
377
378
379
# File 'app/models/spree/store.rb', line 375

def admin_users
  Spree::Deprecation.warn('Store#admin_users is deprecated and will be removed in Spree 6.0. Please use Store#users instead.')

  users
end

#can_be_deleted?Boolean

Returns:



387
388
389
# File 'app/models/spree/store.rb', line 387

def can_be_deleted?
  self.class.where.not(id: id).any?
end

#checkout_zone_or_defaultObject



350
351
352
353
354
# File 'app/models/spree/store.rb', line 350

def checkout_zone_or_default
  Spree::Deprecation.warn('Store#checkout_zone_or_default is deprecated and will be removed in Spree 5')

  @checkout_zone_or_default ||= checkout_zone || Spree::Zone.default_checkout_zone
end

#countries_available_for_checkoutArray<Spree::Country>

Returns the countries available for checkout for the store or creates a new one if it doesn’t exist

Returns:



335
336
337
338
339
# File 'app/models/spree/store.rb', line 335

def countries_available_for_checkout
  @countries_available_for_checkout ||= Rails.cache.fetch(countries_available_for_checkout_cache_key) do
    (checkout_zone.try(:country_list) || Spree::Country.all).to_a
  end
end

#default_country_iso=(iso) ⇒ Object



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'app/models/spree/store.rb', line 236

def default_country_iso=(iso)
  return if iso.blank?

  @default_country_iso = iso

  country = Spree::Country.by_iso(iso)

  if country.present?
    self.default_country = country
  elsif iso_country = ::Country[iso]
    new_country = Spree::Country.create!(
      iso_name: iso_country.local_name&.upcase,
      iso: iso_country.alpha2,
      iso3: iso_country.alpha3,
      name: iso_country.local_name,
      numcode: iso_country.number,
      states_required: Spree::Address::STATES_REQUIRED.include?(iso),
      zipcode_required: !Spree::Address::NO_ZIPCODE_ISO_CODES.include?(iso)
    )

    self.default_country = new_country
  end
end

#default_shipping_categoryObject



395
396
397
# File 'app/models/spree/store.rb', line 395

def default_shipping_category
  @default_shipping_category ||= ShippingCategory.find_or_create_by(name: 'Default')
end

#default_stock_locationSpree::StockLocation

Returns the default stock location for the store or creates a new one if it doesn’t exist



366
367
368
369
370
371
372
373
# File 'app/models/spree/store.rb', line 366

def default_stock_location
  @default_stock_location ||= begin
    stock_location_scope = Spree::StockLocation.where(default: true)
    stock_location_scope.first || ActiveRecord::Base.connected_to(role: :writing) do
      stock_location_scope.create(default: true, name: Spree.t(:default_stock_location_name), country: default_country)
    end
  end
end

#digital_shipping_categoryObject



399
400
401
# File 'app/models/spree/store.rb', line 399

def digital_shipping_category
  @digital_shipping_category ||= ShippingCategory.find_or_create_by(name: 'Digital')
end

#faviconObject



381
382
383
384
385
# File 'app/models/spree/store.rb', line 381

def favicon
  return unless favicon_image.attached? && favicon_image.variable?

  favicon_image.variant(resize_to_limit: [32, 32])
end

#formatted_custom_domainObject



311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'app/models/spree/store.rb', line 311

def formatted_custom_domain
  return unless default_custom_domain

  @formatted_custom_domain ||= if Rails.env.development? || Rails.env.test?
    URI::Generic.build(
      scheme: Rails.application.routes.default_url_options[:protocol] || 'http',
      host: default_custom_domain.url,
      port: Rails.application.routes.default_url_options[:port]
    ).to_s
  else
    URI::HTTPS.build(host: default_custom_domain.url).to_s
  end
end

#formatted_urlObject



284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'app/models/spree/store.rb', line 284

def formatted_url
  @formatted_url ||= begin
    clean_url = url.to_s.sub(%r{^https?://}, '').split(':').first

    if Rails.env.development? || Rails.env.test?
      scheme = Rails.application.routes.default_url_options[:protocol] || :http
      port = Rails.application.routes.default_url_options[:port].presence || (Rails.env.development? ? 3000 : nil)

      if scheme.to_sym == :https
        URI::HTTPS.build(
          host: clean_url,
          port: port
        ).to_s
      else
        URI::HTTP.build(
          host: clean_url,
          port: port
        ).to_s
      end
    else
      URI::HTTPS.build(
        host: clean_url
      ).to_s
    end
  end
end

#formatted_url_or_custom_domainObject



329
330
331
# File 'app/models/spree/store.rb', line 329

def formatted_url_or_custom_domain
  formatted_custom_domain || formatted_url
end

#import_payment_methods_from_storeObject



412
413
414
415
416
417
418
419
# File 'app/models/spree/store.rb', line 412

def import_payment_methods_from_store
  store = Store.find(import_payment_methods_from_store_id)
  payment_method_ids = store.payment_method_ids

  return if payment_method_ids.empty?

  StorePaymentMethod.insert_all(payment_method_ids.map { |payment_method_id| { store_id: id, payment_method_id: payment_method_id } })
end

#import_products_from_storeObject



403
404
405
406
407
408
409
410
# File 'app/models/spree/store.rb', line 403

def import_products_from_store
  store = Store.find(import_products_from_store_id)
  product_ids = store.products.pluck(:id)

  return if product_ids.empty?

  StoreProduct.insert_all(product_ids.map { |product_id| { store_id: id, product_id: product_id } })
end

#metric_unit_system?Boolean

Returns:



391
392
393
# File 'app/models/spree/store.rb', line 391

def metric_unit_system?
  preferred_unit_system == 'metric'
end

#seo_meta_descriptionObject



266
267
268
269
270
271
272
273
274
# File 'app/models/spree/store.rb', line 266

def seo_meta_description
  if meta_description.present?
    meta_description
  elsif seo_title.present?
    seo_title
  else
    name
  end
end

#states_available_for_checkout(country) ⇒ Array<Spree::State>

Returns the states available for checkout for the store or creates a new one if it doesn’t exist

Parameters:

Returns:



344
345
346
347
348
# File 'app/models/spree/store.rb', line 344

def states_available_for_checkout(country)
  Rails.cache.fetch(states_available_for_checkout_cache_key(country)) do
    (checkout_zone.try(:state_list_for, country) || country.states).to_a
  end
end

#supported_currencies_listObject



260
261
262
263
264
# File 'app/models/spree/store.rb', line 260

def supported_currencies_list
  @supported_currencies_list ||= ([default_currency] + read_attribute(:supported_currencies).to_s.split(',')).uniq.map(&:to_s).map do |code|
    ::Money::Currency.find(code.strip)
  end.compact.sort_by { |currency| currency.iso_code == default_currency ? 0 : 1 }
end

#supported_locales_listObject



276
277
278
# File 'app/models/spree/store.rb', line 276

def supported_locales_list
  @supported_locales_list ||= (read_attribute(:supported_locales).to_s.split(',') << default_locale).compact.uniq.sort
end

#supported_shipping_zonesObject



356
357
358
359
360
361
362
# File 'app/models/spree/store.rb', line 356

def supported_shipping_zones
  @supported_shipping_zones ||= if checkout_zone.present?
                                  [checkout_zone]
                                else
                                  Spree::Zone.includes(zone_members: :zoneable).all
                                end
end

#unique_nameObject



280
281
282
# File 'app/models/spree/store.rb', line 280

def unique_name
  @unique_name ||= "#{name} (#{code})"
end

#url_or_custom_domainObject



325
326
327
# File 'app/models/spree/store.rb', line 325

def url_or_custom_domain
  default_custom_domain&.url || url
end