Class: Spree::Store

Inherits:
Object
  • Object
show all
Includes:
FriendlyId, Metadata, 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.



145
146
147
# File 'app/models/spree/store.rb', line 145

def skip_validate_not_last
  @skip_validate_not_last
end

Class Method Details

.available_localesObject



222
223
224
# File 'app/models/spree/store.rb', line 222

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

.current(url = nil) ⇒ Object



205
206
207
# File 'app/models/spree/store.rb', line 205

def self.current(url = nil)
  Spree::Dependencies.current_store_finder.constantize.new(url: url).execute || Spree::Current.store
end

.defaultObject

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



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

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

#admin_usersObject



360
361
362
363
364
# File 'app/models/spree/store.rb', line 360

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:

  • (Boolean)


372
373
374
# File 'app/models/spree/store.rb', line 372

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

#checkout_zone_or_defaultObject



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

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_checkoutObject



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

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
  end
end

#default_country_iso=(iso) ⇒ Object



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'app/models/spree/store.rb', line 226

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



380
381
382
# File 'app/models/spree/store.rb', line 380

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



351
352
353
354
355
356
357
358
# File 'app/models/spree/store.rb', line 351

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



384
385
386
# File 'app/models/spree/store.rb', line 384

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

#faviconObject



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

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

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

#formatted_custom_domainObject



301
302
303
304
305
306
307
308
309
310
311
312
313
# File 'app/models/spree/store.rb', line 301

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



274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# File 'app/models/spree/store.rb', line 274

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



319
320
321
# File 'app/models/spree/store.rb', line 319

def formatted_url_or_custom_domain
  formatted_custom_domain || formatted_url
end

#import_payment_methods_from_storeObject



397
398
399
400
401
402
403
404
# File 'app/models/spree/store.rb', line 397

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



388
389
390
391
392
393
394
395
# File 'app/models/spree/store.rb', line 388

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:

  • (Boolean)


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

def metric_unit_system?
  preferred_unit_system == 'metric'
end

#seo_meta_descriptionObject



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

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) ⇒ Object



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

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
  end
end

#supported_currencies_listObject



250
251
252
253
254
# File 'app/models/spree/store.rb', line 250

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



266
267
268
# File 'app/models/spree/store.rb', line 266

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

#supported_shipping_zonesObject



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

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



270
271
272
# File 'app/models/spree/store.rb', line 270

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

#url_or_custom_domainObject



315
316
317
# File 'app/models/spree/store.rb', line 315

def url_or_custom_domain
  default_custom_domain&.url || url
end