Class: Spree::Store

Inherits:
Object
  • Object
show all
Includes:
FriendlyId, Metadata, Metafields, Spree::Security::Stores, 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::Markets

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

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

Methods included from Metadata

#metadata, #metadata=, #public_metadata=

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.



192
193
194
# File 'app/models/spree/store.rb', line 192

def default_country_iso
  @default_country_iso
end

Class Method Details

.available_localesObject



174
175
176
# File 'app/models/spree/store.rb', line 174

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

.current(_url = nil) ⇒ Object

Delegations



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

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

.defaultObject

Deprecated.

The or_initialize behavior will be removed in Spree 5.5.



161
162
163
164
165
166
167
168
169
170
171
172
# File 'app/models/spree/store.rb', line 161

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 5.5. Please ensure a default store is created before calling Store.default.'
    )
    new(default: true)
  end
end

Instance Method Details

#admin_usersObject



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

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. Comparison is port-less: only scheme + host are matched, so storing ‘localhost` will match `localhost:3000`, `localhost:4000`, etc.

Parameters:

  • url (String)

    the full URL to check

Returns:



272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'app/models/spree/store.rb', line 272

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

  uri = URI.parse(url)
  request_origin = "#{uri.scheme}://#{uri.host}"

  allowed_origins.pluck(:origin).any? do |stored|
    stored_uri = URI.parse(stored)
    "#{stored_uri.scheme}://#{stored_uri.host}" == request_origin
  rescue URI::InvalidURIError
    false
  end
rescue URI::InvalidURIError
  false
end

#checkout_zoneObject

Deprecated.

Use Markets instead. Will be removed in Spree 5.5.



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

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.



185
186
187
188
# File 'app/models/spree/store.rb', line 185

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:



316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'app/models/spree/store.rb', line 316

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_shipping_categoryObject



359
360
361
# File 'app/models/spree/store.rb', line 359

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



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

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



363
364
365
# File 'app/models/spree/store.rb', line 363

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

#formatted_urlObject



223
224
225
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 223

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



254
255
256
# File 'app/models/spree/store.rb', line 254

def formatted_url_or_custom_domain
  formatted_url
end

#metric_unit_system?Boolean

Returns:



355
356
357
# File 'app/models/spree/store.rb', line 355

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:



291
292
293
# File 'app/models/spree/store.rb', line 291

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 first allowed origin if configured, otherwise falls back to formatted_url.

Returns:



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

def storefront_url
  allowed_origins.order(:created_at).pick(:origin) || formatted_url
end

#supported_shipping_zonesObject

Deprecated.

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



297
298
299
300
301
302
303
304
305
306
307
308
# File 'app/models/spree/store.rb', line 297

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



219
220
221
# File 'app/models/spree/store.rb', line 219

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

#url_or_custom_domainObject



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

def url_or_custom_domain
  url
end