Class: Spree::Store
- Inherits:
-
Object
- Object
- Spree::Store
- 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
-
#default_country_iso ⇒ Object
Virtual attribute — sets the country for the default market created on store creation.
Class Method Summary collapse
- .available_locales ⇒ Object
-
.current(_url = nil) ⇒ Object
Delegations.
-
.default ⇒ Object
deprecated
Deprecated.
The or_initialize behavior will be removed in Spree 5.5.
Instance Method Summary collapse
- #admin_users ⇒ Object
-
#allowed_origin?(url) ⇒ Boolean
Returns true if the given URL's origin matches one of the store's allowed origins.
-
#checkout_zone ⇒ Object
deprecated
Deprecated.
Use Markets instead. Will be removed in Spree 5.5.
-
#checkout_zone=(zone) ⇒ Object
deprecated
Deprecated.
Use Markets instead. Will be removed in Spree 5.5.
-
#countries_with_shipping_coverage ⇒ ActiveRecord::Relation<Spree::Country>
Returns countries covered by at least one shipping zone that has an active shipping method attached.
-
#default_channel ⇒ Spree::Channel?
Resolves the store's default channel via the
defaultboolean column so promoting another channel in the admin takes effect immediately. - #default_shipping_category ⇒ Object
-
#default_stock_location ⇒ Spree::StockLocation
Returns the default stock location for the store or creates a new one if it doesn't exist.
- #digital_shipping_category ⇒ Object
- #formatted_url ⇒ Object
- #formatted_url_or_custom_domain ⇒ Object
- #metric_unit_system? ⇒ Boolean
-
#states_available_for_checkout(country) ⇒ Array<Spree::State>
Returns the states available for checkout for the store.
-
#storefront_url ⇒ String
Returns the storefront origin URL for use in customer-facing emails and links.
-
#supported_shipping_zones ⇒ Object
deprecated
Deprecated.
Use Zone.all or #countries_with_shipping_coverage instead. Will be removed in Spree 5.5.
- #unique_name ⇒ Object
- #url_or_custom_domain ⇒ Object
Methods included from UserManagement
#add_user, #default_user_role, #remove_user
Methods included from Spree::Stores::Channels
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_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_iso ⇒ Object
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.
212 213 214 |
# File 'app/models/spree/store.rb', line 212 def default_country_iso @default_country_iso end |
Class Method Details
.available_locales ⇒ Object
185 186 187 |
# File 'app/models/spree/store.rb', line 185 def self.available_locales Spree::Store.default&.supported_locales_list || [] end |
.current(_url = nil) ⇒ Object
Delegations
167 168 169 |
# File 'app/models/spree/store.rb', line 167 def self.current(_url = nil) Spree::Current.store end |
.default ⇒ Object
The or_initialize behavior will be removed in Spree 5.5.
172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'app/models/spree/store.rb', line 172 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_users ⇒ Object
358 359 360 361 362 |
# File 'app/models/spree/store.rb', line 358 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).
291 292 293 294 295 |
# File 'app/models/spree/store.rb', line 291 def allowed_origin?(url) return false if url.blank? allowed_origins.any? { |allowed_origin| allowed_origin.matches?(url) } end |
#checkout_zone ⇒ Object
Use Markets instead. Will be removed in Spree 5.5.
199 200 201 202 |
# File 'app/models/spree/store.rb', line 199 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
Use Markets instead. Will be removed in Spree 5.5.
205 206 207 208 |
# File 'app/models/spree/store.rb', line 205 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_coverage ⇒ ActiveRecord::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).
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 |
# File 'app/models/spree/store.rb', line 325 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_channel ⇒ Spree::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.
194 195 196 |
# File 'app/models/spree/store.rb', line 194 def default_channel channels.default.first || channels.active.first end |
#default_shipping_category ⇒ Object
368 369 370 |
# File 'app/models/spree/store.rb', line 368 def default_shipping_category @default_shipping_category ||= ShippingCategory.find_or_create_by(name: 'Default') end |
#default_stock_location ⇒ Spree::StockLocation
Returns the default stock location for the store or creates a new one if it doesn't exist
348 349 350 351 352 353 354 355 356 |
# File 'app/models/spree/store.rb', line 348 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_category ⇒ Object
372 373 374 |
# File 'app/models/spree/store.rb', line 372 def digital_shipping_category @digital_shipping_category ||= ShippingCategory.find_or_create_by(name: 'Digital') end |
#formatted_url ⇒ Object
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 |
# File 'app/models/spree/store.rb', line 243 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.[:protocol] || :http port = Rails.application.routes.[: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_domain ⇒ Object
274 275 276 |
# File 'app/models/spree/store.rb', line 274 def formatted_url_or_custom_domain formatted_url end |
#metric_unit_system? ⇒ Boolean
364 365 366 |
# File 'app/models/spree/store.rb', line 364 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
300 301 302 |
# File 'app/models/spree/store.rb', line 300 def states_available_for_checkout(country) country.states.to_a end |
#storefront_url ⇒ String
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.
282 283 284 |
# File 'app/models/spree/store.rb', line 282 def storefront_url allowed_origins.order(:created_at).pick(:origin) || formatted_url end |
#supported_shipping_zones ⇒ Object
Use Zone.all or #countries_with_shipping_coverage instead. Will be removed in Spree 5.5.
306 307 308 309 310 311 312 313 314 315 316 317 |
# File 'app/models/spree/store.rb', line 306 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_name ⇒ Object
239 240 241 |
# File 'app/models/spree/store.rb', line 239 def unique_name @unique_name ||= "#{name} (#{code})" end |
#url_or_custom_domain ⇒ Object
270 271 272 |
# File 'app/models/spree/store.rb', line 270 def url_or_custom_domain url end |