Class: Spree::Store

Inherits:
Object
  • Object
show all
Includes:
FriendlyId, Metadata, Metafields, OrderRouting::HasStrategyPreference, Spree::Security::Stores, Spree::Stores::Channels, Spree::Stores::Markets, Spree::Stores::Setup, TranslatableResource, UserManagement
Defined in:
app/models/spree/store.rb

Constant Summary collapse

TRANSLATABLE_FIELDS =

Translations

%i[name meta_description meta_keywords seo_title customer_support_email
address contact_phone].freeze

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::Channels

#ensure_default_channel

Methods included from Spree::Stores::Markets

#countries_available_for_checkout, #countries_from_markets, #default_country, #default_country_id, #default_currency, #default_locale, #has_markets?, #market_for_country, #supported_currencies_list, #supported_locales, #supported_locales_list

Methods included from Spree::Stores::Setup

#payment_method_setup?, #reload, #setup_completed?, #setup_percentage, #setup_task_done?, #setup_tasks, #setup_tasks_done, #setup_tasks_list, #setup_tasks_total, #storefront_setup?

Methods included from Metadata

#metadata, #metadata=, #public_metadata=

Methods included from TranslatableResource

#get_field_with_locale, #translatable_store, #upsert_translations

Instance Attribute Details

#default_country_isoObject

Virtual attribute — sets the country for the default market created on store creation. Not persisted on the store itself; only used by the after_create callback.



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

def default_country_iso
  @default_country_iso
end

Class Method Details

.available_localesObject



196
197
198
# File 'app/models/spree/store.rb', line 196

def self.available_locales
  Spree::Store.default&.supported_locales_list || []
end

.current(_url = nil) ⇒ Object

Delegations



178
179
180
# File 'app/models/spree/store.rb', line 178

def self.current(_url = nil)
  Spree::Current.store
end

.defaultObject

Deprecated.

The or_initialize behavior will be removed in Spree 5.5.



183
184
185
186
187
188
189
190
191
192
193
194
# File 'app/models/spree/store.rb', line 183

def self.default
  # workaround for Mobility bug with first_or_initialize
  if where(default: true).any?
    where(default: true).first
  else
    Spree::Deprecation.warn(
      'Spree::Store.default returning a new unpersisted store when no default store exists is deprecated ' \
      'and will be removed in Spree 6.0. Please ensure a default store is created before calling Store.default.'
    )
    new(default: true)
  end
end

Instance Method Details

#admin_usersObject



373
374
375
376
377
# File 'app/models/spree/store.rb', line 373

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

  users
end

#allowed_origin?(url) ⇒ Boolean

Returns true if the given URL's origin matches one of the store's allowed origins. See AllowedOrigin#matches? for the matching rules (scheme/host/port).

Parameters:

  • url (String)

    the full URL to check

Returns:



306
307
308
309
310
# File 'app/models/spree/store.rb', line 306

def allowed_origin?(url)
  return false if url.blank?

  allowed_origins.any? { |allowed_origin| allowed_origin.matches?(url) }
end

#checkout_zoneObject

Deprecated.

Use Markets instead. Will be removed in Spree 5.5.



210
211
212
213
# File 'app/models/spree/store.rb', line 210

def checkout_zone
  Spree::Deprecation.warn('Store#checkout_zone is deprecated and will be removed in Spree 5.5. Use Markets instead.')
  super
end

#checkout_zone=(zone) ⇒ Object

Deprecated.

Use Markets instead. Will be removed in Spree 5.5.



216
217
218
219
# File 'app/models/spree/store.rb', line 216

def checkout_zone=(zone)
  Spree::Deprecation.warn('Store#checkout_zone= is deprecated and will be removed in Spree 5.5. Use Markets instead.')
  super
end

#countries_with_shipping_coverageActiveRecord::Relation<Spree::Country>

Returns countries covered by at least one shipping zone that has an active shipping method attached. Handles both country-type zones (direct membership) and state-type zones (country inferred from state).

Returns:



340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
# File 'app/models/spree/store.rb', line 340

def countries_with_shipping_coverage
  zone_ids = Spree::Zone
             .joins(:shipping_methods)
             .select(:id)

  country_zone_country_ids = Spree::ZoneMember
                             .where(zone_id: zone_ids, zoneable_type: 'Spree::Country')
                             .select(:zoneable_id)

  state_zone_country_ids = Spree::State
                           .where(id: Spree::ZoneMember
                                      .where(zone_id: zone_ids, zoneable_type: 'Spree::State')
                                      .select(:zoneable_id))
                           .select(:country_id)

  Spree::Country
    .where(id: country_zone_country_ids)
    .or(Spree::Country.where(id: state_zone_country_ids))
    .order(:name)
end

#default_channelSpree::Channel?

Resolves the store's default channel via the default boolean column so promoting another channel in the admin takes effect immediately. Falls back to the first active channel only for malformed data with no flagged default.

Returns:



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

def default_channel
  channels.default.first || channels.active.first
end

#default_shipping_categoryObject



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

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



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

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



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

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

#formatted_urlObject



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'app/models/spree/store.rb', line 254

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



285
286
287
# File 'app/models/spree/store.rb', line 285

def formatted_url_or_custom_domain
  formatted_url
end

#metric_unit_system?Boolean

Returns:



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

def metric_unit_system?
  preferred_unit_system == 'metric'
end

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

Returns the states available for checkout for the store

Parameters:

Returns:



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

def states_available_for_checkout(country)
  country.states.to_a
end

#storefront_urlString

Returns the storefront origin URL for use in customer-facing emails and links. Uses the storefront_url preference when set, then the oldest non-loopback allowed origin (the http://localhost origin seeded on install must never leak into customer emails), otherwise falls back to formatted_url.

Returns:



295
296
297
298
299
# File 'app/models/spree/store.rb', line 295

def storefront_url
  preferred_storefront_url.presence ||
    allowed_origins.order(:created_at).reject(&:loopback?).first&.origin ||
    formatted_url
end

#supported_shipping_zonesObject

Deprecated.

Use Zone.all or #countries_with_shipping_coverage instead. Will be removed in Spree 5.5.



321
322
323
324
325
326
327
328
329
330
331
332
# File 'app/models/spree/store.rb', line 321

def supported_shipping_zones
  Spree::Deprecation.warn(
    'Store#supported_shipping_zones is deprecated and will be removed in Spree 5.5. ' \
    'Use Spree::Zone.all or Store#countries_with_shipping_coverage instead.'
  )
  zone = Spree::Zone.find_by(id: read_attribute(:checkout_zone_id))
  if zone.present?
    [zone]
  else
    Spree::Zone.includes(zone_members: :zoneable).all
  end
end

#unique_nameObject



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

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

#url_or_custom_domainObject



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

def url_or_custom_domain
  url
end